Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 | Send email -- SMTP -- gmail -- Error : SMTPException

I want to send an email by use of Python 3. I cannot yet make sense of the examples that I have seen. Here is one reference: Using Python to Send Email

I have pulled the first simple example found on the above reference. I find this example a good representation of the combination of examples I have seen on the internet. It seems to be the basic form of doing what I am trying to do.

When I try the code below, I receive Error:

File "C:\Python33\Lib\email.py", line 595, in login
    raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.

Here is the code:

# Send Mail

import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)

# Log in to the server
server.login("[email protected]","myPassword")

# Send mail
msg = "\nHello!"
server.sendmail("[email protected]","[email protected]", msg)
like image 622
Blue SeventyTwo Avatar asked Jun 26 '13 23:06

Blue SeventyTwo


3 Answers

I have found a solution on YouTube.

Here is the video link.

# smtplib module send mail

import smtplib

TO = '[email protected]'
SUBJECT = 'TEST MAIL'
TEXT = 'Here is a message from python.'

# Gmail Sign In
gmail_sender = '[email protected]'
gmail_passwd = 'password'

server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_sender, gmail_passwd)

BODY = '\r\n'.join(['To: %s' % TO,
                    'From: %s' % gmail_sender,
                    'Subject: %s' % SUBJECT,
                    '', TEXT])

try:
    server.sendmail(gmail_sender, [TO], BODY)
    print ('email sent')
except:
    print ('error sending mail')

server.quit()
like image 57
Blue SeventyTwo Avatar answered Oct 04 '22 19:10

Blue SeventyTwo


As of mid-October 2017, gmail isn't accepting connections via smtplib.SMTP() on port 587, but requires smtplib.SMTP_SSL() and port 465. This starts TLS immediately, and ehlo isn't needed. Try this snippet instead:

# Gmail Sign In
gmail_sender = '[email protected]'
gmail_passwd = 'password'

server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(gmail_sender, gmail_passwd)

# build and send the email body.
like image 39
RA Montante Avatar answered Oct 04 '22 18:10

RA Montante


This is how I sent an email using Google. Capital letters represent personal information that needs to be edited

try:
    import RUNNING_SCRIPT
except:
    print("threw exception")
    # smtplib module send mail

    import smtplib

    TO = ‘[email protected]'
    SUBJECT = 'SERVER DOWN'
    TEXT = 'Here is a message from python. Your server is down, please check.'

    # Gmail Sign In
    gmail_sender = ‘[email protected]'
    gmail_passwd = ‘APPLICATION SPECIFIC PASSWORD’

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login(gmail_sender, gmail_passwd)

    BODY = '\r\n'.join(['To: %s' % TO, 'From: %s' % gmail_sender,'Subject: %s' % SUBJECT,'', TEXT])

    try:
        server.sendmail(gmail_sender, [TO], BODY)
        print ('email sent')
    except:
        print ('error sending mail')
        server.quit()
like image 39
Brian Babiak Avatar answered Oct 04 '22 19:10

Brian Babiak