Skip to content

Instantly share code, notes, and snippets.

@LuisAlejandro
Created April 15, 2020 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuisAlejandro/e677125db5eed587f92385e10024026c to your computer and use it in GitHub Desktop.
Save LuisAlejandro/e677125db5eed587f92385e10024026c to your computer and use it in GitHub Desktop.
This is a python script to send a multipart email message
# This is a python script to send a multipart email message
# Replace these variables with its values:
# from_address = ""
# to_address = ""
# server_url = "" <--------------- You can use smtp.gmail.com
# server_port = ""
# password = ""
#
# You should write your message in the text variable and the html variable (for an html version of the message)
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from_address = ""
to_address = ""
server_url = ""
server_port = ""
password = ""
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = from_address
msg['To'] = to_address
# Create the body of the message (a plain-text and an HTML version).
text = """
"""
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(server_url, server_port)
s.starttls()
s.login(from_address, password)
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(from_address, to_address, msg.as_string())
s.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment