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
How to Iterate Through Two Lists in Parallel
Why Is Using 'Eval' a Bad Practice
Flatten an Irregular List of Lists
Webdriverwait Not Working as Expected
How to Iterate Over Rows in a Dataframe in Pandas
How to Convert String Representation of List to a List
How to Filter Pandas Dataframe Using 'In' and 'Not In' Like in Sql
Pandas Conditional Creation of a Series/Dataframe Column
How to Detect Collision in Pygame
What Is the 'Self' Parameter in Class Methods
Does Python Have a Ternary Conditional Operator
"Is" Operator Behaves Unexpectedly With Integers
How to Merge Two Dictionaries in a Single Expression
Unboundlocalerror on Local Variable When Reassigned After First Use
How to Avoid Having Class Data Shared Among Instances
Get the Cartesian Product of a Series of Lists
Converting String "Jun 1 2005 1:33Pm" into Datetime
Strange Result When Removing Item from a List While Iterating Over It