本文介绍了使用Mailgun Python API发送HTML电子邮件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用Mailgun python API很好地发送文本电子邮件:
I can send text email with the Mailgun python API fine:
def send_simple_message(mailtext, filename=""):
requests.post("https://api.mailgun.net/v3/mydomain.in/messages",
auth=("api", "key-1234"),
files=[("attachment", open(filename))],
data={"from": "Credit Card Manager <[email protected]>",
"to": ["[email protected]"],
"subject": "Summary of Credit Card payments due",
"text": mailtext})
上面的代码可以正常工作,并且消息已传递.
The above code works fine, and message is delivered.
但是,当我尝试使用html字符串进行以下操作时,
However when I try the following, using an html string,
def send_simple_message(mailtext, mailhtml, filename=""):
requests.post("https://api.mailgun.net/v3/mydomain.in/messages",
auth=("api", "key-61a652b57"),
files=[("attachment", open(filename))],
data={"from": "Credit Card Manager <[email protected]>",
"to": ["[email protected]"],
"subject": "Summary of Credit Card payments due",
"text": mailtext,
"html": mailhtml})
其中,mailhtml是以下字符串:
where, mailhtml is the following string:
<html><body><h4>CREDIT CARD STATEMENT FOR MAY</h4><table><tr><th>Card</th><th>Dues</th><th>Date</th></tr><tr><td>Standard Chartered</td><td>3,755.29</td><td>16/05/2018</td></tr><tr><td>HDFC</td><td>41,616.90</td><td>04/05/2018</td></tr><tr><td>ICICI</td><td>5,833.74</td><td>11/05/2018</td></tr><tr><td>SBI</td><td>20,667.00</td><td>01/05/2018</td></tr></table></body></html>
我收到以下错误:
Traceback (most recent call last):
File "getcreditcards.py", line 611, in <module>
send_simple_message(txtbody, htmlbody, os.getcwd() + '/' + excelfilename)
File "getcreditcards.py", line 484, in send_simple_message
"html": mailhtml
File "/usr/lib/python3/dist-packages/requests/api.py", line 112, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 488, in request
prep = self.prepare_request(req)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 431, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/usr/lib/python3/dist-packages/requests/models.py", line 308, in prepare
self.prepare_body(data, files, json)
File "/usr/lib/python3/dist-packages/requests/models.py", line 496, in prepare_body
(body, content_type) = self._encode_files(files, data)
File "/usr/lib/python3/dist-packages/requests/models.py", line 159, in _encode_files
fdata = fp.read()
File "/usr/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte
这里似乎是什么问题?
推荐答案
您尝试在脚本开始处编写
you tryed to write on the start of the script
#coding: UTF-8
??
这篇关于使用Mailgun Python API发送HTML电子邮件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!