Advertisement
Guest User

Untitled

a guest
May 3rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 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. files = []
  9.  
  10. #Credentials
  11. sender_username = 'pythonmailertst@gmail.com'
  12. sender_password = 'testacc99'
  13. recipient_username = 'nasifistiak@gmail.com'
  14. subject = 'Python Test Email'
  15. body = 'Hello World.'
  16.  
  17. fileDirectory = "FilesToSend/"
  18. numberOfFilesToPick = 2
  19. temp = []
  20.  
  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.  
  30. msgInit()
  31.  
  32. server = smtplib.SMTP('smtp.gmail.com', 587)
  33. server.starttls()
  34. server.login(sender_username, sender_password)
  35.  
  36. def messageSenderFunc():
  37. text = msg.as_string()
  38. #server.sendmail(sender_username, recipient_username, text)
  39. msgInit()
  40. print(temp)
  41. temp.clear()
  42.  
  43. def combinationUtil(arr, temp, start, end, index, r):
  44.  
  45. if (index == r):
  46. for j in temp:
  47. msg.attach(j)
  48. messageSenderFunc()
  49. return
  50.  
  51. i = start
  52. while (i <= end and end - i + 1 >= r - index):
  53. temp.append(arr[i])
  54. combinationUtil(arr, temp, i + 1, end, index + 1, r)
  55. i+=1
  56.  
  57. fileCount=0
  58.  
  59. for filename in os.listdir(fileDirectory):
  60. attachment = open(fileDirectory+filename, 'rb')
  61. part = MIMEBase('application', 'octet-stream')
  62. part.set_payload(attachment.read())
  63. encoders.encode_base64(part)
  64. part.add_header('Content-Disposition', 'attachment; filename= ' + filename)
  65. files.append(part)
  66. fileCount += 1
  67.  
  68. combinationUtil(files, temp, 0, fileCount-1, 0, numberOfFilesToPick)
  69. server.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement