通过python发送带unicode的邮件

时间:2014-10-07 07:03:52

标签: python email unicode smtp

我需要发送包含unicode字符的smtp邮件(utf-8)

我收到此错误" ascii编解码器无法解码位置0的字节0x7d:序号不在0-128"

范围内

这是我的代码:

for mail in mails:
            sender = 'omris@esg.co.il'
            receivers = [mail]

            message = """From: From server <server@esg.co.il>
            To: To client <client@esg.co.il>
            Subject: SMTP e-mail test

            analysis of %s for company %s just finished
            """ % (subject, custName)


            try:
               smtpObj = smtplib.SMTP('code',port)
               smtpObj.sendmail(sender, receivers, message)
               #self.SchedulerLogger.info("mail has been sent to analysts")
               print "Successfully sent email to: ", mail
            except Exception,ex:
               print ex.message
               print "Error: unable to send email"
    except Exception,ex:
        self.SchedulerLogger.error(str(ex.message))
        self.SchedulerLogger.info("send mail failure")

custName是希伯来文..

1 个答案:

答案 0 :(得分:0)

使用html发送电子邮件并使用MIMEText

将消息修改为

from email.mime.text import MIMEText
message = MIMEText("""From: From server <server@esg.co.il>
        To: To client <client@esg.co.il>
        Subject: SMTP e-mail test

        analysis of %s for company %s just finished
        """ % (subject, custName))