I'm using Python 3 and I'm trying to attach a file to an email message. I'm pretty new at MIME and SMTP stuff. So this is my function:
def func():
path = mypath
for file in glob.glob(path + "\\happy*"):
print(file)
sender = myemail
senderto = someonesemail
msg = MIMEMultipart('alternative')
msg['Subject'] = 'The File'
msg['From'] = sender
msg['To'] = senderto
body = "File"
msg.attach(MIMEText(body, 'plain'))
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(file, encoding='charmap').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % file)
msg.attach(part)
global smtpSettings
smtpSettings = smtplib.SMTP(host=myhost, port=587)
print("Step 1 Complete")
smtpSettings.login(smtpusername, smtppassword)
print("Step 2 Complete")
smtpSettings.sendmail(sender, senderto, msg.as_string)
print("Step 3 Complete")
smtpSettings.quit()
print("Success")
Side note: senderto = receiver. The output I get is:
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:/Users/Luis/Desktop/PYTHON/smtptestes.py", line 73, in func
smtpSettings.sendmail(sender, senderto, msg.as_string)
File "C:\Python34\lib\smtplib.py", line 769, in sendmail
esmtp_opts.append("size=%d" % len(msg))
TypeError: object of type 'method' has no len()
Fix in step 3, change
to
because
as_string
is a method