Created
February 6, 2017 15:02
-
-
Save felipessalvatore/0547b7e1e78228df98861df1e6118514 to your computer and use it in GitHub Desktop.
Sending an email with python http://naelshiab.com/tutorial-send-email-python/
This file contains hidden or 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
import smtplib | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEText import MIMEText | |
from email.MIMEBase import MIMEBase | |
from email import encoders | |
fromaddr = "YOUR EMAIL" | |
toaddr = "EMAIL ADDRESS YOU SEND TO" | |
msg = MIMEMultipart() | |
msg['From'] = fromaddr | |
msg['To'] = toaddr | |
msg['Subject'] = "SUBJECT OF THE EMAIL" | |
body = "TEXT YOU WANT TO SEND" | |
msg.attach(MIMEText(body, 'plain')) | |
filename = "NAME OF THE FILE WITH ITS EXTENSION" | |
attachment = open("PATH OF THE FILE", "rb") | |
part = MIMEBase('application', 'octet-stream') | |
part.set_payload((attachment).read()) | |
encoders.encode_base64(part) | |
part.add_header('Content-Disposition', "attachment; filename= %s" % filename) | |
msg.attach(part) | |
server = smtplib.SMTP('smtp.gmail.com', 587) | |
server.starttls() | |
server.login(fromaddr, "YOUR PASSWORD") | |
text = msg.as_string() | |
server.sendmail(fromaddr, toaddr, text) | |
server.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment