Active5 months ago
I am attempting to connect to the Gmail SMTP mail server and perform tasks as outlined by the skeleton code given to me. Only the use of
socket
s is allowed (so not the smtplib
). I need to: send HELO
command, MAIL FROM
, RCPT TO
, and DATA
.SMTP ('mail.python.org', port = 587) >>> context = ssl. Create_default_context >>> smtp. Be sure to read OpenSSL’s documentation about the cipher list format. If you want to check which ciphers are enabled by a given cipher list, use SSLContext. Transport Layer Security (TLS) Extensions D. Eastlake IANA TLS: Transport Layer.
![Python Smtp Tls Python Smtp Tls](/uploads/1/2/5/6/125619039/466162777.jpg)
There are many cases of similar problems posted, but they haven't received the proper answer. For example:Implementing Transport Layer Security in Python - Simple Mail Client
The program is required to connect to
smtp.gmail.com
over port 587
. I've taken two different approaches:- Using STARTTLS:
- Using SSL:Then
clientSocketSSL
replaces all instances ofclientSocket
. The STARTTLS lines are also removed andimport ssl
is added to the top.
When using the first method, the
MAIL FROM:
command isn't returning anything. I'm getting the following output:When using SSL, I'm getting the same as the linked post:
Am I missing something here? I guess my best option is to use TLS but I have no idea how to go about that... is there something wrong with my
Stefan van den AkkerMAIL FROM
command?4,34166 gold badges3232 silver badges5151 bronze badges
user1287523user128752353233 gold badges1212 silver badges2929 bronze badges
![Tls Tls](https://i.stack.imgur.com/us3UI.png)
2 Answers
When using SSL, you need to connect to port 465 instead of port 587. If you use STARTTLS, you still need to use
ssl.wrap_socket
, you just do it later - specifically, after receiving the 220
response to the STARTTLS
command. After doing STARTTLS
, you're supposed to do HELO
again, since the server is supposed to forget anything that happened before the STARTTLS
.In either case, the servers at smtp.google.com ports 465 and 587 still won't return a
250
response to the MAIL
command, since they require that you are authenticated before you send mail. You'll get a 530
response instead. You'll need to use the AUTH
command with your gmail.com credentials to authenticate before you can use MAIL
successfully on those servers.If you don't want to authenticate, and depending on the details of what you need to do, you could try using port 25 of the server found in gmail.com's MX record. At the moment, the server is gmail-smtp-in.l.google.com and supports STARTTLS.
Aleksi TorhamoAleksi Torhamo4,77422 gold badges2626 silver badges3535 bronze badges
Bhargav Rao♦32.6k2121 gold badges9696 silver badges115115 bronze badges
cowpeacowpea