Sendmail Errno [61] Connection Refused

时间:2011-04-11 10:22:13

标签: python smtp smtplib

我一直试图让我的应用程序将一些输出的文本邮寄到电子邮件中。为了简化,我已经分离了脚本:

import smtplib
import sys
import os

SERVER = "localhost"

FROM = os.getlogin()
TO = [raw_input("To : ")]

SUBJECT = "Message From " + os.getlogin()

print "Message : (End with ^D)"
TEXT = ''
while 1:
    line = sys.stdin.readline()
    if not line:
        break
    TEXT = TEXT + line

# Prepare actual message

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# Send the mail

server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

此脚本输出:

    Traceback (most recent call last):
  File "/Users/christianlaustsen/Dropbox/Apps - Python/mail/smtplib_mail.py", line 32, in <module>
    server = smtplib.SMTP(SERVER)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 239, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 295, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 273, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 512, in create_connection
    raise error, msg
error: [Errno 61] Connection refused

正如您所看到的,连接被拒绝了。我在Mac OS X Snow Leopard上运行Python 2.6(如果相关的话)。

我试过很多搜索,但一直找不到解决方案。任何帮助将不胜感激。

7 个答案:

答案 0 :(得分:56)

如果您按如下方式启动本地服务器:

  

python -m smtpd -n -c DebuggingServer localhost:1025

确保修改邮件发送代码以使用非标准端口号:

server = smtplib.SMTP(SERVER, 1025)
server.sendmail(FROM, TO, message)
server.quit()

答案 1 :(得分:16)

我的猜测是您的本地计算机上没有安装任何SMTP服务器。

如果您的电子邮件不敏感,请打开Gmail帐户并send your emails using it with Python

答案 2 :(得分:12)

使用Python启动一个简单的SMTP服务器,如下所示:

python -m smtpd -n -c DebuggingServer localhost:1025

答案 3 :(得分:4)

如果您不想运行单独的服务器,并且您只使用Unix,则可以使用此技术,从http://www.yak.net/fqa/84.html复制,最初来自Python FAQ:

在Unix上,使用sendmail非常简单。 sendmail程序的位置因系统而异;有时它是/ usr / lib / sendmail,有时是/ usr / sbin / sendmail。 sendmail手册页将帮助您。这是一些示例代码:

SENDMAIL = "/usr/sbin/sendmail" # sendmail location
import os
p = os.popen("%s -t" % SENDMAIL, "w")
p.write("To: cary@ratatosk.org\n")
p.write("Subject: test\n")
p.write("\n") # blank line separating headers from body
p.write("Some text\n")
p.write("some more text\n")
sts = p.close()
if sts != 0:
    print "Sendmail exit status", sts

答案 4 :(得分:1)

我想创建一些东西,以便你可以复制粘贴它并让它工作,但这是我最接近的:

from email.message import EmailMessage
import smtplib
import os

def send_email(message,destination):
    # important, you need to send it to a server that knows how to send e-mails for you
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    # don't know how to do it without cleartexting the password and not relying on some json file that you dont git control...
    server.login('valid.username@gmail.com', 'password_for_gmail')
    msg = EmailMessage()
    msg.set_content(message)

    msg['Subject'] = 'TEST'
    msg['From'] = 'valid.username@gmail.com'
    msg['To'] = destination
    server.send_message(msg)

if __name__ == '__main__':
    send_email('msg','destination@email')

我认为该教程具有误导性,因为它假设您没有很好地告诉您已经有一个正在运行的服务器为您发送电子邮件......这很奇怪。我的脚本唯一的问题是我不知道如何在没有明确写入密码的情况下使其工作但是唉......至少它发送了吗?只是制作一个假的电子邮件地址或其他东西......

答案 5 :(得分:0)

如果您是系统的root用户,则可能需要安装opensmtpd。首先,您不需要手动运行服务器(默认情况下启用此服务,因此在安装smtpd后,可以手动启动它或重新启动计算机)。其次,您无需更改行server = smtplib.SMTP(SERVER)。最后,使用yum install opensmtpd或等效的apt-get命令。

答案 6 :(得分:0)

无论出于何种原因,我很难将服务器和端口传递给构造函数,但是没有连接函数。这最终为我工作:

    s = smtplib.SMTP(timeout=30) #seconds
    s.connect(EMAIL_HOST, EMAIL_PORT)
    m = MIMEText('see subject')
    m['subject'] = 'sweet subject'
    m['from'] = EMAIL_FROM
    m['to'] = to_list  # comma-separated list of emails
    s.sendmail(m['from'], m['to'].split(','), m.as_string())
    s.quit()