我想从我的 Debian 计算机发送电子邮件。我不希望电子邮件来自我的 Gmail 帐户,就像大多数 Pythonsmtplib
示例一样。
我打电话给我的 ISP,询问端口 25,他们告诉我使用(端口 587)或(带 SSL 的端口 465)。两个端口具有相同的结果,电子邮件已生成但未送达。如果我尝试smtplib.SMTP_SSL
在 python 中使用端口 465,则会收到此错误:
File "emailer.py", line 32, in <module>
smtp = smtplib.SMTP_SSL("localhost", 465)
File "/usr/lib/python2.7/smtplib.py", line 792, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 798, in _get_socket
new_socket = ssl.wrap_socket(new_socket, self.keyfile, self.certfile)
File "/usr/lib/python2.7/ssl.py", line 891, in wrap_socket
ciphers=ciphers)
File "/usr/lib/python2.7/ssl.py", line 566, in __init__
self.do_handshake()
File "/usr/lib/python2.7/ssl.py", line 788, in do_handshake
self._sslobj.do_handshake()
socket.error: [Errno 104] Connection reset by peer
我的 Python 电子邮件程序代码:
to_addr = "[email protected]"
from_addr = ""
body_text = "auto email test"
message = MIMEText(body_text)
message["Subject"] = "subject"
message["From"] = from_addr
message["To"] = to_addr
message["Return-Path"] = "<>"
message["Auto-Submitted"] = "auto-generated"
smtp = smtplib.SMTP("localhost", 587)
try:
#smtp.starttls()
print("sending email")
smtp.sendmail(from_addr, [to_addr], message.as_string()) #body_text)
print("sent email")
except Exception, em:
print("ERROR: " + str(em) )
except SMTPException, em:
print("ERROR: " + str(em) )
smtp.quit()
这段代码运行没有任何错误。当我检查邮件队列时,邮件就在那里,没有去任何地方。
root@lappy:/home/user/raid# mailq
0m 482 1aLBNr-0005Fj-Ui <[email protected]>
[email protected]
/var/log/exim4/mainlog
这种类型的消息在尝试发送消息时出现,但最终超时。
2016-01-18 10:06:20 1aLBNr-0005Fj-Ui <= [email protected] H=localhost (lappy.home) [::1] P=esmtp S=482
2016-01-18 10:08:27 1aLBNr-0005Fj-Ui gmail-smtp-in.l.google.com [74.125.203.26] Connection timed out
2016-01-18 10:10:34 1aLBNr-0005Fj-Ui alt2.gmail-smtp-in.l.google.com [64.233.160.26] Connection timed out
2016-01-18 10:12:42 1aLBNr-0005Fj-Ui alt3.gmail-smtp-in.l.google.com [74.125.207.26] Connection timed out
2016-01-18 10:12:43 1aLBNr-0005Fj-Ui == [email protected] R=dnslookup T=remote_smtp defer (110): Connection timed out
exim
将其打印到主日志中。在这里您可以看到配置为侦听端口 587:
2016-01-18 09:14:03 exim 4.84 daemon started: pid=10234, -q30m, listening for SMTP on [127.0.0.1]:587 [::1]:587
我有这一行/etc/default/exim4
:
SMTPLISTENEROPTIONS='-oX 587 -oP /var/run/exim4/exim.pid'
我目前没有在 exim ( /etc/exim4/exim4.conf.template
) 中激活 TLS:
#####################################################
### main/03_exim4-config_tlsoptions
#####################################################
#tls_on_connect_ports=465
### main/03_exim4-config_tlsoptions
#################################