Advertisement
Guest User

Untitled

a guest
May 3rd, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. import smtplib
  2. from email.mime.text import MIMEText
  3. from email.mime.multipart import MIMEMultipart
  4. from email.mime.base import MIMEBase
  5. from email import encoders
  6. import os
  7.  
  8. # Credentials
  9. sender_username = 'pythonmailertst@gmail.com'
  10. sender_password = 'testacc99'
  11. recipient_username = 'nasifistiak@gmail.com'
  12. subject = 'Python Test Email'   # Subject of emall
  13. body = 'Hello World.'           # Body (text) of email
  14.  
  15. # Options
  16. fileDirectory = "FilesToSend/"  # Put files to send in this folder, or change the folder to something else
  17. numberOfFilesToPick = 3         # Select how many files you want to send here
  18.  
  19. files = []
  20. temp = ['0'] * numberOfFilesToPick
  21. msg = MIMEMultipart()
  22. def msgInit():
  23.     global msg
  24.     msg = MIMEMultipart()
  25.     msg['From'] = sender_username
  26.     msg['To'] = recipient_username
  27.     msg['Subject'] = subject
  28.     msg.attach((MIMEText(body, 'plain')))
  29. msgInit()
  30.  
  31. server = smtplib.SMTP('smtp.gmail.com', 587)
  32. server.starttls()
  33. server.login(sender_username, sender_password)
  34.  
  35. def messageSenderFunc():
  36.     text = msg.as_string()
  37.     server.sendmail(sender_username, recipient_username, text)
  38.     global temp
  39.     msgInit()
  40.  
  41. def combinationUtil(arr, temp, start, end, index, r):
  42.  
  43.     if (index == r):
  44.         for j in temp:
  45.             msg.attach(j)
  46.         messageSenderFunc()
  47.         return
  48.  
  49.     i = start
  50.     while (i <= end and end - i + 1 >= r - index):
  51.         temp[index] = arr[i]
  52.         combinationUtil(arr, temp, i + 1, end, index + 1, r)
  53.         i+=1
  54.  
  55. for filename in os.listdir(fileDirectory):
  56.     attachment = open(fileDirectory+filename, 'rb')
  57.     part = MIMEBase('application', 'octet-stream')
  58.     part.set_payload(attachment.read())
  59.     encoders.encode_base64(part)
  60.     part.add_header('Content-Disposition', 'attachment; filename= ' + filename)
  61.     files.append(part)
  62.  
  63. fileCount = len(files)
  64. combinationUtil(files, temp, 0, fileCount-1, 0, numberOfFilesToPick)
  65. server.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement