如何在 Python 中使用 smtplib 正确编码邮件
How encoding correctly for mailing with smtplib in Python
我正在尝试制作一个 python 脚本来验证某些文件并在有更改时发送电子邮件。
脚本运行良好,但如果我写了一些特殊字符,他不会发送邮件...
我已经查看了编码网站,但无法解决我的问题。
这是我的脚本:
#!/usr/bin/env python3
# -*- coding:Latin-1 -*-
import os
import shutil
import smtplib
# Fonctions
def compare (old,new):
x = 0
if old == new:
print('Les fichiers sont à jour')
x = 0
if old != new:
print('Nouveaux Fichiers de tests')
x = 1
mail = open('mail.txt', 'w', encoding='utf8')
mail.write("test d'écriture\n")
mail.write("si ca marche c'est top\n")
mail.close()
return x
# Récupération des fichiers
os.chdir("/var/www/corealpi/integration/integration_core")
rep = os.getcwd()
print(rep)
# copie du fichier tests.txt
shutil.copyfile('tests.txt','/home/git/integration/tests.new')
os.chdir("/home/git/integration")
rep_int = os.getcwd()
print(rep_int)
# Ouverture des fichiers
oldFile = open('tests.old','r')
newFile = open('tests.new','r')
tOld = oldFile.read()
tNew = newFile.read()
verif = compare (tOld,tNew)
print(verif)
# Envoie de mails
if verif == 1:
from email.mime.text import MIMEText
mailfile = 'mail.txt'
with open(mailfile) as fp:
# Create a text/plain message
msg = MIMEText(fp.read())
msg.set_charset('utf-8')
msg['Subject'] = 'Nouveaux Fichiers de %s' % mailfile
msg['From'] = 'sender@mail.fr'
msg['To'] = 'receiver@mail.fr'
# Envoi du message
s = smtplib.SMTP('192.168.3.2')
s.send_message(msg)
s.quit
else:
print('Rien à signaler')
我用 utf8 编码...但它似乎不适用于 send_message
?但也许是我把我弄糊涂了或者说我的生活太复杂了。
如果有人可以帮助我?
编辑 : 我尝试了很多编码方式,但我无法解决这个问题...请 heeelp :)
感谢您的帮助。
您忘记说明您平台上的默认编码是什么,以及您使用的是 Python2 还是 3.
我假设您使用 Python 2,但对于 Python 3 应该不会有太大差异。
实际上,您应该在 email.mime.text.MIMEText
对象中设置字符集,因为 doc 说 charset 默认为 us-ascii,这是错误的。你应该使用:
msg = MIMEText(fp.read(), 'plain', charset)
其中 charset 是编码数据的字符集,如果您使用 Python2,或者您要编码邮件的字符集是您使用的 Python 3.
如果您想独立于 Python2 中的本地编码以 UTF8 发送邮件,请先将其解码为 unicode 字符串,然后应用输出字符集:
msg = MIMEText(fp.read().decode(charset), 'plain', 'utf-8')
我正在尝试制作一个 python 脚本来验证某些文件并在有更改时发送电子邮件。
脚本运行良好,但如果我写了一些特殊字符,他不会发送邮件...
我已经查看了编码网站,但无法解决我的问题。
这是我的脚本:
#!/usr/bin/env python3
# -*- coding:Latin-1 -*-
import os
import shutil
import smtplib
# Fonctions
def compare (old,new):
x = 0
if old == new:
print('Les fichiers sont à jour')
x = 0
if old != new:
print('Nouveaux Fichiers de tests')
x = 1
mail = open('mail.txt', 'w', encoding='utf8')
mail.write("test d'écriture\n")
mail.write("si ca marche c'est top\n")
mail.close()
return x
# Récupération des fichiers
os.chdir("/var/www/corealpi/integration/integration_core")
rep = os.getcwd()
print(rep)
# copie du fichier tests.txt
shutil.copyfile('tests.txt','/home/git/integration/tests.new')
os.chdir("/home/git/integration")
rep_int = os.getcwd()
print(rep_int)
# Ouverture des fichiers
oldFile = open('tests.old','r')
newFile = open('tests.new','r')
tOld = oldFile.read()
tNew = newFile.read()
verif = compare (tOld,tNew)
print(verif)
# Envoie de mails
if verif == 1:
from email.mime.text import MIMEText
mailfile = 'mail.txt'
with open(mailfile) as fp:
# Create a text/plain message
msg = MIMEText(fp.read())
msg.set_charset('utf-8')
msg['Subject'] = 'Nouveaux Fichiers de %s' % mailfile
msg['From'] = 'sender@mail.fr'
msg['To'] = 'receiver@mail.fr'
# Envoi du message
s = smtplib.SMTP('192.168.3.2')
s.send_message(msg)
s.quit
else:
print('Rien à signaler')
我用 utf8 编码...但它似乎不适用于 send_message
?但也许是我把我弄糊涂了或者说我的生活太复杂了。
如果有人可以帮助我?
编辑 : 我尝试了很多编码方式,但我无法解决这个问题...请 heeelp :)
感谢您的帮助。
您忘记说明您平台上的默认编码是什么,以及您使用的是 Python2 还是 3.
我假设您使用 Python 2,但对于 Python 3 应该不会有太大差异。
实际上,您应该在 email.mime.text.MIMEText
对象中设置字符集,因为 doc 说 charset 默认为 us-ascii,这是错误的。你应该使用:
msg = MIMEText(fp.read(), 'plain', charset)
其中 charset 是编码数据的字符集,如果您使用 Python2,或者您要编码邮件的字符集是您使用的 Python 3.
如果您想独立于 Python2 中的本地编码以 UTF8 发送邮件,请先将其解码为 unicode 字符串,然后应用输出字符集:
msg = MIMEText(fp.read().decode(charset), 'plain', 'utf-8')