Advertisement
Guest User

Untitled

a guest
Jan 29th, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. import smtplib
  2. from os.path import basename
  3. from email.mime.application import MIMEApplication
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. from email.utils import COMMASPACE, formatdate
  7.  
  8.  
  9. def send_mail(send_from, send_to, subject, text, cc=[], bcc=[], files=None):
  10.     assert isinstance(send_to, list)
  11.    
  12.     gmail_user = 'redacted'
  13.     gmail_password = 'redacted'
  14.  
  15.     msg = MIMEMultipart('alternative')
  16.     msg['From'] = send_from
  17.     msg['To'] = COMMASPACE.join(send_to)
  18.     msg['Cc'] = COMMASPACE.join(cc)
  19.     msg['Bcc'] = COMMASPACE.join(bcc)
  20.     msg['Date'] = formatdate(localtime=True)
  21.     msg['Subject'] = subject
  22.  
  23.     msg.attach(MIMEText(text, 'html'))
  24.  
  25.     for f in files or []:
  26.         with open(f, "rb") as fil:
  27.             if f.endswith('.pdf'):
  28.                 part = MIMEApplication(
  29.                     fil.read(),
  30.                     _subtype = "pdf",
  31.                     Name=basename(f)
  32.                 )
  33.             else:
  34.                 part = MIMEApplication(
  35.                 fil.read(),
  36.                 Name=basename(f)
  37.             )
  38.         part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
  39.         msg.attach(part)
  40.  
  41.  
  42.     smtp = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  43.     smtp.ehlo()
  44.     smtp.login(gmail_user, gmail_password)
  45.     smtp.sendmail(send_from, send_to+cc+bcc, msg.as_bytes())
  46.     smtp.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement