Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import smtplib
- from os.path import basename
- from email.mime.application import MIMEApplication
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- from email.utils import COMMASPACE, formatdate
- def send_mail(send_from, send_to, subject, text, cc=[], bcc=[], files=None):
- assert isinstance(send_to, list)
- gmail_user = 'redacted'
- gmail_password = 'redacted'
- msg = MIMEMultipart('alternative')
- msg['From'] = send_from
- msg['To'] = COMMASPACE.join(send_to)
- msg['Cc'] = COMMASPACE.join(cc)
- msg['Bcc'] = COMMASPACE.join(bcc)
- msg['Date'] = formatdate(localtime=True)
- msg['Subject'] = subject
- msg.attach(MIMEText(text, 'html'))
- for f in files or []:
- with open(f, "rb") as fil:
- if f.endswith('.pdf'):
- part = MIMEApplication(
- fil.read(),
- _subtype = "pdf",
- Name=basename(f)
- )
- else:
- part = MIMEApplication(
- fil.read(),
- Name=basename(f)
- )
- part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
- msg.attach(part)
- smtp = smtplib.SMTP_SSL('smtp.gmail.com', 465)
- smtp.ehlo()
- smtp.login(gmail_user, gmail_password)
- smtp.sendmail(send_from, send_to+cc+bcc, msg.as_bytes())
- smtp.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement