Advertisement
Typhoon

Send Mass Email with Python

Apr 12th, 2015
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import smtplib
  5. from email.mime.multipart import MIMEMultipart
  6. from email.mime.text import MIMEText
  7. from email.header import Header
  8. from email.utils import formataddr
  9.  
  10. # Import TEXT only File as Body
  11. t = open('textmsg.txt', 'r')
  12. tmsgfile = t.read()
  13. # Import HTML only File as Body
  14. h = open('htmlmsg.txt', 'r')
  15. hmsgfile = h.read()
  16. # Import E-Mail addresses from file. Separate mail@ddress per line
  17. addr_f = open('mails.txt', 'r')
  18. addr_list = []
  19.  
  20. for line in addr_f:
  21.     addr_list.append(str((line).replace("\n","")))
  22. #addr_list.sort()
  23. print addr_list
  24. print "\nE-Mails count : ", len(addr_list)
  25.  
  26. # Pick Up email recipient from list and send email to address
  27. counter = 1
  28. for mail in addr_list:
  29.     from_mail = formataddr((str(Header('www.mysite.sk', 'utf-8')), 'info@mysite.sk'))
  30.     to_mail = mail
  31.  
  32.     msg = MIMEMultipart('alternative')
  33.     msg['Content-Type'] = "text/html; charset=utf-8"
  34.     msg['Subject'] = Header('MySubject', 'utf-8')
  35.     msg['From'] = from_mail
  36.     msg['To'] = to_mail
  37.  
  38.     text = tmsgfile
  39.     html = hmsgfile
  40.     part1 = MIMEText(text, 'plain', "utf-8")
  41.     part2 = MIMEText(html, 'html', "utf-8")
  42.  
  43.     msg.attach(part1)
  44.     msg.attach(part2)
  45.  
  46.     s = smtplib.SMTP('smtp.server.sk:25')
  47.     s.starttls()
  48.     s.login('info@mysite.sk','MyPass')
  49.     s.sendmail(from_mail, to_mail, msg.as_string())
  50.  
  51.     print "\n", counter, "Message sent to :", mail
  52.     counter += 1
  53.    
  54. s.quit()
  55. print "\nFinish"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement