Mailgun Inline Images with Python

Mailgun’s API is really nice and super-simple to use. However, I was having issues getting it to send inline images in my emails. I think the example in the documentation (here) is wrong. My files were coming through as attachement with a filename of “inline” rather than the actual filename. The actual filename was correctly coming through in the img cid tag but because the attachments had the wrong filename, they weren’t being shown.

My solution, which seems to be working, is to use MultiDict (which can be imported from werkzeug if you’re using Flask) and create the files part of the API call like this where the filename and file object are contained in a tuple:

files=MultiDict([("inline", ("test1.jpg", open("files/test1.jpg"))),
("inline", ("test2.jpg", open("files/test2.jpg")))])

Seems to work!

ECMWF Python urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

Just posting this for future Googlers:

I was getting this error trying to access ECMWF’s data through their web API. On Mac, I couldn’t make the suggested solution work.

I thought I’d re-install Python to see if that helped but noticed during the install the following text:

Certificate verification and OpenSSL

**NEW** This variant of Python 3.6 now includes its own private copy of OpenSSL 1.0.2. Unlike previous releases, the deprecated Apple-supplied OpenSSL libraries are no longer used. This also means that the trust certificates in system and user keychains managed by the Keychain Access application and the security command line utility are no longer used as defaults by the Python ssl module. For 3.6.0, a sample command script is included in /Applications/Python 3.6 to install a curated bundle of default root certificates from the third-party certifi package (https://pypi.python.org/pypi/certifi). If you choose to use certifi, you should consider subscribing to the project’s email update service to be notified when the certificate bundle is updated.

The bundled pip included with the Python 3.6 installer has its own default certificate store for verifying download connections.

Clearly Python does something different around certificates now. So I ran the “Install Certificates.command” found in /Applications/Python 3.6 and suddenly everything started working fine.