Reputation: 1583
My below code is for sending email with an attachment.
def email(location):
COMMASPACE = ', '
fromaddr = "[email protected]"
toaddr = ("[email protected]")
outer = MIMEMultipart()
outer['Subject'] = 'Dashboard'
outer['From'] = fromaddr
outer['To'] = toaddr
msg = MIMEBase('text','plain')
msgtext = 'Please find dashboard for the current reporting week.'
msg.set_payload(msgtext)
Encoders.encode_base64(msg)
outer.attach(msg)
outer.epilogue = ' '
ctype, encoding = mimetypes.guess_type("parse.mht")
maintype, subtype = ctype.split('/', 1)
fp = open(location,'rb')
msg = MIMEBase(maintype, subtype)
msg.set_payload(fp.read())
fp.close()
Encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment', filename='Dashboard.mht')
outer.attach(msg)
s = smtplib.SMTP()
s.connect('mailhost.dev.ch3.s.com')
s.sendmail(fromaddr, toaddr, outer.as_string(False))
s.close()
try:
email(location)
appendlog.write("Email has been sent, clearing down files... \n")
print "Email has been sent, clearing down files..."
success = 1
except Exception,e:
print repr(e)
print "Email has failed to send."
I am getting an exception as
Typerror("Expected list, got type 'str' ",)
Email has failed to send
Can someone tell me what is wrong in the code?
Upvotes: 0
Views: 65
Reputation: 21461
toaddr
should be a list
, not tuple
or str
if you use SMTP
Or you can use yagmail, where you can also just send a string, and it also much easier to send attachments with:
import yagmail
fromaddr = "[email protected]"
toaddr = "[email protected]"
yag = yagmail.SMTP(fromaddr, 'yourpassword', host='mailhost.dev.ch3.s.com')
yag.send(toaddr, 'Subject', ['Please find dashboard for the current reporting week.',
'/path/to/local/parse.mht'])
Note that adding the filename as string will convert it to an attachment.
Upvotes: 1