Forums

smtplib and GMAIL

I am using the following:

:::python

import smtplib

def my_Mailer():

sender_email = 'xxxx@gmail.com'
receiver_email = 'yyyy@gmail.com'
cc_email = 'zzzz@gmail.com'
password = 'myapppassword'# Create Email

from email.message import EmailMessage
msg = EmailMessage()
msg.set_content('EMAIL TEST.')
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Cc'] = cc_email
msg['Subject'] = 'PythonAnywhere Test Message'


server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(sender_email, password)
server.send_message(msg)
server.quit()

I am using an app password I have 2 factor There are no new security events flagged at google it works on my local machine but not on pythonanywhere

What am I doing wrong

even switched to: server = smtplib.SMTP_SSL('smtp.gmail.com', 587)

no errors when it runs

Does it work now or not?

OK changed the line to 587

:::python

Traceback (most recent call last):
File "emailer_test.py", line 36, in <module>
my_Mailer()
File "emailer_test.py", line 30, in my_Mailer
server = smtplib.SMTP_SSL('smtp.gmail.com', 587)
File "/usr/lib/python3.8/smtplib.py", line 1034, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/usr/lib/python3.8/smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.8/smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.8/smtplib.py", line 1042, in _get_socket
new_socket = self.context.wrap_socket(new_socket,
File "/usr/lib/python3.8/ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "/usr/lib/python3.8/ssl.py", line 1040, in _create
self.do_handshake()
File "/usr/lib/python3.8/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:1108)

OK...this worked:

:::python

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart



 def my_Mailer():
     # Import Python Packages
     sender_email = 'sender@gmail.com'
     receiver_email = 'person@gmail.com'
     cc_email = 'person@yahoo.com
     password = 'myapppassword'# Create Email

     msg = MIMEMultipart()
     msg['From'] = 'sender@gmail.com'
     msg['To'] = receiver_email
     msg['Cc'] = cc_email
     msg['Subject'] = 'PYTHON ANYWHERE TEST MESSAGE'
     message = 'This is a test message'
     msg.attach(MIMEText(message))

     mailserver = smtplib.SMTP('smtp.gmail.com',587)
# identify ourselves to smtp gmail client
     mailserver.ehlo()
# secure our email with tls encryption
     mailserver.starttls()
# re-identify ourselves as an encrypted connection
     mailserver.ehlo()
     mailserver.login(sender_email, password)

#mailserver.sendmail(sender_email,receiver_email,msg.as_string())
     mailserver.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())

     mailserver.quit()

Glad to hear that you made it work!