Guest User

Untitled

a guest
Dec 15th, 2018
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. import time
  2. import smtplib
  3. import threading
  4.  
  5. from email import encoders
  6. from email.mime.multipart import MIMEMultipart
  7. from email.mime.base import MIMEBase
  8. from email.mime.text import MIMEText
  9.  
  10. SMTP_HOST = "smtp.mail.yahoo.com"
  11. SMTP_PORT = 587
  12. SMTP_ACCO = "xxxxxxxxxxx@yahoo.com"
  13. SMTP_PASS = "xxxxxxxxxxx"
  14.  
  15. FROM_ADDR = SMTP_ACCO
  16.  
  17. class myThread(threading.Thread):
  18. def __init__(self, toaddr):
  19. threading.Thread.__init__(self)
  20. self.toaddr = toaddr
  21.  
  22. def run(self):
  23. threadLock.acquire()
  24. sendTo(self.toaddr)
  25. threadLock.release()
  26.  
  27. def sendTo(toaddr):
  28. outer = MIMEMultipart()
  29. outer['From'] = FROM_ADDR
  30. outer['To'] = toaddr
  31. outer['Subject'] = "Python email"
  32.  
  33. body = "Hi, \nPython test mail."
  34. outer.attach(MIMEText(body, 'plain'))
  35.  
  36. # attach file
  37. fp = open('file.pdf', 'rb')
  38. msg = MIMEBase('application', 'pdf')
  39. msg.set_payload(fp.read())
  40. fp.close()
  41.  
  42. # Encode the payload
  43. encoders.encode_base64(msg)
  44.  
  45. # Set the filename parameter
  46. msg.add_header('Content-Disposition', 'attachment', filename='file.pdf')
  47. outer.attach(msg)
  48. compose = outer.as_string()
  49.  
  50. # Send the mail
  51. server = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
  52. server.set_debuglevel(False) # show communicationwith the server
  53.  
  54. #Next, log in to the server
  55. server.ehlo()
  56. server.starttls()
  57. server.ehlo()
  58. server.login(SMTP_ACCO, SMTP_PASS)
  59.  
  60. server.sendmail(FROM_ADDR, toaddr, compose)
  61.  
  62. # close server
  63. server.quit()
  64.  
  65. def main():
  66. # start time
  67. start = time.time()
  68.  
  69. # send email to list recipients file
  70. f = open('recipients','r')
  71. for addr in f.readlines():
  72. t = myThread(addr)
  73. t.start()
  74. threads.append(t)
  75. f.close()
  76.  
  77. # Wait for all threads to complete
  78. for t in threads:
  79. t.join()
  80.  
  81. print('Time: ', time.time() - start)
  82.  
  83. # init threadlock
  84. threadLock = threading.Lock()
  85. threads = []
  86.  
  87. if __name__ == '__main__':
  88. main()
Add Comment
Please, Sign In to add comment