I want to send alert email in Persian language using python3. but i encounter with the following error:
Sending mail failed; 'ascii' codec can't encode characters in position 115-120: ordinal not in range(128)
and this is my python script:
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))
How can i resolve this problem? Thanks