Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

python - How to send a mail directly to SMTP server without authentication?

I would like to send an email directly from a script to a Gmail email account, by connecting directly to smtp.gmail.com.

However, I would prefer not to have the gmail password in the script. From what I have read, it appears that Gmail requires authentication before it will deliver any mail, including to its own users.

My question is, how is mail coming from another SMTP server ever delivered, since that SMTP server will not have Gmail credentials. Does Gmail only require authentication for "anonymous" senders, and since my script is running on a personal computer, it is subject to higher security? Here is the python script I am running:

import smtplib
import email
msg = email.message.Message()
msg["From"] = "[email protected]"
msg["To"] = "[email protected]"
msg["Subject"] = "Test message"
server = smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
server.ehlo_or_helo_if_needed()
try:
    failed = server.sendmail("[email protected]","[email protected]", msg.as_string())
    server.close()
except Exception as e:
    print(e)

When I run this script, the output is:

(530, b'5.5.1 Authentication Required. Learn more at
5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 fw5sm21125889wib.0', '[email protected]')

My question is, how do external SMTP servers avoid this problem? And is whatever they do replicable in a local script, or does it require correct reverse DNS records, SPF records, etc.?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thats a real good question, and i am replying inline.

I would like to send an email directly from a script to a Gmail email account, by connecting directly to smtp.gmail.com.

First of all smtp.gmail.com is not a mailserver which accepts mail (from other mailservers), but rather allow Gmail users to login and hence send or check email. If we want to find out the Gmail mailservers that accepts mails from other mailservers. We can run the following cmd on the terminal:

dig mx gmail.com +short

output:

10 alt1.gmail-smtp-in.l.google.com.
40 alt4.gmail-smtp-in.l.google.com.
5 gmail-smtp-in.l.google.com.
30 alt3.gmail-smtp-in.l.google.com.
20 alt2.gmail-smtp-in.l.google.com. 

Since gmail-smtp-in.l.google.com. has the lowest value of 5 we use it as the preferred mailserver

However, I would prefer not to have the gmail password in the script. From what I have read, it appears that Gmail requires authentication before it will deliver any mail, including to its own users.

Exactly one uses smtp.gmail.com to login and send/check emails to/from their respective accounts, therefore we require user credentials. However we don't need credentials on sending emails to its mail server i.e gmail-smtp-in.l.google.com (Example below)

My question is, how is mail coming from another SMTP server ever delivered, since that SMTP server will not have Gmail credentials. Does Gmail only require authentication for "anonymous" senders, and since my script is running on a personal computer, it is subject to higher security? Here is the python script I am running:

As I have made myself clear from discussion above we don't need Gmail credentials to connect to Gmail mail servers, however if we connect to Gmail mail servers using personal computers we can get away with sending a few emails, but to send more emails we need to build domain reputation and accountability using DKIM, SPF etc (Thats a whole different spectrum).

The following python script sends email to a gmail account without authentication.

import smtplib

fromaddr = '[email protected]'
toaddrs  = ['[email protected]']
# string inside msg below must have "Subject: <subject line>
"
# for a subject to be sent, and "To: " for the recipient to be shown in the email
msg = '''To: [email protected]
    Subject: Subject line here

    The body goes here
    .
'''

msg = msg.format(fromaddr =fromaddr, toaddr = toaddrs[0])
# The actual mail send
server = smtplib.SMTP('gmail-smtp-in.l.google.com:25')
server.starttls()
server.ehlo("example.com")
server.mail(fromaddr)
server.rcpt(toaddrs[0])
server.data(msg)
server.quit()  

Or try the following Telnet snippet

telnet gmail-smtp-in.l.google.com 25

HELO sendingdomain.com

MAIL FROM:<[email protected]>

RCPT TO:<[email protected]>

DATA
From: <[email protected]>
To: <[email protected]>
Subject: Just a test email

The body of the mail goes here.          
.

QUIT

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...