2011-09-08 44 views
0

這個工程:sendmail的成功,但是Python的smtplib無法連接

printf 'hi' | sendmail -f [email protected] [email protected] 

但這種失敗:

def send_mail(send_from, send_to, subject, text, files=[ ], server="localhost"): 
    assert type(send_to)==list 
    msg = MIMEMultipart() 
    msg['From'] = send_from 
    msg['To'] = COMMASPACE.join(send_to) 
    msg['Date'] = formatdate(localtime=True) 
    msg['Subject'] = subject 
    msg.attach(MIMEText(text)) 
    for f in files: 
     part = MIMEBase('application', "octet-stream") 
     part.set_payload(open(f, "r").read()) 
     Encoders.encode_base64(part) 
     part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f)) 
     msg.attach(part) 
    smtp = smtplib.SMTP(server) 
    smtp.sendmail(send_from, send_to, msg.as_string()) 
    smtp.close() 

輸出:

Traceback (most recent call last): 
    File "send_mail.py", line 50, in <module> 
    send_mail(send_from, send_to, subject, text, files) 
    File "send_mail.py", line 35, in send_mail 
    smtp = smtplib.SMTP(server) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 242, in __init__ 
    (code, msg) = self.connect(host, port) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 302, in connect 
    self.sock = self._get_socket(host, port, self.timeout) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 277, in _get_socket 
    return socket.create_connection((port, host), timeout) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection 
    raise err 
socket.error: [Errno 61] Connection refused 

如何獲得我的send_mail方法?

+0

聲音像沒有被綁定到'127.0.0.1:25'。 – cdhowie

+0

愚蠢的問題,但是你試圖在兩種情況下連接到相同的服務器?你的Python代碼中有一個默認的參數'server = localhost'。 –

+3

從命令行發送與通過tcp套接字發送不同的環境(如您的python代碼所做的那樣)。確保你的sendmail實際上正在監聽端口25上的外部連接(即使只有127.0.0.1:25) –

回答

0

重新發布與用戶回答這個問的問題:

事實上,沒有什麼是必然127.0.0.1:25。開始smtpd解決了這個問題。 @亞當羅森菲爾德 - 是的,它是同一臺服務器。 - lugbug 9年9月8日在22:49

相關問題