# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
textfile = 'content.txt'
fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
print msg
fp.close()
me = 'youraccount@gmail.com'
to = 'recipient@gmail.com'
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = to
# Send the message via our own SMTP server, but don't include the
# envelope header.
#You need to turn on 'Allow less secure apps' to get access to the smtp server
#https://myaccount.google.com/lesssecureapps?pli=1
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
gmail_user = 'youraccount@gmail.com'
gmail_password = 'password'
server.login(gmail_user, gmail_password)
server.sendmail(me, to, msg.as_string())
server.quit()
Turn on 'Allow less secure apps' to get access to the smtp server |
No comments:
Post a Comment