Skip to content

Instantly share code, notes, and snippets.

@bot11
Created April 3, 2015 13:38

Revisions

  1. bot11 created this gist Apr 3, 2015.
    34 changes: 34 additions & 0 deletions send_from_gmail.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    import smtplib

    gmail_user = "[email protected]"
    gmail_pwd = "frompwd"


    TO = '[email protected]'
    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
    '''