【问题标题】:Python smtplib - No recipient after sending mailPython smtplib - 发送邮件后没有收件人
【发布时间】:2015-09-07 18:38:05
【问题描述】:

我最近编写了一个脚本,如果我想监控的网站使用 smtplib 发生更改,它会向我发送一封电子邮件。该程序有效,我收到了电子邮件,但是当我查看已发送的电子邮件时(因为我从同一个帐户向自己发送电子邮件),它说没有收件人或“收件人:”地址,只有一个密件抄送我希望将电子邮件发送到的地址。这是 smtplib 的一个特性吗?它实际上并没有添加“收件人:”地址,而只添加了密件抄送地址?代码如下:

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

【问题讨论】:

标签: python email smtplib


【解决方案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() 会自动将传入的第二个地址作为密件抄送您认为?
  • 是的,我认为这一定是默认行为。我一直只是在邮件中包含标题,因此直到现在才遇到您的问题。
【解决方案2】:

尝试手动将任何标题添加到您的消息中,并以空行与正文分隔,例如:

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

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

【讨论】:

  • 太棒了,感谢您的回复,只需要添加这些标题
【解决方案3】:

试试这个,似乎对我有用。

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

【讨论】:

    【解决方案4】:

    把它扔出去:请尝试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/图像或添加附件,您就会真的爱上这个包!

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

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 2014-01-31
      • 2010-10-07
      • 2012-02-09
      • 1970-01-01
      • 2011-10-19
      • 2021-04-26
      相关资源
      最近更新 更多