Tuesday, April 16, 2019

Sending email by using Python

#This is the code for sending email using Gmail and Python

# 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()

Allow less secure apps
Turn on 'Allow less secure apps' to get access to the smtp server





No comments:

Post a Comment

Applying SMA10/20, SMA20/50 as trading signals

This is the comparison for results before and after applying SMA10/20 and SMA20/50 in the stock trader. Background Trading 3 stock ma...