Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from smtplib import SMTP
  3. from smtplib import SMTP_SSL
  4. from smtplib import SMTPException
  5. from email.mime.text import MIMEText
  6. import sys
  7.  
  8. #Global varialbes
  9. EMAIL_SUBJECT = "Email from Python script"
  10. EMAIL_RECEIVERS = ['receiverId@gmail.com']
  11. EMAIL_SENDER = 'senderId@yahoo.com'
  12. TEXT_SUBTYPE = "plain"
  13.  
  14. YAHOO_SMTP = "smtp.mail.yahoo.com"
  15. YAHOO_SMTP_PORT = 465
  16.  
  17. def listToStr(lst):
  18. """This method makes comma separated list item string"""
  19. return ','.join(lst)
  20.  
  21. def send_email(content, pswd):
  22. """This method sends an email"""
  23. msg = MIMEText(content, TEXT_SUBTYPE)
  24. msg["Subject"] = EMAIL_SUBJECT
  25. msg["From"] = EMAIL_SENDER
  26. msg["To"] = listToStr(EMAIL_RECEIVERS)
  27.  
  28. try:
  29. #Yahoo allows SMTP connection over SSL.
  30. smtpObj = SMTP_SSL(YAHOO_SMTP, YAHOO_SMTP_PORT)
  31. #If SMTP_SSL is used then ehlo and starttls call are not required.
  32. smtpObj.login(user=EMAIL_SENDER, password=pswd)
  33. smtpObj.sendmail(EMAIL_SENDER, EMAIL_RECEIVERS, msg.as_string())
  34. smtpObj.quit();
  35. except SMTPException as error:
  36. print "Error: unable to send email : {err}".format(err=error)
  37.  
  38. def main(pswd):
  39. """This is a simple main() function which demonstrates sending of email using smtplib."""
  40. send_email("Test email was generated by Python using smtplib and email libraries", pswd);
  41.  
  42. if __name__ == "__main__":
  43. """If this script is executed as stand alone then call main() function."""
  44. if len(sys.argv) == 2:
  45. main(sys.argv[1])
  46. else:
  47. print "Please provide password"
  48. sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement