Advertisement
Guest User

Untitled

a guest
Mar 28th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #! /usr/bin/python
  2.  
  3. import sys
  4. import os
  5. import re
  6.  
  7. SMTPserver = 'smtp.gmail.com'
  8. SMPTport = 587
  9. sender = 'SENDER'
  10.  
  11. if len(sys.argv) == 1:
  12. destination = sender
  13. subject = "No subject."
  14. content = """\
  15. No message content.
  16. """
  17. if len(sys.argv) == 2:
  18. destination = sys.argv[1]
  19. subject = "No subject."
  20. content = """\
  21. No message content.
  22. """
  23. if len(sys.argv) == 3:
  24. destination = sys.argv[1]
  25. subject = sys.argv[2]
  26. content = """\
  27. No message content.
  28. """
  29. if len(sys.argv) == 4:
  30. destination = sys.argv[1]
  31. subject = sys.argv[2]
  32. content = sys.argv[3]
  33. if len(sys.argv) == 5:
  34. destination = sys.argv[1]
  35. subject = sys.argv[2]
  36. content = sys.argv[3]
  37. filename = sys.argv[4]
  38.  
  39. USERNAME = "USERNAME"
  40. PASSWORD = "PASSWORD"
  41.  
  42. # typical values for text_subtype are plain, html, xml
  43. text_subtype = 'plain'
  44.  
  45. #from smtplib import SMTP_SSL as SMTP # this invokes the secure SMTP protocol (port 465, uses SSL)
  46. from smtplib import SMTP # use this for standard SMTP protocol (port 25, no encryption)
  47.  
  48. # old version
  49. # from email.MIMEText import MIMEText
  50. from email.mime.text import MIMEText
  51. from email.mime.multipart import MIMEMultipart
  52.  
  53. try:
  54. msg = MIMEMultipart(content, text_subtype)
  55. msg['Subject']= subject
  56. msg['From'] = sender # some SMTP servers will do this automatically, not all
  57. conn = SMTP(SMTPserver, SMPTport)
  58. conn.ehlo()
  59. conn.starttls()
  60. conn.set_debuglevel(False)
  61. conn.login(USERNAME, PASSWORD)
  62. try:
  63. if len(sys.argv) == 5:
  64. f = open(filename)
  65. attachment = MIMEText(f.read())
  66. attachment.add_header('Content-Disposition', 'attachment', filename=filename)
  67. msg.attach(attachment)
  68. body = MIMEText(content, 'plain')
  69. msg.attach(body)
  70. conn.sendmail(sender, destination, msg.as_string())
  71. finally:
  72. conn.quit()
  73. print ("Message sent to", destination, "with subject", subject,".")
  74.  
  75. except Exception as e:
  76. sys.exit( "mail failed; %s" % str(e) ) # give a error message
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement