在 python 中发送电子邮件时出错:'bytes' 对象没有属性 'encode'
error when sending email in python: 'bytes' object has no attribute 'encode'
我需要在 python3 中发送一封电子邮件,下面是脚本,它失败了,错误为:
'bytes' object has no attribute 'encode'
import smtplib
from email.mime.text import MIMEText
from email.message import EmailMessage
att1 = [u'201902260920AM.log']
msg = MIMEText("EmailOperator testing email.")
msg['Subject'] = "EmailOperator testing email."
msg['From'] = "Airflow_Notification_No_Reply@company.Com"
msg['To'] = "pasle@company.com"
msg['files'] = str(att1).encode("UTF-8")
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
发送带附件的电子邮件的正确方法是什么?
不胜感激,如果有人能在这里开导我,先谢谢了。
UPDATE1:您可以 运行 python3 中的上述代码,您将收到错误
UPDATE2:事实上,我想附加的实际日志文件应该是这样的:
'/home/pasle/airflow/logs/pipeline_client1/send_email/2019-02-27T01:40:38.451894+00:00/1.log'
而且我需要发送带有多个附件的邮件,谢谢你的帮助。
att1 = [u'201902260920AM.log']
msg = MIMEText("EmailOperator testing email.")
msg['Subject'] = "EmailOperator testing email."
msg['From'] = "Airflow_Notification_No_Reply@novantas.Com"
msg['To'] = "rxie@novantas.com"
msg['files'] = att1[0].encode("utf-8")
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
它应该可以工作。
您使用的事实 [u'ABC']
将是 Unicode 字符串的单元素列表。
因此您需要将列表转换为单个 Unicode 字符串,然后将其转换为 utf-8
。
更新:
import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
files = ['/home/pasle/airflow/logs/pipeline_client1/send_email/2019-02-27T01:40:38.451894+00:00/1.log',
'/home/pasle/airflow/logs/pipeline_client1/send_email/2019-02-27T01:40:38.451894+00:00/2.log']
msg = MIMEMultipart()
msg['From'] = 'Airflow_Notification_No_Reply@novantas.com'
msg['To'] = 'rxie@novantas.com'
msg['Subject'] = 'Email operator testing email.'
message = MIMEText('Email operator testing email body text.')
msg.attach(message)
for f in files:
with open(f, "rb") as file:
part = MIMEApplication(
file.read(),
Name=basename(f)
)
# After the file is closed
part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
msg.attach(part)
gmail_sender = 'sender@gmail.com'
gmail_passwd = 'password'
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(gmail_sender, gmail_passwd)
server.send_message(msg)
server.quit()
在我调查您的问题时,Attribute Error
的发生是由于未将 msg
声明为 MIMEMultipart()
方法。
我需要在 python3 中发送一封电子邮件,下面是脚本,它失败了,错误为:
'bytes' object has no attribute 'encode'
import smtplib
from email.mime.text import MIMEText
from email.message import EmailMessage
att1 = [u'201902260920AM.log']
msg = MIMEText("EmailOperator testing email.")
msg['Subject'] = "EmailOperator testing email."
msg['From'] = "Airflow_Notification_No_Reply@company.Com"
msg['To'] = "pasle@company.com"
msg['files'] = str(att1).encode("UTF-8")
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
发送带附件的电子邮件的正确方法是什么?
不胜感激,如果有人能在这里开导我,先谢谢了。
UPDATE1:您可以 运行 python3 中的上述代码,您将收到错误
UPDATE2:事实上,我想附加的实际日志文件应该是这样的:
'/home/pasle/airflow/logs/pipeline_client1/send_email/2019-02-27T01:40:38.451894+00:00/1.log'
而且我需要发送带有多个附件的邮件,谢谢你的帮助。
att1 = [u'201902260920AM.log']
msg = MIMEText("EmailOperator testing email.")
msg['Subject'] = "EmailOperator testing email."
msg['From'] = "Airflow_Notification_No_Reply@novantas.Com"
msg['To'] = "rxie@novantas.com"
msg['files'] = att1[0].encode("utf-8")
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
它应该可以工作。
您使用的事实 [u'ABC']
将是 Unicode 字符串的单元素列表。
因此您需要将列表转换为单个 Unicode 字符串,然后将其转换为 utf-8
。
更新:
import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
files = ['/home/pasle/airflow/logs/pipeline_client1/send_email/2019-02-27T01:40:38.451894+00:00/1.log',
'/home/pasle/airflow/logs/pipeline_client1/send_email/2019-02-27T01:40:38.451894+00:00/2.log']
msg = MIMEMultipart()
msg['From'] = 'Airflow_Notification_No_Reply@novantas.com'
msg['To'] = 'rxie@novantas.com'
msg['Subject'] = 'Email operator testing email.'
message = MIMEText('Email operator testing email body text.')
msg.attach(message)
for f in files:
with open(f, "rb") as file:
part = MIMEApplication(
file.read(),
Name=basename(f)
)
# After the file is closed
part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
msg.attach(part)
gmail_sender = 'sender@gmail.com'
gmail_passwd = 'password'
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(gmail_sender, gmail_passwd)
server.send_message(msg)
server.quit()
在我调查您的问题时,Attribute Error
的发生是由于未将 msg
声明为 MIMEMultipart()
方法。