Sending HTML email using Python

ID : 10272

viewed : 55

Tags : pythonemailhtml-emailpython

Top 5 Answer for Sending HTML email using Python

vote vote

91

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() 
vote vote

87

You might try using my mailer module.

from mailer import Mailer from mailer import Message  message = Message(From="me@example.com",                   To="you@example.com") message.Subject = "An HTML Email" message.Html = """<p>Hi!<br>    How are you?<br>    Here is the <a href="http://www.python.org">link</a> you wanted.</p>"""  sender = Mailer('smtp.example.com') sender.send(message) 
vote vote

78

Here is a Gmail implementation of the accepted answer:

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. mail = smtplib.SMTP('smtp.gmail.com', 587)  mail.ehlo()  mail.starttls()  mail.login('userName', 'password') mail.sendmail(me, you, msg.as_string()) mail.quit() 
vote vote

60

Here is a simple way to send an HTML email, just by specifying the Content-Type header as 'text/html':

import email.message import smtplib  msg = email.message.Message() msg['Subject'] = 'foo' msg['From'] = 'sender@test.com' msg['To'] = 'recipient@test.com' msg.add_header('Content-Type','text/html') msg.set_payload('Body of <b>message</b>')  # Send the message via local SMTP server. s = smtplib.SMTP('localhost') s.starttls() s.login(email_login,         email_passwd) s.sendmail(msg['From'], [msg['To']], msg.as_string()) s.quit() 
vote vote

51

for python3, improve @taltman 's answer:

  • use email.message.EmailMessage instead of email.message.Message to construct email.
  • use email.set_content func, assign subtype='html' argument. instead of low level func set_payload and add header manually.
  • use SMTP.send_message func instead of SMTP.sendmail func to send email.
  • use with block to auto close connection.
from email.message import EmailMessage from smtplib import SMTP  # construct email email = EmailMessage() email['Subject'] = 'foo' email['From'] = 'sender@test.com' email['To'] = 'recipient@test.com' email.set_content('<font color="red">red color text</font>', subtype='html')  # Send the message via local SMTP server. with smtplib.SMTP('localhost') as s:     s.login('foo_user', 'bar_password')     s.send_message(email) 

Top 3 video Explaining Sending HTML email using Python

Related QUESTION?