Created
June 8, 2018 11:31
-
-
Save conquistadorjd/2200247e5b61351800f360e0cdf461df to your computer and use it in GitHub Desktop.
How to send an email with Python3.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################################################### | |
# program to send an email without subject or attachment | |
############################################################################################################### | |
import smtplib | |
print('*** Start of the Program ***') | |
sender = "[email protected]" | |
sender_password = "sender_password" | |
receivers = '[email protected]' | |
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################################################### | |
# program to send an email with attachment | |
############################################################################################################### | |
import smtplib | |
print('*** Start of the Program ***') | |
sender = "[email protected]" | |
sender_password = "sender_password" | |
receivers = '[email protected]' | |
message = """From: From Person <[email protected]> | |
To: To Person <[email protected]> | |
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################################################### | |
# 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 = "[email protected]" | |
sender_password = "sender_password" | |
receivers = '[email protected]' | |
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