Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python smtplib-发送邮件后没有收件人_Python_Email_Smtplib - Fatal编程技术网

Python smtplib-发送邮件后没有收件人

Python smtplib-发送邮件后没有收件人,python,email,smtplib,Python,Email,Smtplib,我最近写了一个脚本,如果我想监控的网站使用smtplib进行了更改,我会给我发送一封电子邮件。该程序运行正常,我收到了电子邮件,但当我看到发送的电子邮件(因为我是从同一个帐户向自己发送电子邮件)时,它说没有收件人或“收件人:”地址,只有一个密件抄送中心,地址是我希望发送电子邮件的地址。这是smtplib的一个特性吗?它实际上不添加“To:”地址,只添加Bcc地址?代码如下: if (old_source != new_source): # now we create a mesasge to

我最近写了一个脚本,如果我想监控的网站使用smtplib进行了更改,我会给我发送一封电子邮件。该程序运行正常,我收到了电子邮件,但当我看到发送的电子邮件(因为我是从同一个帐户向自己发送电子邮件)时,它说没有收件人或“收件人:”地址,只有一个密件抄送中心,地址是我希望发送电子邮件的地址。这是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()

尝试手动将任何标题添加到邮件中,并用空行与正文隔开,例如:

...
msg="""From: sender@domain.org
To: recipient@otherdomain.org
Subject: Test mail

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

试试这个,似乎对我有用

#!/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()

按以下方式更新代码将实现此目的:

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

把它扔出去:请试试。免责声明:我是维护者,但我觉得它可以帮助所有人

它确实提供了很多默认设置:我很确定您可以直接通过以下方式发送电子邮件:

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/图像或添加附件,你就会爱上这个软件包了


它还可以防止您在代码中输入密码,从而使其更加安全。

->#在开头添加from:和to:标题好,既然消息没有任何标题,smtplib的sendmail()会自动将第二个地址作为您认为的密件抄送(bcc)输入?是的,我认为这一定是默认行为。我总是在邮件中包含标题,因此直到现在才发现您的问题。太棒了,谢谢您的回复,我只需要添加这些标题