From 988815ef9035a19aa589c90a6f3d3b3564ba41ea Mon Sep 17 00:00:00 2001 From: Dearc Date: Fri, 24 Feb 2017 21:23:47 +0800 Subject: [PATCH] fixed problem when send chinese words by http method --- .idea/workspace.xml | 128 +++++++++++++++------------------------- app/models/test.db | Bin 113664 -> 113664 bytes app/tools/http_email.py | 20 +++++-- 3 files changed, 63 insertions(+), 85 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 19f6d4b..35fb90c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,14 +2,9 @@ - - - - - @@ -24,9 +19,9 @@ - + - + @@ -156,7 +142,6 @@ @@ -217,8 +203,8 @@ DEFINITION_ORDER - @@ -303,24 +289,6 @@ - + - - + + @@ -703,7 +671,7 @@ - + @@ -957,14 +925,6 @@ - - - - - - - - @@ -1080,10 +1040,18 @@ + + + + + + + + - + @@ -1096,6 +1064,22 @@ + + + + + + + + + + + + + + + + @@ -1106,34 +1090,18 @@ - - + + - - - - - - - - - - - - - - - - - - + + diff --git a/app/models/test.db b/app/models/test.db index 8d728897c763dd3aee09d6642c40a41656180229..f74ec07b51e110414e73d8572a74b6ac60f4f920 100644 GIT binary patch delta 499 zcmZqp!PfADZGtqT+e8^>RyPJcR;P_AjoM5Wypx}6n=y)QR^<4>C@8?fsL04?8mw*j zblt)i>lX5DR@a-(Ir)P)3lksT<|F1EOu8D(p^TO(sYb>IsfM~~iIxVshK8wTx)!F% zX1c}}DW)doNv7r|DT#a_g_9@TQQ17#Dld@HaxzcPeMZyGZ+b+WC)KdAq%$z51MQ4u zu5T3M<6#IDR@BsWbmZlbCdTaKigJV-Z3^t3?rVM8Hq%DGz>dqbSO?;Cou}KEKG{3R z$iUEC*T6{E$V9=&(8|ypi1dt&EDQ_`pfXZeq+Eg=-9lVlHVc-puxw_z@rRv@mw|zS ik$GSoZD3(#YNlslY=Y@%WGR@V86PmqY-MKryBh!$sg?Br delta 109 zcmV-z0FwWJ_y&OZ29O&8Jdqqj1v~&P@wl;Mfhz(40F&n{HUS^AAqWWqvnMW>36W3+ zlkgV=v&c7u0+Em@1SkVl0h5s2B(t45Zcza>lMIaC0Xehmj1)t&AdLhFvjo`u2eSd> P0vxj-bO8jl0|EWJ4JRg^ diff --git a/app/tools/http_email.py b/app/tools/http_email.py index 01caf44..aff9083 100644 --- a/app/tools/http_email.py +++ b/app/tools/http_email.py @@ -2,7 +2,7 @@ from __future__ import print_function try: - from security import MailConfig + from app.security import MailConfig except ImportError: print("failed load security file, you'll not be able to use some modules") @@ -36,17 +36,22 @@ def unique_id(): return uuid.uuid4().__str__() -def single_mail_api(recipients="", subject="", text="", html="", method="GET"): +def single_mail_api(recipients="", subject="", text=None, html="", method="GET"): # https://help.aliyun.com/document_detail/29442.html?spm=5176.doc29441.6.567.kBMXeE # paras must be encode with utf-8 # base URL base_http_url = " http://dm.aliyuncs.com/" base_https_url = " https://dm.aliyuncs.com/" - # 邮件发送接口参数 - # ToAddress目标地址,多个Email地址可以逗号分隔 + if isinstance(recipients, list): + # ToAddress目标地址,多个Email地址可以逗号分隔 recipients = ",".join(recipients) - + if isinstance(html, unicode): + # 中文必须转换成utf-8,否则出错 + html = html.encode("utf-8") + if isinstance(subject, unicode): + subject = subject.encode("utf-8") + # 邮件发送接口参数 private_params = dict(Action="SingleSendMail", AccountName=MailConfig.MAIL_USERNAME, ReplyToAddress="true", AddressType="1", ToAddress=recipients, Subject=subject, HtmlBody=html) # 公共方法参数 @@ -97,3 +102,8 @@ def single_mail_api(recipients="", subject="", text="", html="", method="GET"): raise HttpMailSendException(r.text) else: raise HttpMethodException("HTTP Method Unsupported.") + +if __name__ == "__main__": + single_mail_api(recipients=u"datingwithme@live.cn", + subject=u"Python发送邮件", + html=u"Python通过HTTP方式发送邮件") \ No newline at end of file