Error sending html email with mailgun python API

2024/10/13 15:22:00

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.

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

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": mailhtmlFile "/usr/lib/python3/dist-packages/requests/api.py", line 112, in postreturn request('post', url, data=data, json=json, **kwargs)File "/usr/lib/python3/dist-packages/requests/api.py", line 58, in requestreturn session.request(method=method, url=url, **kwargs)File "/usr/lib/python3/dist-packages/requests/sessions.py", line 488, in requestprep = self.prepare_request(req)File "/usr/lib/python3/dist-packages/requests/sessions.py", line 431, in prepare_requesthooks=merge_hooks(request.hooks, self.hooks),File "/usr/lib/python3/dist-packages/requests/models.py", line 308, in prepareself.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_filesfdata = 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?

Answer

you tryed to write on the start of the script

 #coding: UTF-8 

??

https://en.xdnf.cn/q/118063.html

Related Q&A

How to take HTML user input and query it via Python SQL?

Is there a way to take user input from HTML, and use python to run the input through to a SQL database? Does the input need to be parsed? I want the the user to be able to type in a store name, and f…

Reading and taking specific file contents in a list in python

I have a file containing:name: Sam placing: 2 quote: Ill win.name: Jamie placing: 1 quote: Be the best.and I want to read the file through python and append specific contents into a list. I want my fir…

Scipy / ctypes build-config: How to load lib?

These docs have a nice example on how to compile some external C/C++ code and load this using ctypes. This works great with manual compilation (where im controlling the names of my library which later …

Webfaction Django 1.4.1: easy_thumbnails 3.0b – Couldnt get the thumbnail error

I use easy_thumbnails and it works fine on a development machine but in production I get errors like shown below, when I use {% thumbnail photo.image 300x170 %} templatetag. Though can directly browse …

acronym replacement with its value using python

i have dictionary like that i need to replace acronyms in text with its value in dictionary i use this code but it doesnt give me the appropriate result when i test the function using acronyms("we…

Grako - How to do error handling?

How do I do error handling with Grako?EBNF (MyGrammar.ebnf):pattern = { tag | function }* ; tag = tag:( "%" name:id "%" ); function = function:("$" name:id "…

How get the softlayer storage credendials?

Im trying to get the Username,password and host IQN of a authorized Softlayer Network Storage. I used this python script, but the shell returns []import SoftLayerAPI_USERNAME = xxxAPI_KEY = yyyystorage…

dopy.manager.DoError: Unable to authenticate you

Im trying to configure a Virtual Machine(with Vagrant and Ansible), that needs a file.py to the full correct configuration of this machine (according to the book that Im studying),Im was using the Digi…

subprocess.Popen: OSError: [Errno 2] No such file or directory only on Linux

This is not a duplicate of subprocess.Popen: OSError: [Errno 13] Permission denied only on Linux as that problem occurred due to wrong permissions. That has been fixed and this is an entirely different…

Windows Theano Keras - lazylinker_ext\mod.cpp: No such file or directory

I am installing Theano and Keras follwing the How do I install Keras and Theano in Anaconda Python on Windows?, which worked fine for me with an older release before. Now I have upgraded to the latest…