SMTP将邮件发送给许多收件人,但没有收到它 [英] SMTP sent mail to many recipients but doesn't received it

查看:199
本文介绍了SMTP将邮件发送给许多收件人,但没有收到它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

当我尝试通过脚本发送至17封电子邮件(包括发件人电子邮件)时,我检查了基于Gmail的已发邮件,发现邮件已发送但我没有收到它。 只有第一个接受者收到该邮件。
如果我通过该邮件回复所有人,每个人都只收到该回复。



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



这是我的代码:

 #!/ usr / bin / env python 
导入smtplib
从datetime导入csv
导入日期时间,timedelta

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

sender ='MYBOT@gmail.com'

密码=无
开放('pass','rt')为f:
password = f。阅读()。strip('\\\
')


def send_mail(收件人,主题,正文):

发送愉快的bithday邮件

headers = [From:+ sender,
Subject:+ subject,
To:+ recipient,
MIME-Version :1.0,
Content-Type:text / html]

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

smtp = smtplib.SMTP(SMTP_SERVER,SMTP_PORT)
smtp.ehlo()
smtp.starttls()
smtp.ehlo
smtp.login(发件人,密码)

body =+ body +
smtp.sendmail(发件人,收件人,标题+\r\\\
\r\\\
+正文)
打印发送至,
打印收件人

smtp.quit()

def send_happybirthday(收件人):
body =祝你生日快乐!
\\\
< br />从C2k8pro与爱

subject ='[BirthReminder]祝你生日快乐!来自C2k8pro'
send_mail(收件人,主题,body)


def send_notification(all_mails,names):
body =明天是%s的生日%names
send_mail(all_mails ,body,body)

def test_send_mail():

notify_body =明天是
recipients = ['MYBOT @ gmail。 com']

today = datetime.now()
format =%d-%m-%Y
今天打印
today_in_str = datetime.strftime(今天,格式)


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

today = datetime.now()
one_day = timedelta(days = 1)
明天=今天+ one_day

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

print'今天是',str_today
tomorrow_birth = []
阅读器中的行:
name = row [1] .strip()
dob = row [2]
dmy = dob.split(/)
mail = row [3]
all_mails.append (邮件)

#TODO修复只有1位数字的dob
birth_date = dmy [0] +/+ dmy [1]
如果str_today == birth_date:
print'生日快乐%s'%姓名

尝试:
send_happybirthday(邮件)
除了例外,e:
print e

elif str_tomorrow == birth_date:
tomorrow_birth.append(name)
print明天是%s的生日%name

#删除空字符串
all_mails = filter(None,all_mails)
print'All mail:',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()

任何人都可以解释这一点。

解决方案

我从这里找到了解决方案



用Python发送电子邮件给.txt文件中的多个收件人smtplib



我传递了一个字符串,其中包含所有以逗号分隔的收件人,分别是msg ['To']和sendmail()。
对msg ['To']是真的,但对于sendmail,我必须使用一个列表。


I 've written a Python script to automatically send some information to my friends. I used SMTPlib, it works well if I only sent to me or addition another mail .

When i try to send to 17 emails (including my sender email) through script, then I checked sent mail on web-base Gmail, i saw that mail was sent but I didn't receive it. ONLY FIRST RECIPIENT received that mail. If I reply to all from that mail, everyone got only that reply.

I cant figure out why they didn‘t received it when I sent from script, I ask my friend check in spam but she didn't find anything.

This is my code:

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

Can anyone explain this. Thanks!

解决方案

I found solution from here

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

I passed a string contain all recipients separated by comma to msg['To'] and sendmail(). It's true for msg['To'] but with sendmail, I have to use a list.

这篇关于SMTP将邮件发送给许多收件人,但没有收到它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆