Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # encoding: utf-8
- from __future__ import unicode_literals, print_function
- import smtplib
- from email.mime.multipart import MIMEMultipart
- from email.mime.base import MIMEBase
- from email.mime.text import MIMEText
- from email.utils import formatdate
- from email import encoders
- def send_mail(send_from, send_to, subject, text, filename):
- '''
- send_from: str
- send_to: list
- subject: str
- text: str
- filename: file path
- '''
- msg = MIMEMultipart()
- msg['From'] = send_from
- msg['To'] = ', '.join(send_to)
- msg['Date'] = formatdate(localtime=True)
- msg['Subject'] = subject
- msg.attach(MIMEText(text, _subtype='plain', _charset='utf8'))
- # html = '<html><head><title>test</title></head><body>hello</body></html>'
- # msg.attach(MIMEText(html, _subtype='html', _charset='utf8'))
- part = MIMEBase('application', 'octet-stream')
- part.set_payload(open(filename, 'rb').read())
- encoders.encode_base64(part)
- part.add_header('Content-Disposition', 'attachment; filename="{}"'.format(filename))
- msg.attach(part)
- # FIXME: using you smtp server and login information
- server = ''
- port = 587
- username = ''
- password = ''
- conn = get_smtp_conn(server, port, username, password)
- conn.sendmail(send_from, send_to, msg.as_string())
- conn.quit()
- def get_smtp_conn(server, port, username, password, isTls=True):
- smtp = smtplib.SMTP(server, port)
- if isTls:
- smtp.starttls()
- smtp.login(username, password)
- return smtp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement