In company projects, emails need to be sent through the background, and the email content includes image attachments. If sent through PHPmailer, due to possible delays in the mail server, sending emails through PHPmailer requires waiting for the email to be sent successfully before the results can be returned. This has been proven in practice that sometimes sending emails cannot return results immediately, affecting the user experience.
So I sent the email through python, and PHP called it by calling the script, so that it would return immediately after successfully executing the script without having to judge whether the email was sent successfully. As long as the script file is successfully executed, a success flag is returned to the client. This greatly improves the email sending speed and ensures a good user experience.
However, when sending emails through python, I encountered the problem of garbled characters. The following phenomenon occurred during the debugging process:
1. The combination of Chinese and English letters causes garbled characters.
2. Two Chinese characters for the name of the person who replied to the email are normal, but three Chinese characters are garbled. This problem is very hidden. I only discovered this problem today, which caused me to make the same mistake twice in front of my boss. Because my test was OK (my name is two characters), but I didn’t test three characters, and I didn’t expect that the problem would arise here.
3. The email subject is garbled
4. Everything is normal, but when you click "Reply" on the email, some of the content that appears is garbled.
5. After the content problem was solved, I found that the name in the reply was also garbled. Moreover, QQ mailbox is normal, foxmail is normal, 163 is normal, and gmail is normal, but outlook is garbled.
Calling environment:
1. I use the reply person, reply email, sending email, file name, etc. as parameters of the script in PHP, and call the cmd command for convenient execution. As parameters, some characters are special characters. For example, issues such as ampersand, single quotation marks, double quotation marks, etc. Another problem is that there cannot be spaces between each parameter. If there are spaces, the order of parameters is disrupted.
In short, the problem of garbled characters cannot be perfectly solved. In the end, there was no other way, so I adopted the following method to finally solve the garbled code problem.
In PHP, write the content of the sent email, such as subject, reply name, email, content, etc., into the configuration file. The name of this configuration file is random, and the file directory is in the temporary directory of PHP. Ensure multi-person use. Then pass the configuration file name (including the path) when calling the python script in PHP, and process it by reading the configuration file in python. In this case, the topic and reply person, that is, the part involving Chinese characters, are garbled in 163 (the content part has not been tested at present. It has been determined that the topic and reply person involving Chinese characters are garbled in the 163 mailbox, but there are no garbled characters in the QQ mailbox , everything is normal), the solution is to convert it to utf8 through Header("xxxx", "utf-8") and everything is normal.
Share the relevant code below:
PHP calls python script
inifile=u'D:/PHP/Php/tmp/' + sys.argv[1]
config=ConfigParser.ConfigParser()
config.read(inifile)
os.remove(inifile)
subject=Header(config.get("mail","subject"),"utf-8")
ReplyToName=config.get("mail","ReplyToName")
ReplyToMail=config.get("mail","ReplyToMail")
To=config.get("mail","To")
file_name=config.get("mail","file_name")
From = "%s
server = smtplib.SMTP("smtp.exmail.qq.com",25)
server.login("xxxx_business@5186.me","itop202") #仅smtp服务器需要验证时
# 构造MIMEMultipart对象做为根容器
main_msg = email.MIMEMultipart.MIMEMultipart()
# 构造MIMEText对象做为邮件显示内容并附加到根容器
text_msg = email.MIMEText.MIMEText("xxx帮你转发的邮件",_charset="utf-8")
main_msg.attach(text_msg)
# 构造MIMEBase对象做为文件附件内容并附加到根容器
ctype,encoding = mimetypes.guess_type(file_name)
if ctype is None or encoding is not None:
ctype='application/octet-stream'
maintype,subtype = ctype.split('/',1)
file_msg=email.MIMEImage.MIMEImage(open(file_name,'rb').read(),subtype)
## 设置附件头
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改邮件头
main_msg.attach(file_msg)
# 设置根容器属性
main_msg['From'] = From
if ReplyToMail!='none':
main_msg['Reply-to'] = "%s<%s>" % (Header(ReplyToName,"utf-8"),ReplyToMail)
#main_msg['To'] = To
main_msg['Subject'] = subject
main_msg['Date'] = email.Utils.formatdate()
#main_msg['Bcc'] = To
# 得到格式化后的完整文本
fullText = main_msg.as_string()
# 用smtp发送邮件
try:
server.sendmail(From, To.split(';'), fullText)
finally:
server.quit()
os.remove(file_name)