45

我一直在尝试让我的应用程序将一些输出的文本邮寄到电子邮件中。为简化起见,我隔离了脚本:

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(如果相关的话)。

我已经尝试过很多次搜索,但无法找到解决方案。任何帮助将不胜感激。

4

7 回答 7

60

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

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

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

server = smtplib.SMTP(SERVER, 1025)
server.sendmail(FROM, TO, message)
server.quit()
于 2011-09-14T20:53:37.403 回答
16

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

如果您的电子邮件不敏感,请打开一个 Gmail 帐户并使用 Python 发送您的电子邮件

于 2011-04-11T10:27:34.070 回答
16

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

python -m smtpd -n -c DebuggingServer localhost:1025
于 2011-08-26T00:58:37.013 回答
5

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

在 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
于 2013-11-06T06:08:41.733 回答
4

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

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')

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


很久以前提出这个问题,所以我不记得这是什么意思,确切地放在这里以防万一:

仅当您启用对不太安全的应用程序的访问时,它才有效: myaccount.google.com/lesssecureapps 。我认为你应该把它作为答案。

我可能只是为此使用虚假电子邮件或类似的东西或我的组织不记得的电子邮件来解决它。祝你好运!

于 2018-05-01T21:07:55.300 回答
0

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

于 2014-04-22T13:36:33.660 回答
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()
于 2016-08-24T15:31:46.760 回答