Python smtplib - 发送邮件后无收件人

3

最近,我编写了一个脚本,使用smtplib向我发送一封电子邮件,如果我想要监视的网站发生了变化。程序已经可以使用,并且我可以收到电子邮件,但是当我查看发送的电子邮件时(因为我会从同一个帐户向自己发送电子邮件),它显示没有收件人或"To:"地址,只有一个Bcc地址,该地址是我要发送电子邮件的地址。这是否是smtplib的一个特性 -- 它实际上不会添加"To:"地址,只有Bcc地址?代码如下:

if (old_source != new_source):

# now we create a mesasge to send via email
fromAddr = "example@gmail.com"
toAddr = "example@gmail.com"
msg = ""

# smtp login
username = "example@gmail.com"
pswd = "password"

# create server object and login to the gmail smtp
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server.login(username, pswd)
server.sendmail(fromAddr, toAddr, msg)
server.quit()

在开头添加发件人和收件人的标题!

- Padraic Cunningham
4个回答

1
请手动添加任何标题到您的消息中,使用一个空行与正文分开,例如:
...
msg="""From: sender@domain.org
To: recipient@otherdomain.org
Subject: Test mail

Mail body, ..."""
...

太棒了,感谢回复,只需要添加这些头信息即可。 - Ben Granger

1
更新您的代码如下,就可以解决问题:

if (old_source != new_source):

# now we create a mesasge to send via email
fromAddr = "example@gmail.com"
toAddr = "example@gmail.com"
msg = ""

# smtp login
username = "example@gmail.com"
pswd = "password"

# create server object and login to the gmail smtp

server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
header = 'To:' + toAddr + '\n' + 'From: ' + fromAddr + '\n' + 'Subject:testing \n'
msg = header + msg
server.login(username, pswd)
server.sendmail(fromAddr, toAddr, msg)
server.quit()

好的,因为这条消息没有头部,所以smtplib的sendmail()自动将传递的第二个地址作为“密送”抄送,你觉得呢? - Ben Granger
是的,我认为那应该是默认行为。我一直都是将头部信息包含在消息中,因此直到现在我才遇到了你的问题。 - gtlambert

0

你可以试试这个方法,对我来说似乎有效。

#!/usr/bin/python

#from smtplib import SMTP # Standard connection
from smtplib import SMTP_SSL as SMTP #SSL connection
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

sender = 'example@gmail.com'
receivers = ['example@gmail.com']


msg = MIMEMultipart()
msg['From'] = 'example@gmail.com'
msg['To'] = 'example@gmail.com'
msg['Subject'] = 'simple email via python test 1'
message = 'This is the body of the email line 1\nLine 2\nEnd'
msg.attach(MIMEText(message))

ServerConnect = False
try:
    smtp_server = SMTP('smtp.gmail.com','465')
    smtp_server.login('#####@gmail.com', '############')
    ServerConnect = True
except SMTPHeloError as e:
    print "Server did not reply"
except SMTPAuthenticationError as e:
    print "Incorrect username/password combination"
except SMTPException as e:
    print "Authentication failed"

if ServerConnect == True:
    try:
        smtp_server.sendmail(sender, receivers, msg.as_string())
        print "Successfully sent email"
    except SMTPException as e:
        print "Error: unable to send email", e
    finally:
        smtp_server.close()

0

只是随便一提:请尝试使用 yagmail。免责声明:我是维护者,但我觉得它能帮助每个人!

它真的提供了很多默认值:我非常确定你能够直接发送电子邮件:

import yagmail
yag = yagmail.SMTP(username, password)
yag.send(to_addrs, contents = msg)

这也将设置头文件 :)

您需要先安装yagmail,可以使用以下任一方式:

pip install yagmail  # python 2
pip3 install yagmail # python 3

一旦您想要嵌入HTML/图像或添加附件,您将会非常喜欢这个软件包!

它还可以通过防止您在代码中使用密码来使其更加安全。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接