Python 3.x 用python发送电子邮件时出错:';字节';对象没有属性';编码';

Python 3.x 用python发送电子邮件时出错:';字节';对象没有属性';编码';,python-3.x,sendmail,Python 3.x,Sendmail,我需要用python3发送一封电子邮件,下面是脚本,它失败了,错误为: “bytes”对象没有属性“encode” 发送带有附件的电子邮件的正确方式是什么 如果有人能在这里启发我,我将不胜感激,提前谢谢你 更新1:您可以在python3中运行上述代码,您将收到错误消息 UPDATE2:实际上,我想要附加的实际日志文件如下所示: '/home/pasle/aiffair/logs/pipeline\u client1/send\u email/2019-02-27T01:40:38.451894+

我需要用python3发送一封电子邮件,下面是脚本,它失败了,错误为:

“bytes”对象没有属性“encode”

发送带有附件的电子邮件的正确方式是什么

如果有人能在这里启发我,我将不胜感激,提前谢谢你

更新1:您可以在python3中运行上述代码,您将收到错误消息


UPDATE2:实际上,我想要附加的实际日志文件如下所示:
'/home/pasle/aiffair/logs/pipeline\u client1/send\u 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()

当我调查您的问题时,由于未将
msg
声明为
MIMEMultipart()
方法,出现了
属性错误。

难道
att1
不是
列表
对象吗?这就是它显示字节错误的原因。谢谢
AttributeError:“bytes”对象没有属性“encode”
实际上,我要附加的实际日志文件是这样的:`/home/pasle/aiffair/logs/pipeline\u client1/send\u email/2019-02-27T01:40:38.451894+00:00/1.log',我需要发送带有多个附件的电子邮件,谢谢你的帮助。谢谢Karikey,但正如我之前在这里评论的那样,还有另一个错误
AttributeError:“bytes”对象没有属性“encode”
做了一些更改谢谢,在看到您的最新更新之前,我已经开始工作了,同样的方法。
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()