Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sender = "from@gmail.com"
- to_address = ["to@gmail.com"]
- subject = "Test subject"
- body = "Test body"
- title = "title"
- attach_files = ['c:\dummy.pdf']
- host = 'localhost.com'
- user = 'user@mail.com'
- password = 'password'
- msg_root = MIMEMultipart('related')
- addr = '{} <{}>'.format(title, sender)
- name, address = parseaddr(addr)
- msg_root['From'] = formataddr((
- Header(name, 'utf-8').encode(),
- addr.encode('utf-8') if isinstance(addr, unicode) else addr))
- msg_root['To'] = ','.join(to_address)
- msg_root['Subject'] = Header(subject, 'utf-8')
- msg_text = MIMEText(body, 'html', 'utf-8')
- msg_root.attach(msg_text)
- for index, attach_file in enumerate(attach_files, start=1):
- with open(attach_file, 'rb') as attach_obj:
- attach = MIMEApplication(attach_obj.read(),
- _subtype="pdf",
- name=os.path.basename(attach_file))
- attach.add_header('Content-Disposition', 'attachment',
- filename=os.path.basename(attach_file))
- msg_root.attach(attach)
- connection = smtplib.SMTP_SSL(host=host, timeout=5)
- try:
- connection.login(user=user, password=password)
- connection.sendmail(user, all_address, msg_root.as_string())
- finally:
- connection.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement