2012-02-03 21 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