Python3中的Unicode

时间:2016-07-20 08:50:53

标签: python-3.x unicode

我想使用python3以波斯语发送警报电子邮件。但我遇到以下错误:

  

发送邮件失败; 'ascii'编解码器不能编码位置115-120的字符:序数不在范围内(128)

这是我的python脚本:

import sys
import smtplib
import datetime

mail_from = "test@example.com"
rcpt_to = sys.argv[1]  # mail receiver

username = "test@example.com"
password = "P@ssW0rD"
smtpServer = "MAIL_SERVER_IP_ADDRESS"

subject = sys.argv[2]
date = datetime.datetime.now().strftime("%d-%m-%Y %H:%M")
message_body = 'سلام'

msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % (mail_from, rcpt_to, subject, date, message_body)

try:
    conn = smtplib.SMTP(smtpServer, 25)
    conn.set_debuglevel(False)
    conn.login(username, password)
    try:
        conn.sendmail(mail_from, rcpt_to, msg)
    finally:
        conn.close()
except Exception as exc:
    sys.exit("Sending mail failed; %s" % str(exc))

我该如何解决这个问题?感谢

0 个答案:

没有答案