我正在尝试使用 Python 以编程方式发送电子邮件。电子邮件保留在邮件队列 ( mailq
) 中。详细信息如下。
我的诊断能力已经到了极限。也许问题是我的 ISP 阻止了从我的(动态)IP 地址发出的电子邮件。也许 Gmail 和 Yahoo 期望使用不同的端口或协议。
技术设置:
- Debian 4.8.4-1
- exim4(使用重新配置
# dpkg-reconfigure exim4-config
;如何重置为默认值?) - Python 2.7.9
- Debian PC --> 路由器 --> WAN
Python 电子邮件程序脚本
#!/usr/bin/env python
import cgi
from email.MIMEImage import MIMEImage
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import mimetypes
import multiprocessing
import os
import smtplib
import sys
import traceback
to_addr = "<me>@gmail.com"
from_addr = "<me>@gmail.com"
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")
#smtp = smtplib.SMTP("localhost", 587)
#smtp = smtplib.SMTP("localhost", 666)
#smtp = smtplib.SMTP("localhost", 25)
try:
print("sending email")
smtp.sendmail(from_addr, [to_addr], body_text)
print("sent email")
except Exception, em:
print("ERROR: " + str(em) )
except SMTPException, em:
print("ERROR: " + str(em) )
smtp.quit()
Python脚本输出(未指定端口,或指定端口25)
root@deb:/****/****/****# python emailer.py
sending email
sent email
这是滞留在邮件队列中的电子邮件:
root@deb:/****/****/****# mailq
5m 266 1aFMdt-0002d6-KW <>
<me>@gmail.com
Python脚本输出(使用端口587,或端口666)〜在这种情况下,mailq为空
root@mypc:/****/****/****# python emailer.py
Traceback (most recent call last):
File "emailer.py", line 51, in <module>
smtp = smtplib.SMTP("localhost", 666)
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 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 111] Connection refused
exim4 日志输出 ( /var/log/exim4/mainlog
)
2016-01-02 08:54:49 1aFMdt-0002d6-KW <= <> H=localhost (deb.home) [127.0.0.1] P=esmtp S=266
2016-01-02 08:56:57 1aFMdt-0002d6-KW gmail-smtp-in.l.google.com [74.125.203.26] Connection timed out
2016-01-02 08:56:57 1aFMdt-0002d6-KW == <me>@gmail.com R=dnslookup T=remote_smtp defer (110): Connection timed out
答案1
您可以通过执行以下操作来检查您是否有权访问邮件中继:
nc mailrelay 25
我强烈怀疑您的 ISP 阻止了外发邮件流量(网络上已经有太多垃圾邮件,据估计占所有电子邮件流量的 95%)