Advertisement
TimGJ

Python send files to multiple recipients via Gmail

Nov 3rd, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. import email
  2. from email.mime.multipart import MIMEMultipart
  3. from email.mime.application import MIMEApplication
  4. import mimetypes
  5. import smtplib
  6. import datetime
  7.  
  8.  
  9. def GMailFile(filenames, MailRecipients, subject):
  10.  
  11.     GmailUser      = '<whatever>@gmail.com'
  12.     GmailPassword  = '<whatever>'
  13.    
  14.     d = datetime.date.today()
  15.     if type(filenames) is str:
  16.         filenames = [filenames]
  17.     if type(MailRecipients) is str:
  18.         MailRecipients = [MailRecipients]
  19.        
  20.     msg = MIMEMultipart()
  21.     msg['From'] = GmailUser
  22.     msg['To'] = ", ".join(MailRecipients)
  23.     msg['Subject'] = subject
  24.     for filename in filenames:
  25.         part = MIMEApplication(open(filename,"rb").read())
  26.         part.add_header('Content-Disposition', 'attachment', filename=filename)
  27.         msg.attach(part)
  28.     smtpserver = smtplib.SMTP('smtp.gmail.com:587')
  29.     smtpserver.ehlo()
  30.     smtpserver.starttls()
  31.     smtpserver.ehlo
  32.     smtpserver.login(GmailUser, GmailPassword)
  33.     smtpserver.sendmail(msg['From'], MailRecipients, msg.as_string())
  34.     smtpserver.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement