excelguy
excelguy

Reputation: 1624

Python, Send email through Gmail

I am trying to use python to send an email through gmail.

So far I have made my gmail account to allow less secure apps. Still getting errors though.

import smtplib
from email.message import EmailMessage
def send_mail(to_email, subject, message,
              server=('smtp.gmail.com', 587),
              from_email='[email protected]'):
    # import smtplib
    msg = EmailMessage()
    msg['Subject'] = subject
    msg['From'] = from_email
    msg['To'] = ', '.join(to_email)
    msg.set_content(message)
    print(msg)
    server = smtplib.SMTP(server)
    server.set_debuglevel(1)
    server.login(from_email, 'Password')  # user & password
    server.send_message(msg)
    server.quit()
    print('successfully sent the mail.')


send_mail(to_email=['[email protected]', '[email protected]'],
          subject='hello', message='Please Work')

error msg:

Traceback (most recent call last):
  File "C:/Users/Louis/AppData/Local/Programs/Python/Python36/emailtry.py", line 22, in <module>
    subject='hello', message='Please Work')
  File "C:/Users/Louis/AppData/Local/Programs/Python/Python36/emailtry.py", line 13, in send_mail
    server = smtplib.SMTP(server)
  File "C:\Users\Louis\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Users\Louis\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 324, in connect
    if not port and (host.find(':') == host.rfind(':')):
AttributeError: 'tuple' object has no attribute 'find'

Can anyone help with what I am missing?

EDIT

With the added server change, I am now getting

send: 'ehlo Louis-PC.hitronhub.home\r\n'
reply: b'250-smtp.gmail.com at your service, [2607:fea8:5b9f:f9c5:6d68:a665:373b:b92a]\r\n'
reply: b'250-SIZE 35882577\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-STARTTLS\r\n'
reply: b'250-ENHANCEDSTATUSCODES\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-CHUNKING\r\n'
reply: b'250 SMTPUTF8\r\n'
reply: retcode (250); Msg: b'smtp.gmail.com at your service, [2607:fea8:5b9f:f9c5:6d68:a665:373b:b92a]\nSIZE 35882577\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8'

And

Traceback (most recent call last):
  File "C:/Users/Louis/AppData/Local/Programs/Python/Python36/emailtry.py", line 22, in <module>
    subject='hello', message='Please Work')
  File "C:/Users/Louis/AppData/Local/Programs/Python/Python36/emailtry.py", line 15, in send_mail
    server.login(from_email, 'Password')  # user & password
  File "C:\Users\Louis\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 697, in login
    "SMTP AUTH extension not supported by server.")
smtplib.SMTPNotSupportedError: SMTP AUTH extension not supported by server.
>>> 

Upvotes: 1

Views: 322

Answers (1)

legoscia
legoscia

Reputation: 41578

The smtplib.SMTP constructor expects server host and port as separate arguments, not as a tuple. Change the call to:

    server = smtplib.SMTP(server[0], server[1])

Google requires the client to turn on encryption before authenticating. That's the reason for the confusing error message: the AUTH extension is in fact not supported while in unencrypted state. Before the server.login call, add:

server.starttls()

Upvotes: 2

Related Questions