【发布时间】:2014-08-04 16:39:54
【问题描述】:
我检查了 Stackoverflow 并发现了类似的问题,但似乎没有一个能够回答我的具体问题。我正在使用 Python 2.6。
以下函数用于通过 sendmail 成功发送带有附件的纯文本电子邮件:
def sndmsg(u,A,a):
"""
+ sends email(s) with plain text and attachment
u = str - company url
A = str - manager's name
a = list of email addresses to send to
"""
u=u.replace('/','')
file = open('message.txt','rb')
txt=file.read()
file.close()
for t in a:
out = MIMEMultipart()
out['Subject'] = 'Free Report'
out['From'] = 'admin@example.com'
out['To'] = t.strip()
out['Date'] = date()
pt1 = MIMEText(txt.format(A,u))
out.attach(pt1)
att = MIMEApplication(open('/path/'+u+'.xls',"rb").read())
att.add_header('Content-Disposition', 'attachment', filename=u+'.xls')
out.attach(att)
p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
p.communicate(out.as_string())
随后对其进行了修改以包含 html 格式的文本(如下)。代码执行良好,并毫无错误地进入下一个函数,但这次我的任何收件箱或垃圾邮件文件夹中都没有收到电子邮件。
def sndmsg(u,A,a):
"""
+ sends email(s) with html text, plain text and attachment
u = str - company url
A = str - manager's name
a = list of email addresses to send to
H[0] = str - html table
"""
u=u.replace('/','')
file = open('message.txt','rb')
txt=file.read()
file.close()
file = open('message.html','rb')
htm=file.read()
file.close()
for t in a:
out = MIMEMultipart()
out['Subject'] = 'Free Report'
out['From'] = 'admin@example.com'
out['To'] = t.strip()
out['Date'] = date()
inn = MIMEMultipart('alternative')
pt1 = MIMEText(txt.format(A,u))
pt2 = MIMEText(htm.format(A,u,H[0]), 'html')
inn.attach(pt1)
inn.attach(pt2)
out.attach(inn)
att = MIMEApplication(open('/path/'+u+'.xls',"rb").read())
att.add_header('Content-Disposition', 'attachment', filename=u+'.xls')
out.attach(att)
p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
p.communicate(out.as_string())
我的主要问题是它是编码问题还是Sendmail 问题。
我尝试独立使用第二个功能,并在浏览器中单独打开了 html 消息,看起来不错,但电子邮件仍然没有发送。
我没有对SSH 进入服务器的root 访问权限并检查/var/log/mail。我已经询问了服务器管理员,但没有得到任何帮助。
如果是编码错误,需要修复或改进什么?
感谢您对我的无知的耐心和您的帮助。
【问题讨论】:
-
好吧,一方面,
H是未定义的。 -
感谢您的评论。是的,H 是一个全局变量,其中包含在其他函数中调用以生成 html 表的 str 列表,其中一个 H[0] 被插入到此模板电子邮件中。