【问题标题】:SMTP sent mail to many recipients but doesn't received itSMTP 向许多收件人发送邮件但未收到
【发布时间】:2012-07-29 12:26:20
【问题描述】:

我编写了一个 Python 脚本来自动向我的朋友发送一些信息。我使用了SMTPlib,如果我只发送给我或另外一封电子邮件,它会很好地工作。

当我尝试发送到 17 封电子邮件(包括我的发件人电子邮件)时,它会显示在基于网络的 Gmail 的已发送邮件中。我看到邮件已发送,但我没有收到。只有第一个收件人收到了电子邮件。 如果我从那封邮件中回复所有人,那么每个人都只会收到那个回复。

我不知道为什么当我从脚本发送它时他们没有收到它,我问我的朋友检查垃圾邮件,但她没有找到任何东西。

这是我的代码:

#!/usr/bin/env python
import smtplib
import csv
from datetime import datetime, timedelta

SMTP_SERVER  = 'smtp.gmail.com'
SMTP_PORT = 587

sender = 'MYBOT@gmail.com'

password = None
with open('pass', 'rt') as f:
    password = f.read().strip('\n')


def send_mail(recipient, subject, body):
    """
    Send happy bithday mail
    """
    headers = ["From: " + sender,
               "Subject: " + subject,
               "To: " + recipient,
               "MIME-Version: 1.0",
               "Content-Type: text/html"]

    headers = "\r\n".join(headers) 

    smtp = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo
    smtp.login(sender, password)

    body = "" + body +""
    smtp.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
    print "Sent to ", 
    print recipient

    smtp.quit()

def send_happybirthday(recipient):
    body = """Happy birthday to you!
            \n<br/>From C2k8pro with love
          """
    subject ='[BirthReminder] Happy birthday to you! from C2k8pro'
    send_mail(recipient, subject, body)


def send_notification(all_mails, names):
    body = """Tomorrow is birthday of %s""" % names
    send_mail(all_mails, body,  body)

def test_send_mail():

    notify_body = """Tomorrow is birthday of """
    recipients = ['MYBOT@gmail.com']

    today = datetime.now()
    format = "%d-%m-%Y"
    print today
    today_in_str = datetime.strftime(today, format)


def read_csv():
    FILENAME = 'mails.csv'
    reader = csv.reader(open(FILENAME, 'rt'), delimiter=',')

    today = datetime.now()
    one_day = timedelta(days=1)
    tomorrow = today + one_day

    all_mails = []
    str_format = "%d/%m"
    str_today = today.strftime(str_format)
    str_tomorrow = tomorrow.strftime(str_format)

    print 'Today is ', str_today
    tomorrow_birth = []
    for row in reader:
        name = row[1].strip()
        dob = row[2]
        dmy = dob.split("/")
        mail = row[3]
        all_mails.append(mail)

        #TODO fix dob with only 1 digit
        birth_date = dmy[0] + "/" + dmy[1]
        if str_today == birth_date:
            print 'Happy birthday %s' % name

            try:
                send_happybirthday(mail)
            except Exception, e:
                print e

        elif str_tomorrow == birth_date:
            tomorrow_birth.append(name)
            print "Tomorrow is %s's birthday" % name

    # Remove empty string
    all_mails = filter(None, all_mails)
    print 'All mails: ', len(all_mails)
    str_all_mails = ', '.join(all_mails)

    if tomorrow_birth:
        all_tomorrow = ', '.join(tomorrow_birth)
        send_notification(str_all_mails, all_tomorrow)


def main():
    read_csv()

if __name__ == "__main__":
    main()

谁能解释一下。谢谢!

【问题讨论】:

  • 你能提供你的代码吗?
  • 也许我从这里找到了解决方案:stackoverflow.com/questions/6941811/… 我将相同的字符串传递给 msg['To'] 和 sendmail(from_email, emails, msg.as_string() )
  • 投反对票:如果没有看到代码或错误消息,这个问题是不可能解决的。

标签: python smtp gmail


【解决方案1】:

我从这里找到了解决方案

Send Email to multiple recipients from .txt file with Python smtplib

我将一个包含所有收件人的字符串传递给 msg['To'] 和 sendmail()。 msg['To'] 是这样,但是对于 sendmail,我必须使用一个列表。

【讨论】:

    猜你喜欢
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    相关资源
    最近更新 更多