0

I try to send message from gmail with python 3.6 by this part of code:

import smtplib as smtp
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os

SUBJECT = *subject message*
SOURCE = *directory*
TEXT = *some text in message*

msg = MIMEMultipart()
msg['From'] = *send from email*
msg['To'] = *send to email*
msg['Subject'] = SOURCE

#################### part with attachment
msg.attach(MIMEText(TEXT, 'plain'))
filename = os.path.basename(SOURCE)
attachment = open(SOURCE, 'rb')
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename = %s'%filename)
msg.attach(part)
#################### end of attachment part

#################### server part
server = smtp.SMTP(smtp.gmail.com', 587)
server.starttls()
server.login(*send from email*, *send from email password*)
server.sendmail(*send from email*, *send to email*, msg.as_string())
server.close()

But get an AtributeError:

AttributeError: 'list' object has no attribute 'encode'

If i delete Attachment part of code and add msg = MIMEText(TEXT) before server part i get an letter in my email, but it doesn't contain subject. So i get a letter with some text only.

What i do wrong? Any thoughts?

EDIT: The error appears in msg.as_string() line

Lumos
  • 570
  • 1
  • 11
  • 24

2 Answers2

0

Here's how I send emails over python

#this will allow us to send emails over our smtp server
import smtplib

#who the message will be from, doesn't really need to be set since the message variable will do this automatically
sender ='sender@email.com'
#who the message will be sent to (this one matters)
receiver = 'receiver@email.com'

#the actual email we'll be sending, note its contents- the To section is irrelevant but neccessary
message = """From: From Person <sender@email.com>
To: To Person <receiver@email.com>
Subject: SMTP Email Test

this is a test email
"""

try:
    #proc our server to send the mail by providing it's ip to the program
    smtpObj = smtplib.SMTP('<your smtp ip address goes here>')#this is the ip of the server I'm on
    #attempt to send the piece of  mail
    smtpObj.sendmail(sender, receiver, message)
    #if the above executes send a confirmation to the user
    print "Successfully sent mail!"
except:
    #if something goes wrong catch it ad send a message to the user
    print "Error: unable to send mail"

to add attachments simply add the following which I retrieved from here

# open the file to be sent  
filename = "File_name_with_extension"
attachment = open("Path of the file", "rb") 

# instance of MIMEBase and named as p 
p = MIMEBase('application', 'octet-stream') 

# To change the payload into encoded form 
p.set_payload((attachment).read()) 

# encode into base64 
encoders.encode_base64(p) 

p.add_header('Content-Disposition', "attachment; filename= %s" % filename) 

# attach the instance 'p' to instance 'msg' 
smtpObj.attach(p) 
CyberStems
  • 326
  • 2
  • 15
  • Thank you. But in this code there aren't any way to sent an attachment. – Lumos Dec 03 '19 at 13:53
  • my apologies just realized I sent an old version of the script, I have updated it with the resource I used to enable attachments. Hope this helps :) if you have any questions please let me know – CyberStems Dec 03 '19 at 13:55
  • Feel sorry to say it, but this doesn't help. I have the same ```AttributeError: 'list' object has no attribute 'encode``` – Lumos Dec 03 '19 at 14:05
  • 1
    sorry I wasn't able to help; perhaps I'll pass this resource onto you as a last ditch effort to assist you: https://stackoverflow.com/questions/44607943/how-to-send-email-attachments-with-python-3-6 – CyberStems Dec 03 '19 at 14:09
0

Changing:

msg.attach(MIMEText(TEXT, 'plain'))
filename = os.path.basename(SOURCE)
attachment = open(SOURCE, 'rb')
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename = %s'%filename)
msg.attach(part)

with:

msg.attach(MIMEText(text))

for f in files:
    with open(f, "rb") as fil:
        part = MIMEApplication(fil.read(), Name = basename(f))           
    part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
    msg.attach(part)

worked for me. Thanks to How to send email attachments? @Oli answer.

Lumos
  • 570
  • 1
  • 11
  • 24