1

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.
>>> 
asked Sep 30, 2018 at 15:22

1 Answer 1

2

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()
answered Sep 30, 2018 at 15:29
Sign up to request clarification or add additional context in comments.

4 Comments

That looks normal. It's the "greeting" from the server showing that it's ready to accept messages. I think the reason that's shown is the debug level - making the script print interactions with the server.
ive edited the question to include the full error msg I was getting.
Right, need to turn on encryption before trying to log in. I updated the answer.
Thanks your the best.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.