Skip to content

Instantly share code, notes, and snippets.

@bot11
Created April 3, 2015 13:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bot11/7dd3777c942bcc4911aa to your computer and use it in GitHub Desktop.
Save bot11/7dd3777c942bcc4911aa to your computer and use it in GitHub Desktop.
send mail using gmail.
import smtplib
gmail_user = "from@gmail.com"
gmail_pwd = "frompwd"
TO = 'to@someother.com'
SUBJECT = "Testing sending using gmail"
TEXT = "Testing sending mail using gmail servers"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
BODY = '\r\n'.join(['To: %s' % TO,
'From: %s' % gmail_user,
'Subject: %s' % SUBJECT,
'', TEXT])
server.sendmail(gmail_user, [TO], BODY)
print ('email sent')
#If got the following error:
'''
Traceback (most recent call last):
File "send_from_gmail.py", line 13, in <module>
server.login(gmail_user, gmail_pwd)
File "/usr/lib/python2.7/smtplib.py", line 615, in login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/...........................................................................>
Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=XXXX XXXXXXX.2 - gsmtp')
Access : https://www.google.com/settings/security/lesssecureapps and set
Access for less secure apps : turn off
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment