我正在处理使用python2.7.6从linux集群发送到内部电子邮件收件人的零星未发送电子邮件,这些收件人的地址已知有效。由于原始代码使用cat <file> | mailx -s <subject> <recipient>
的弃用语法来生成用于提交为subprocess(cmd, shell=True)
的cmd,因此我使用smtp进行了一些修改后的代码修改,如下所示。
import smtplib
from email.mime.text import MIMEText
def emailer(from_addr, to_addr, subj, email_body):
if not os.path.isfile(email_body):
msg = MIMEText(email_body)
else:
with open(email_body, 'rb') as FHrb:
msg = MIMEText(FHrb.read())
msg['Subject']=subj
s = smtplib.SMTP('localhost'); s.set_debuglevel(True)
try:
s.sendmail(from_addr,
','.join(to_addr) if isinstance(to_addr, list) else to_addr.split(','),
msg.as_string())
except Exception as e:
print e
s.quit()
当被调用时,此代码返回控制台下面的详细程度(隐藏的特定细节以保护无辜并减少帖子大小)
send: 'ehlo <localhost>.<localdomain>\r\n'
reply: '250-<localhost>.localdomain\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 10240000\r\n'
reply: '250-VRFY\r\n'
reply: '250-ETRN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-8BITMIME\r\n'
reply: '250 DSN\r\n'
reply: retcode (250); Msg: <localhost>.localdomain
PIPELINING
SIZE 10240000
VRFY
ETRN
ENHANCEDSTATUSCODES
8BITMIME
DSN
send: 'mail FROM:<SENDER> size=4178\r\n'
reply: '250 2.1.0 Ok\r\n'
reply: retcode (250); Msg: 2.1.0 Ok
send: 'rcpt TO:<me@my_email_address>\r\n'
reply: '250 2.1.5 Ok\r\n'
reply: retcode (250); Msg: 2.1.5 Ok
send: 'data\r\n'
reply: '354 End data with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF>
data: (354, 'End data with <CR><LF>.<CR><LF>')
send: 'Content-Type: text/plain; charset="us-ascii"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\nSubject: subject supplied\r\n\r\n\r\n<CONTENT REMOVED>.\r\n'
reply: '250 2.0.0 Ok: queued as 7548F12004F\r\n'
reply: retcode (250); Msg: 2.0.0 Ok: queued as 7548F12004F
data: (250, '2.0.0 Ok: queued as 7548F12004F')
{}
尾随空{}表示成功。这是由maillogs确认的:
Feb 1 11:02:06 <localhost> postfix/smtpd[19642]: 7548F12004F: client=localhost[127.0.0.1]
Feb 1 11:02:06 <localhost> postfix/cleanup[18681]: 7548F12004F: message-id=<20170201110206.7548F12004F@<localhost>.localdomain>
Feb 1 11:02:06 <localhost> postfix/qmgr[2243]: 7548F12004F: from=<ngsservice@<localhost>.localdomain>, size=4685, nrcpt=1 (queue active)
Feb 1 11:02:06 <localhost> postfix/smtp[19867]: 7548F12004F: to=me@my_email_address, relay=<our ip address>[<our ip address>]:25, delay=0.5, delays=0/0.01/0/0.48, dsn=2.6.0, status=sent (250 2.6.0 <20170201110206.7548F12004F@<localhost>.localdomain> [InternalId=32804943] Queued mail for delivery)
Feb 1 11:02:06 <localhost> postfix/qmgr[2243]: 7548F12004F: removed
但是,如果我传递完全无效的电子邮件地址(例如,在Outlook中测试时返回未送达收据),我会得到完全相同的回复。
控制台输出表明系统具有DSN功能。运行调试命令sendmail -bv <me@my_email_address>
将返回控制台消息Mail Delivery Status Report will be mailed to <me>
,并且maillog具有DSN条目:
Jan 26 08:34:38 <localdomain> postfix/pickup[16208]: 83BC11200A5: uid=<my_uid> from=<me>
Jan 26 08:34:38 <localdomain> postfix/cleanup[8341]: 83BC11200A5: message-id=<20170126083438.83BC11200A5@<localdomain>.localdomain>
Jan 26 08:34:38 <localdomain> postfix/qmgr[2243]: 83BC11200A5: from=<me@<localdomain>.localdomain>, size=316, nrcpt=1 (queue active)
Jan 26 08:34:38 <localdomain> postfix/smtp[8343]: 83BC11200A5: to=<me@my_email_address>, relay=<our ip address>[<our ip address>]:25, delay=0.06, delays=0.03/0.01/0.01/0.02, dsn=2.1.5, status=deliverable (250 2.1.5 Recipient OK)
Jan 26 08:34:38 <localdomain> postfix/cleanup[8341]: 8F0591200A7: message-id=<20170126083438.8F0591200A7@<localdomain>.localdomain>
Jan 26 08:34:38 <localdomain> postfix/bounce[8344]: 83BC11200A5: sender delivery status notification: 8F0591200A7
Jan 26 08:34:38 <localdomain> postfix/qmgr[2243]: 83BC11200A5: removed
Jan 26 08:34:38 <localdomain> postfix/qmgr[2243]: 8F0591200A7: from=<>, size=1988, nrcpt=1 (queue active)
Jan 26 08:34:38 <localdomain> postfix/local[8345]: 8F0591200A7: to=<me@<localdomain>.localdomain>, relay=local, delay=0.02, delays=0/0.01/0/0.01, dsn=2.0.0, status=sent (delivered to mailbox)
Jan 26 08:34:38 <localdomain> postfix/qmgr[2243]: 8F0591200A7: removed
因此系统似乎能够进行DSN传递状态通知,这似乎是一个很好的方式,但我需要帮助解决如何实现这一点。对我来说这是一个新领域,所以明确的细节会很棒。非常感谢