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)
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()
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With