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 simp
Solution 1:
I have found a solution on YouTube.
Here is the video link.
# smtplib module send mailimport smtplib
TO = 'recipient@mailservice.com'
SUBJECT = 'TEST MAIL'
TEXT = 'Here is a message from python.'# Gmail Sign In
gmail_sender = 'sender@gmail.com'
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()
Solution 2:
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 = 'sender@gmail.com'
gmail_passwd = 'password'
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(gmail_sender, gmail_passwd)
# build and send the email body.
Solution 3:
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 = ‘EXAMPLE_RECIPIENT@gmail.com'
SUBJECT = 'SERVER DOWN'
TEXT = 'Here is a message from python. Your server is down, please check.'
# Gmail Sign In
gmail_sender = ‘YOUR_GMAIL_ACCOUNT@gmail.com'
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()
Solution 4:
This function is working for me:
`def server_connect(account, password, server, port=587): if int(port) == 465: # gmail server email_server = smtplib.SMTP_SSL(server, str(port)) else: email_server = smtplib.SMTP(server, port) email_server.ehlo() email_server.starttls() email_server.login(account, password) return email_server #-------- `
I hope this helps.
Post a Comment for "Python 3 | Send Email -- Smtp -- Gmail -- Error : Smtpexception"