Fix: send nCloud Outbound mail (failed)

This commit is contained in:
2019-04-20 16:54:15 +09:00
parent e4f55f5d23
commit c51d4be681
2 changed files with 78 additions and 16 deletions
+73 -12
View File
@@ -37,7 +37,7 @@ class MailSender:
bank_owner_name = "박지상" bank_owner_name = "박지상"
def __init__(self): def __init__(self):
post_param = self.make_default_post_param() self.post_param = MailSender.make_default_post_param()
def send_email(self, timestamp, signature): def send_email(self, timestamp, signature):
url = NCloud.host_name_url + NCloud.request_url url = NCloud.host_name_url + NCloud.request_url
@@ -46,12 +46,12 @@ class MailSender:
headers = { headers = {
'X-NCP-APIGW-TIMESTAMP': timestamp, 'X-NCP-APIGW-TIMESTAMP': timestamp,
'X-NCP-IAM-ACCESS-KEY': NCloud.access_key, 'X-NCP-IAM-ACCESS-KEY': NCloud.access_key,
'X-NCP-APIGW-SIGNATURE-V2': signature, 'X-NCP-APIGW-SIGNATURE-V2': signature.decode('utf-8'),
'content-type': 'application/json' 'Content-type': 'application/json'
} }
# print(url) # print(url)
# print('headers : ' + json.dumps(headers)) # print('headers : ' + json.dumps(headers, indent='\t'))
# print('data : ' + json.dumps(post_param)) # print('data : ' + json.dumps(self.post_param, indent='\t'))
# exit(0) # exit(0)
res = requests.post(url, headers=headers, data=json.dumps(self.post_param)) res = requests.post(url, headers=headers, data=json.dumps(self.post_param))
@@ -81,7 +81,7 @@ class MailSender:
self.post_param[key] = value self.post_param[key] = value
def set_post_param_recipients(self, maestro_name, maestro_email): def set_post_param_recipients(self, maestro_name, maestro_email):
self.post_param['recipients']: [ self.set_post_param('recipients', [
{ {
'address': maestro_email, 'address': maestro_email,
'name': '', 'name': '',
@@ -96,10 +96,11 @@ class MailSender:
'type': 'B', 'type': 'B',
'parameters': [] 'parameters': []
} }
] ])
def make_default_post_param(self): @staticmethod
self.post_param = { def make_default_post_param():
post_param = {
'advertising': False, # 광고메일여부 default: False 'advertising': False, # 광고메일여부 default: False
'attachFileIds': [], # 첨부파일ID 'attachFileIds': [], # 첨부파일ID
'body': '', # Email 본문 'body': '', # Email 본문
@@ -121,13 +122,16 @@ class MailSender:
'templateSid': 0, # 템플릿 ID 'templateSid': 0, # 템플릿 ID
'title': '' 'title': ''
} }
return post_param
@staticmethod @staticmethod
def get_timestamp(): def get_timestamp():
# list(micro_time,timestamp) = explode(' ',microtime())
time_array = MailSender.micro_time().split(' '); time_array = MailSender.micro_time().split(' ');
microtime = time_array[0]; microtime = time_array[0];
timestamp = time_array[1]; timestamp = time_array[1];
# print('microtime : ' + microtime)
# print('timestamp : ' + timestamp)
# print('microtime[2:5] : ' + microtime[2:5])
return timestamp + microtime[2:5] return timestamp + microtime[2:5]
@staticmethod @staticmethod
@@ -149,17 +153,74 @@ class MailSender:
def b64encode(source): def b64encode(source):
source = source.encode('utf=8') source = source.encode('utf=8')
return base64.b64encode(source) return base64.b64encode(source)
# a = base64.b64encode(bytes(source.encode('utf-8')))
# b = base64.b64decode(a).decode('utf-8', 'ignore')
# return b
@staticmethod @staticmethod
def make_signature(url, timestamp): def make_signature(url, timestamp):
space = " " space = " "
new_line = "\n" new_line = "\n"
hmac_data = NCloud.method + space + url + new_line + timestamp + new_line + NCloud.access_key hmac_data = NCloud.method + space + url + new_line + timestamp + new_line + NCloud.access_key
# print('hmac_data : ' + hmac_data)
hash_hmac_data = MailSender.hash_hmac(hashlib.sha256, hmac_data, NCloud.secret_key) hash_hmac_data = MailSender.hash_hmac(hashlib.sha256, hmac_data, NCloud.secret_key)
# signature = MailSender.b64encode(hash_hmac_data) signature = MailSender.b64encode(hash_hmac_data)
signature = hash_hmac_data # signature = base64.b64encode(bytes(hash_hmac_data, 'utf-8'))
# signature = hash_hmac_data
return signature return signature
@staticmethod
def send_temp_mail():
post_param = {
"advertising": False, # 광고메일여부 default: False
"attachFileIds": [], # 첨부파일ID
"body": "", # Email 본문
"individual": False, # 개인발송여부 (개인발송 시 참조인, 숨은참조 무시됨) default: true
"parameters": [], # 치환 파라미터 (전체 수신자에게 적용), ‘치환 ID’ 를 key로,
# ‘치환 ID에 맵핑되는 값’ 을 value로 가지는 Map 형태의 Object
"recipients": [],
"referencesHeader": "", # 특정 메일을 모아서 보기 위해 네이버 메일에서 지원하는 기능,
# 해당 필드에 동일한 값을 입력한 메일들을 모아서 볼 수 있다.
# (다음의 형태가 되어야 함 : <unique_id@domain.com> )
"reservationUtc": "", # 예약 발송 일시 ( 1970년 1월 1일 00:00:00 협정 세계시(UTC)
# 부터의 경과 시간을 1/1000초로 환산한 정수 ),
# reservationDateTime 값보다 이 값이 우선 적용된다.
"reservationDateTime": "", # 다음과 같은 형태의 예약 발송 일시 (yyyy-MM-dd HH:mm UTC+09:00),
# reservationUtc 값이 우선한다.
"sender_address": MailSender.sender_email,
"sender_name": MailSender.sender_name,
"temprequest_id": "", # 첨부파일 등록 시 사용한 임시 요청 ID
"templateSid": 337, # 템플릿 ID
"title": ""
}
print(post_param)
print('=' * 30)
jsonData = json.dumps(post_param)
print(jsonData)
timestamp = MailSender.get_timestamp()
signature = MailSender.make_signature(NCloud.request_url, timestamp)
print(signature)
url = NCloud.host_name_url + NCloud.request_url
print(url)
headers = {
"X-NCP-APIGW-TIMESTAMP": timestamp,
"X-NCP-IAM-ACCESS-KEY": NCloud.access_key,
"X-NCP-APIGW-SIGNATURE-V2": signature.decode("utf-8"),
"Content-type": "application/json"
}
print('headers : ' + json.dumps(headers, indent='\t'))
# exit(0)
res = requests.post(url, headers=headers, data=jsonData)
print('sending mail result : {0}'.format(res.status_code))
print('result text : \n{0}'.format(res.text))
''' '''
@staticmethod @staticmethod
def curl(post_param, timestamp, signature): def curl(post_param, timestamp, signature):
+5 -4
View File
@@ -3,7 +3,7 @@ from mail_sender import NCloud
from mail_sender import MailSender from mail_sender import MailSender
def send_mail_waiting_7days_maestro(name, email, register_date): def send_mail_for_test(name, email, register_date):
mail_sender.make_default_post_param() mail_sender.make_default_post_param()
mail_sender.set_post_param('templateSid', 337) mail_sender.set_post_param('templateSid', 337)
mail_sender.set_post_param_recipients(name, email) mail_sender.set_post_param_recipients(name, email)
@@ -14,11 +14,12 @@ def send_mail_waiting_7days_maestro(name, email, register_date):
# print('hmac : ' + MailSender.hash_hmac('ripemd160', 'The quick brown fox jumped over the lazy dog.', 'secret')) # print('hmac : ' + MailSender.hash_hmac('ripemd160', 'The quick brown fox jumped over the lazy dog.', 'secret'))
signature = MailSender.make_signature(NCloud.request_url, timestamp) signature = MailSender.make_signature(NCloud.request_url, timestamp)
# print('signature : ' + signature.decode('utf-8')) # print('signature : ' + signature.decode('utf-8'))
mail_sender.send_email(timestamp, signature) # print('signature : ' + signature)
# mail_sender.send_email(timestamp, signature)
MailSender.send_temp_mail()
mail_sender = MailSender() mail_sender = MailSender()
send_mail_waiting_7days_maestro('test1', 'jisangs@daum.net', '2019-04-08') send_mail_for_test('test1', 'jisangs@daum.net', '2019-04-08')
''' '''