Send HTML Emails With Python

Send HTML emails with Python

From Python v2.7.14 documentation - 18.1.11. email: Examples:

Here’s an example of how to create an HTML message with an alternative plain text version:

#! /usr/bin/python

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "my@email.com"
you = "your@email.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()

How to pass variable to html emails sent from Python

Just replace your placeholders with the variables:

html_message = html_message.replace('{{user_name}}',user_name).replace('{{user_age}}',user_age)

Rendering HTML in Python when sending e-mails using import smtplib

You can use the MIMEText object from email.mime.text to create an email that specifies it's content as HTML.

from email.mime.text import MIMEText

message = '<html><body> <b>hello world</b> </body></html>'

my_email = MIMEText(message, "html")
my_email["From"] = "me@email.com"
my_email["To"] = "you@other.org"
my_email["Subject"] = "Hello!"

server = smtplib.SMTP(my_server)
server.sendmail(from_email, to_email, my_email.as_string())

This handles the formatting of the email header for you. .as_string() produces:

Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
From: me@email.com
To: you@other.org
Subject: Hello!

<html><body> <b>hello world</b> </body></html>

How to send HTML emails with images using python3 and email library?

It looks like you are embedding the image using Content-ID (CID), which has limited email support (https://www.caniemail.com/features/image-base64/). Can you use the HTML img tag i.e. <img src="https://www.image.com/host-somewhere-on-internet" />?

See https://sendgrid.com/blog/embedding-images-emails-facts/ for more information.

HTML email body with embedded local image

You need to set the PR_ATTACH_CONTENT_ID MAPI property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x3712001F") using Attachment.PropertyAccessor.SetProperty and refer that attachment through the src attribute that matches the value of PR_ATTACH_CONTENT_ID set on the attachment:

attachment = MailItem.Attachments.Add("c:\temp\MyPicture.jpg")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1")
MailItem.HTMLBody = "<html><body>Test image <img src=""cid:MyId1""></body></html>"


Related Topics



Leave a reply



Submit