通过python发送邮件

问题描述:

我尝试安装和配置SMTP服务器。在我看来,这件事发生了。当我从Linux命令行发送测试邮件时,我会收到邮件,例如:

I tried to install and configure SMTP server. and it seems to me it happened. And when I send test mail from linux command line I receive mail, ex :

echo "Test mail from postfix4" | mail -s "Test Postfix4" test@gmail.com

但是当我尝试通过python我收到错误消息:

But when i tried to do the same via python i got error :

>>> from email.MIMEText import MIMEText
>>> import smtplib
>>> sender = 'test@mail.com'
>>> to = 'test@gmail.com'
>>> subj = "atata"
>>> body = "ololo"
>>> server = 'hostname'
>>> msg = MIMEText(body, 'plain', 'utf-8')
>>> msg['Subject'] = subj
>>> msg['To'] = to
>>> msg['Importance'] = 'high'
>>> s = smtplib.SMTP(server)
>>> s.sendmail(sender, [to], msg.as_string())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/smtplib.py", line 742, in sendmail
    raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'test@gmail.com': (454, '4.7.1 <test@gmail.com>: Relay access denied')}
>>> s.quit()
(221, '2.0.0 Bye')

有人

我提出了类似的问题此处:尤其要注意,调用 mail 命令命令行上的操作与使用 smtplib 中的 SMTP.sendmail()明显不同:

I asnwered a similar question here: In particular note that calling the mail command on the command-line is doing something significantly different from using SMTP.sendmail() from smtplib:


  • mail 命令绕过SMTP,而是在本地注入邮件。机器通常配置为根据其邮件配置对注入的电子邮件进行明智的处理。在您描述的设置中,您可以认为这是邮件服务器的后门(此处为Postfix):它不需要SMTP即可将邮件存储在Postfix中; Postfix从那里接管。

  • The mail command bypasses SMTP, instead injecting mail locally; the machine is typically configured to do something sensible with the injected email according to its mail configuration. In a setup such as you've described you could think of that as being a back-door into the mail server (Postfix here): It doesn't need SMTP to deposit the message with Postfix; Postfix takes over from there.

SMTP.sendmail()不会没有后门访问权限,而是您的Python程序正在与本地计算机上的Postfix建立 localhost ):需要说服您的Postfix设置传入的SMTP连接足够可靠,可以接受它,然后对邮件进行一些有用的操作。通常,这是通过让您的Python程序进行身份验证(请参见上面对您的问题的 server.login()注释),和/或使用其他端口/服务: mail 提交,通常是在端口587上,和/或具有非常宽松的Postfix配置,可以接受任何内容-或任何源自 localhost 的内容。

SMTP.sendmail() by contrast doesn't have that back-door access, instead your Python program is setting up an SMTP connection to Postfix on your local machine (localhost): Your Postfix setup needs to be persuaded that this incoming SMTP connection is trustworthy enough to accept it and then do something useful with the message. This is typically by having your Python program authenticate (see the server.login() comments to your question above), and/or using a different port/service: "mail submission", often on port 587, and/or having a very lax Postfix configuration that accepts anything - or perhaps anything originating from localhost.

还值得注意的是,您看到的错误消息 4.7.1< test@gmail.com&gt ;:中继拒绝访问 将来自您的本地系统,而不是Gmail。

Also worth noting is that the error message you're seeing, 4.7.1 <test@gmail.com>: Relay access denied' will be coming from your local system - and not from Gmail.

对于此类问题,了解什么是邮件中继确实很有帮助。是,并且要非常清楚任何错误消息来自哪台机器:如果您认为错误消息来自远程系统,很容易在这里跳到错误的结论。

For issues like this it's really helpful to understand what "mail relaying" is, and to be very aware of which machine any error message is coming from: It's easy to jump to the wrong conclusion here if you think the error message is coming from the remote system.