Error sending html email with mailgun python API

元气小坏坏 提交于 2019-12-24 21:26:31

问题


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 <creditcards@mydomain.in>",
  "to": ["bob@mydomain.in"],
  "subject": "Summary of Credit Card payments due",
  "text": mailtext})

The above code works fine, and message is delivered.

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 <creditcards@mydomain.in>",
  "to": ["bob@mydomain.in"],
  "subject": "Summary of Credit Card payments due",
  "text": mailtext,
  "html": 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>             

I get the following error:

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

What seems to be the problem here?


回答1:


you tryed to write on the start of the script

 #coding: UTF-8 

??



来源:https://stackoverflow.com/questions/50116967/error-sending-html-email-with-mailgun-python-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!