这是一个非常好的问题,我正在内联回复。
我想通过直接连接到 smtp.gmail.com,将电子邮件直接从脚本发送到 Gmail 电子邮件帐户。
首先smtp.gmail.com不是接受邮件(来自其他邮件服务器)的邮件服务器,而是允许Gmail用户登录并发送或检查电子邮件。如果我们想找出接受其他邮件服务器邮件的 Gmail 邮件服务器。我们可以在终端上运行以下cmd:
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.
具有最低的价值5我们使用它作为首选邮件服务器
但是,我不希望脚本中包含 gmail 密码。据我所知,Gmail 似乎需要身份验证才能发送任何邮件,包括发送给自己的用户。
正好有一个使用smtp.gmail.com登录并向各自的帐户发送/检查电子邮件,因此我们需要用户凭据。但是,我们不需要凭据即可将电子邮件发送到其邮件服务器,即gmail-smtp-in.l.google.com(如下示例)
我的问题是,来自另一个 SMTP 服务器的邮件如何传送,因为该 SMTP 服务器没有 Gmail 凭据。 Gmail是否只需要对“匿名”发件人进行身份验证,并且由于我的脚本在个人计算机上运行,因此它的安全性更高?这是我正在运行的 python 脚本:
正如我在上面的讨论中明确表示的那样,我们不需要 Gmail 凭据即可连接到Gmail 邮件服务器,但是如果我们连接到Gmail 邮件服务器使用个人计算机,我们可以发送几封电子邮件,但要发送更多电子邮件,我们需要使用 DKIM、SPF 等(这是一个完全不同的范围)建立域声誉和责任。
以下 python 脚本将电子邮件发送到 gmail 帐户,无需身份验证。
import smtplib
fromaddr = '[email protected] /cdn-cgi/l/email-protection'
toaddrs = ['[email protected] /cdn-cgi/l/email-protection']
# string inside msg below must have "Subject: <subject line>\n"
# for a subject to be sent, and "To: " for the recipient to be shown in the email
msg = '''To: [email protected] /cdn-cgi/l/email-protection
Subject: Subject line here\n
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()
或者尝试以下 Telnet 片段
telnet gmail-smtp-in.l.google.com 25
HELO sendingdomain.com
MAIL FROM:<[email protected] /cdn-cgi/l/email-protection>
RCPT TO:<[email protected] /cdn-cgi/l/email-protection>
DATA
From: <[email protected] /cdn-cgi/l/email-protection>
To: <[email protected] /cdn-cgi/l/email-protection>
Subject: Just a test email
The body of the mail goes here.
.
QUIT