python - How to send email attachments? -
i having problems understanding how email attachment using python. have emailed simple messages smtplib
. please explain how send attachment in email. know there other posts online python beginner find them hard understand.
here's another, adapted here:
import smtplib os.path import basename email.mime.application import mimeapplication email.mime.multipart import mimemultipart email.mime.text import mimetext email.utils import commaspace, formatdate def send_mail(send_from, send_to, subject, text, files=none, server="127.0.0.1"): assert isinstance(send_to, list) msg = mimemultipart() msg['from'] = send_from msg['to'] = commaspace.join(send_to) msg['date'] = formatdate(localtime=true) msg['subject'] = subject msg.attach(mimetext(text)) f in files or []: open(f, "rb") fil: part = mimeapplication( fil.read(), name=basename(f) ) # after file closed part['content-disposition'] = 'attachment; filename="%s"' % basename(f) msg.attach(part) smtp = smtplib.smtp(server) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.close()
it's same first example... should easier drop in.
Comments
Post a Comment