使用mailgun python API发送html电子邮件时出错

时间:2018-05-01 13:02:36

标签: python python-requests mailgun

我可以使用Mailgun python API发送短信:

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})             

以上代码工作正常,并传递消息。

但是,当我尝试使用html字符串时,

<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>             

其中,mailhtml是以下字符串:

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

我收到以下错误:

int main(int argc, char** argv)
{
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    const char* script = "io.write(\"From Lua\")"; // or read the string from a user, file, etc.
    if(luaL_loadstring(L, script) || lua_pcall(L, 0, 0, 0))
    {
        //error occured
        const char* s = luaL_checkstring(L, -1);
        fprintf(stderr, "Error in script: %s", s);
        exit(EXIT_FAILURE);
    }
    lua_close(L);
    exit(EXIT_SUCCESS);
}

这里似乎有什么问题?

1 个答案:

答案 0 :(得分:0)

你试着写剧本的开头

 #coding: UTF-8 

...