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