2012-02-03 38 views
0

发送HTML电子邮件usind sendmail命令我有下面的代码:如何从Python的

def send_email(self, alert_emails, subj, msg): 
    global alert_from 

    p = os.popen("/usr/sbin/sendmail -t" , 'w') 
    p.write("To: %s\n" % (','.join(alert_emails),)) 
    p.write("From: %s\n" % (alert_from,)) 
    p.write("Subject: %s\n\n" % (subj,)) 
    p.write(msg) 
    return p.close() 

它发送纯文本邮件。我怎样才能改变它来发送HTML邮件呢?

感谢

+1

已经回答: http://stackoverflow.com/questions/882712/sending-html-email-in-python – 2012-02-03 15:22:15

回答

1

使用Content-Type: text/html标题。并使用smtplib发送电子邮件。

0

为什么你会使用sendmail直接? Python有这样的库。使用email模块创建邮件,并使用smtplib发送邮件。

加上没有必要使用global声明,除非你改变你的函数里面全局变量,你不是。

+0

不知道为什么,但是当我使用所有的smtplib邮件到垃圾邮件。我不知道为什么,但sendmail -t可以与纯文本消息一起工作。我知道这是跛脚:)也许你是对的,我必须找到为什么所有的电子邮件使用smtplib去垃圾邮件...... – KennyPowers 2012-02-03 15:23:35