I’m trying to figure out how to send HTML emails with inline images using Python and the Mailgun API. My current setup uses Flask, Jinja2, and the requests library. Here’s what I’ve got so far:
The email sends fine, but the images don’t show up. In my HTML template, I’m using <img src="cid:logo.png"> to reference the images. Am I missing something? Should I be handling the image files differently? Any help would be appreciated!
I’ve found that using the ‘inline’ parameter in Mailgun can be tricky. Instead, try attaching the images and referencing them with Content-ID headers. Here’s a modified version of your code that should work:
In your HTML template, reference the images like this: <img src=\"cid:logo.png\">. This approach has worked well for me in the past. Let me know if you need any clarification.
As someone who’s worked extensively with email APIs, I can tell you that handling inline images can be a bit tricky. One approach that’s worked well for me is using the requests-toolbelt library. It simplifies the process of creating multipart encoded requests, which is exactly what you need for inline images.
Here’s a snippet that might help:
from requests_toolbelt import MultipartEncoder
def send_email(recipient, sender, subject, text, html_content, image_files):
data = MultipartEncoder(
fields={
'from': sender,
'to': recipient,
'subject': subject,
'text': text,
'html': html_content,
'inline': [(f.name, f.read(), 'image/png') for f in image_files]
}
)
response = requests.post(
f'https://api.mailgun.net/v3/{config.MAILGUN_DOMAIN}/messages',
auth=('api', config.MAILGUN_KEY),
data=data,
headers={'Content-Type': data.content_type}
)
return response
This approach has consistently worked for me across different email clients. Give it a shot and let me know if you run into any issues.