Email attachment received as 'noname' with PYTHON

132 Views Asked by At

I'm trying to send an email with attachment with the Python 'email' module, everything is working fine but the attachment is getting received by the receiver as 'noname'.

Check my code below:

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

# Setup email log message
logfile = "log.txt"
sender = "[email protected]"
password = "xxxxx"
receiver = "[email protected]"
subject = "Log file ALERT!"
body = """
Hello Dave,
Here is your log file.
"""

email = MIMEMultipart()
email["From"] = sender
email["To"] = receiver
email["Subject"] = subject

email.attach(MIMEText(body, "plain"))
attachment = "logkeys.txt"
attached_file = open(attachment, 'rb')
payload = MIMEBase('application', 'octet-stream')
payload.set_payload((attached_file).read())
encoders.encode_base64(payload)
email.add_header('Content-Disposition', 'attachment', filename=attachment)
email.attach(payload)

session = smtplib.SMTP_SSL('smtp.gmail.com', 465) #use gmail with port
session.login(sender, password) #login with mail_id and password
session.sendmail(sender, receiver, email.as_string())
session.quit()

Where is the error coming from?

I already changed header and payload to other options available but it's still sending as 'noname'

0

There are 0 best solutions below