标头“收件人:”批量发件人 [英] Header "To:" for a Bulk Email Sender

查看:108
本文介绍了标头“收件人:”批量发件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个python代码,以向已签署列表的人们发送一些新闻通讯。
我的问题是标题收件人:部分!我无法将电子邮件放在收件人:地址列表中,并且收件人打开电子邮件时,他们在收件人:标题中看不到他们的电子邮件地址。这是我在说什么的屏幕截图: http://tinypic.com/r/zlr7sl / 9

I`m trying to make a python code to send some newsletter to people have signed up to a list. my problem is with Header "To:" part! I can't put emails in a list as "To:" address, and when receivers open the email, they don't see their email address in "To:" header. And here is a screenshot of what I'm talking about: http://tinypic.com/r/zlr7sl/9

我不是程序员,而是在尝试学习新知识。我的英语不太好。我希望你能理解我。

I`m not a programmer and am just trying to learn something new. My English is not perfect. I hope you understand me.

from smtplib import SMTP
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
host = 'smtp.server.com'
port = 587
usr  = 'USERNAME'
pwd  = 'PASSWORD'
from_email = 'SENDER)EMAIL'
my_list = open('slist.txt', 'r')
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Subject'
msg['From'] = from_email
msg['To'] = '' # <<<<<I want to put emails in slist.txt in this header one by one while sending the emails.
msg.add_header('reply-to','reply-to')
plain_text = 'Testing Message'
html_text = '''\
>>> HTML CODE<<
'''
part1 = MIMEText(plain_text, 'plain')
part2 = MIMEText(html_text, 'html')
msg.attach(part1)
msg.attach(part2)
server = SMTP(host, port)
server.ehlo()
server.starttls()
server.login(usr, pwd)
try:
    for emails in my_list:
     server.sendmail(from_email, emails, msg.as_string())
    print('!!!YEAHH!!!')
except:
    print('***OPS***')
server.close()


推荐答案

只要信封收件人是,您放在 To:标头中的内容并不重要

It doesn't really matter what you put in the To: header as long as the envelope recipient is correctly communicated.

邮件列表常用的技巧是将邮件列表本身放在To:标头中:

A common trick used by mailing lists is to put the mailing list itself in the To: header:

From: mailing-list@example.edu
To: mailing-list@example.edu
Bcc: actual@example.net, recipients@example.org,
 here@example.com, ...

如果您将此传递给 sendmail -t ,您将获得为伪造的 To:地址(或邮件循环,如果列表最终发送给自己,然后将传入的消息重新发送到整个列表,等等)进行退回,但是 sendmail 以完全忽略标题的方式接受收件人列表。您可以在文件 email.txt 中做到这一点:

If you pass this to sendmail -t you will get a bounce for the bogus To: address (or a mail loop, if the list ends up sending to itself, and then resending the incoming message to the entire list, etc) but sendmail accepts a list of recipients in a mode where the headers are completely ignored. You could do have this in a file email.txt:

From: me@example.org
To: fred@example.net
Subject: Secret stuff

xyzzy

现在,如果您执行 sendmail you@example.com< email.txt ,则邮件将发送给您(仅,而不是弗雷德(Fred)。

Now if you do sendmail you@example.com <email.txt the message will be sent to you (only, and not to Fred).

可将其视为信封中的一张纸。如果信封内的纸说北极的圣诞老人是收件人,但您将其放在发给宾夕法尼亚大道1600号总统先生的信封中,则无论该信上的内容如何,​​该消息都会发送给白宫。

Think of it as a sheet of paper inside an envelope. If the paper inside the envelope says "Santa Claus, North Pole" as the recipient, but you put it in an envelope addressed to "Mr. President, 1600 Pennsylvania Avenue" the message will go to the White House regardless of what it says on the paper inside the sealed envelope.

因此,就Python代码而言,您可以这样做

So in terms of Python code, you could do

msg['Subject'] = 'Subject'
msg['From'] = from_email
msg['To'] = 'noreply@example.org'
# ...
server = SMTP(host, port)
server.ehlo()
server.starttls()
server.login(usr, pwd)
try:
     server.sendmail(from_email, my_list, msg.as_string())

和不论 To:标头中的值如何,邮件都会发送到 my_list 上的收件人。

and the message will go to the recipients on my_list regardless of the value in the To: header.

另一方面,如果要为每个收件人发送带有单独的 To:标头的单独邮件,则需要在loo中修改 To:标头

On the other hand, if you want an individual message with an individual To: header to be sent for each recipient, you need to modify the To: header in the loop, too.

msg['Subject'] = 'Subject'
msg['From'] = from_email
msg['To'] = 'noreply@example.org'
# ...
server = SMTP(host, port)
server.ehlo()
server.starttls()
server.login(usr, pwd)
for user in my_list do:
    try:
         msg['To'] = user
         server.sendmail(from_email, [user], msg.as_string())
    except:
         raise HorrorError('Really, you want to raise an exception here')

但是,您应该了解,如果您在同一域中有多个收件人,则他们的电子邮件服务器将被几乎相同的邮件淹没,其区别仅在于 To:标头。您可以避免这种情况,特别是如果每​​个域中的收件人数量很少,但是某些邮件管理员肯定认为它是滥用的,并且/或者可以触发自动垃圾邮件过滤器。

However, you should understand that if you have multiple recipients in the same domain, their email server will be inundated with virtually identical messages which only differ by the To: header. You could get away with this, especially if the number of recipients in each domain is small, but it is definitely regarded as abusive by some mail administrators, and/or could trigger automated spam filters.

有可能,我在中放入了提高除外:处理程序,因为您真的除处理程序外,不应使用毯子。至少,您应该捕获错误并打印出确切原因的详细信息。否则,您可能会隐藏越来越多的错误。

Tangentially, I put in a raise in the except: handler because you really really should not be using a blanket except handler. At the very least, you should capture the error and print out detailed information about what exactly failed; otherwise you are hiding a probably growing number of bugs from yourself.

这篇关于标头“收件人:”批量发件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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