Sending HTML-Embedded Emails With Python

<html>
<body>
<p>
Hi!<br>
Here is the <a href="google.com">link</a> you wanted.
</p>
</body>
</html>
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
message = MIMEMultipart('Alternative')
message['From'] = 'from@gmail.com'
message['Subject'] = ''
html = open('Message.html').read()
message.attach(MIMEText(html, 'html'))
server = stmplib.SMTP('smtp.gmail.com', 587)
server.startls()
server.login('Username', 'Password')
server.sendmail('from@gmail.com', 'to@gmail.com', message.as_string())

Just The Code:

import smtp
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
message = MIMEMultipart('Alternative')
message['To'] = 'to@gmail.com'
message['From'] = 'from@gmail.com'
message['Subject'] = ''
html = open('Message.html').read()
message.attach(MIMEText(html, 'html'))
server = stmplib.SMTP('smtp.gmail.com', 587)
server.startls()
server.login('Username', 'Password')
server.sendmail('from@gmail.com', 'to@gmail.com', message.as_string())
<!-- The contents of the email are stored in this file -->
<html>
<body>
<p>
Hi!<br>
Here is the <a href="google.com">link</a> you wanted.
</p>
</body>
</html>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store