Linux
為什麼我用 Python 生成的電子郵件卡在郵件隊列中
我正在嘗試使用 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
您可以通過例如檢查您是否可以訪問郵件中繼
nc mailrelay 25
我強烈懷疑您的 ISP 會阻止外發郵件流量(網路上的垃圾郵件已經太多了,據估計佔所有電子郵件流量的 95%)