error when sending email in python: 'bytes' object has no attribute 'encode'
Asked Answered
L

1

7

I need to send out an email in python3, below is the script and it failed with an error of:

'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'] = "[email protected]"
msg['To'] = "[email protected]"

msg['files'] = str(att1).encode("UTF-8")

s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()

What's the right way to send out an email with attachment?

Much appreciated if anyone can enlighten me here, thank you in advance.

UPDATE1: you can run the above code in python3 and you will receive the error

UPDATE2: Indeed the actual log files I want to attach would be something like this: '/home/pasle/airflow/logs/pipeline_client1/send_email/2019-02-27T01:40:38.451894+00:00/1.log'

and I need to send emails with multiple attachments, thank you for your help.

Loggerhead answered 27/2, 2019 at 3:6 Comment(1)
Isn't att1 a list object? So that is why it is showing byte error.Radiogram
R
4
att1 = [u'201902260920AM.log']
msg = MIMEText("EmailOperator testing email.")
msg['Subject'] = "EmailOperator testing email."
msg['From'] = "[email protected]"
msg['To'] = "[email protected]"

msg['files'] = att1[0].encode("utf-8")

s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()

It should probably work.

The fact you are using [u'ABC'] would be a one-element list of Unicode strings.

So you need to convert the list to a single Unicode string, and then convert that to utf-8.

UPDATE:

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'] = '[email protected]'
msg['To'] = '[email protected]'
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 = '[email protected]'
gmail_passwd = 'password'

server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(gmail_sender, gmail_passwd)

server.send_message(msg)
server.quit()

As I looked into your problem Attribute Error was occurring due to not declaring msg as a MIMEMultipart() method.

Radiogram answered 27/2, 2019 at 3:17 Comment(5)
Thanks. AttributeError: 'bytes' object has no attribute 'encode'Loggerhead
Indeed the actual log files I want to attach would be something like this: `'/home/pasle/airflow/logs/pipeline_client1/send_email/2019-02-27T01:40:38.451894+00:00/1.log', and I need to send emails with multiple attachments, thank you for your help.Loggerhead
Thanks Karikey, but as I commented earlier here, there is another error AttributeError: 'bytes' object has no attribute 'encode'Loggerhead
Made some changesRadiogram
Thank you, I got that working before I see your latest update, same approach.Loggerhead

© 2022 - 2024 — McMap. All rights reserved.