Skip to content

Instantly share code, notes, and snippets.

@conquistadorjd
Created June 8, 2018 11:31
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 conquistadorjd/2200247e5b61351800f360e0cdf461df to your computer and use it in GitHub Desktop.
Save conquistadorjd/2200247e5b61351800f360e0cdf461df to your computer and use it in GitHub Desktop.
How to send an email with Python3.6
###############################################################################################################
# program to send an email without subject or attachment
###############################################################################################################
import smtplib
print('*** Start of the Program ***')
sender = "sender@gmail.com"
sender_password = "sender_password"
receivers = 'receiver@gmail.com'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender,sender_password )
message_body = "This is test email"
server.sendmail(sender, receivers, message_body)
print('email sent')
server.quit()
###############################################################################################################
# program to send an email with attachment
###############################################################################################################
import smtplib
print('*** Start of the Program ***')
sender = "sender@gmail.com"
sender_password = "sender_password"
receivers = 'receiver@gmail.com'
message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender,sender_password )
server.sendmail(sender, receivers, message)
print("Successfully sent email")
###############################################################################################################
# program to send an email with attachment
###############################################################################################################
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
print('*** Start of the program ***')
sender = "sender@gmail.com"
sender_password = "sender_password"
receivers = 'receiver@gmail.com'
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receivers
msg['Subject'] = "Subject of the Mail- image -2"
body = "Body_of_the_mail"
msg.attach(MIMEText(body, 'plain'))
# path along with extension of file to be attachmented
filename = '/home/conquistador/code/github/python-01-utilities/image/output/14991-the-waste-of-capital-in-proportion-to-the-total-capital-in.jpg'
attachmentment = open(filename, "rb")
# instance of MIMEBase and named as p
attachment = MIMEBase('application', 'octet-stream')
# To change the payload into encoded form
attachment.set_payload((attachmentment).read())
# encode into base64
encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', "attachmentment; filename= %s" % filename)
# attachment the instance to instance 'msg'
msg.attach(attachment)
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(sender, sender_password)
text = msg.as_string()
s.sendmail(sender, receivers, text)
print('*** email sent ***')
s.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment