Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import argparse
  5. import smtplib
  6. from email.mime.text import MIMEText
  7. from email.mime.multipart import MIMEMultipart
  8.  
  9. # Edit these defaults, as needed
  10. # If you want to get your defaults from environment variables, and/or if you want to hardcode a new default
  11. # edit this code like:
  12. # ```
  13. # import os
  14. #
  15. # DEFAULT_FROM = os.environ.get("EMAIL_FROM_ADDRESS") or "example@example.com"
  16. # ```
  17.  
  18. DEFAULT_HOST = "smtp.mailgun.org"
  19. DEFAULT_PORT = 587
  20. DEFAULT_USER = None
  21. DEFAULT_PASSWORD = None
  22. DEFAULT_FROM = None
  23.  
  24. DEFAULT_SUBJECT = None
  25. DEFAULT_TO = None
  26. DEFAULT_CC = None
  27. DEFAULT_BCC = None
  28. DEFAULT_HTML_FILE = None
  29. DEFAULT_PLAIN_FILE = None
  30.  
  31. VERBOSITY_NONE = 0
  32. VERBOSITY_LOW = 1
  33. VERBOSITY_HIGH = 2
  34.  
  35. DESCRIPTION = """Send SMTP email from the command line"""
  36.  
  37. EPILOG = """Thank you for using the software"""
  38.  
  39. def main():
  40. argParser = argparse.ArgumentParser(description=DESCRIPTION, epilog=EPILOG, formatter_class=argparse.RawDescriptionHelpFormatter)
  41.  
  42. argParser.add_argument("-H", "--host", help="Sets the 'host' field. Default to '{0}'".format(DEFAULT_HOST if DEFAULT_HOST is not None else ""), default=DEFAULT_HOST)
  43. argParser.add_argument("-p", "--port", type=int, help="Sets the 'port' field. Default to '{0}'".format(DEFAULT_PORT if DEFAULT_PORT is not None else ""), default=DEFAULT_PORT)
  44. argParser.add_argument("-u", "--user", help="Sets the 'userName' field. Default to '{0}'".format(DEFAULT_USER if DEFAULT_USER is not None else ""), default=DEFAULT_USER)
  45. argParser.add_argument("-P", "--password", help="Sets the 'password' field. Default to '{0}'".format(DEFAULT_PASSWORD if DEFAULT_PASSWORD is not None else ""), default=DEFAULT_PASSWORD)
  46. argParser.add_argument("-f", "--fromAddress", help="Sets the 'fromAddress' field. Default to '{0}'".format(DEFAULT_FROM if DEFAULT_FROM is not None else ""), default=DEFAULT_FROM)
  47. argParser.add_argument("-s", "--subject", help="Sets the 'subject' field. Default to '{0}'".format(DEFAULT_SUBJECT if DEFAULT_SUBJECT is not None else ""), default=DEFAULT_SUBJECT)
  48. argParser.add_argument("-t", "--to", help="Sets the 'toList' field. Formatted as a comma-separated list of emails. Default to '{0}'".format(DEFAULT_TO if DEFAULT_TO is not None else ""), default=DEFAULT_TO)
  49. argParser.add_argument("-c", "--cc", help="Sets the 'ccList' field. Formatted as a comma-separated list of emails. Default to '{0}'".format(DEFAULT_CC if DEFAULT_CC is not None else ""), default=DEFAULT_CC)
  50. argParser.add_argument("-b", "--bcc", help="Sets the 'bccList' field. Formatted as a comma-separated list of emails. Default to '{0}'".format(DEFAULT_BCC if DEFAULT_BCC is not None else ""), default=DEFAULT_BCC)
  51. argParser.add_argument("-n", "--htmlFile", help="Sets the 'htmlFile' field. Default to '{0}'".format(DEFAULT_HTML_FILE if DEFAULT_HTML_FILE is not None else ""), default=DEFAULT_HTML_FILE)
  52. argParser.add_argument("-N", "--plainFile", help="Sets the 'plainFile' field. Default to '{0}'".format(DEFAULT_PLAIN_FILE if DEFAULT_PLAIN_FILE is not None else ""), default=DEFAULT_PLAIN_FILE)
  53.  
  54. argParser.add_argument("-v", "--verbosity", type=int, help="write info messages to standard output", default=VERBOSITY_NONE)
  55. argParser.add_argument("-d", "--dryRun", help="Program runs normally, only mail is not sent", action="store_true")
  56.  
  57. args = argParser.parse_args()
  58.  
  59. verbosity = args.verbosity
  60. dryRun = args.dryRun
  61.  
  62. host = args.host
  63. port = args.port
  64.  
  65. userName = args.user
  66. password = args.password
  67.  
  68. if (userName and not password) or (not userName and password):
  69. print "Username and Password must be specified together"
  70. sys.exit(1)
  71.  
  72. subject = args.subject
  73. if subject is None:
  74. print "Subject must be specified"
  75. sys.exit(1)
  76.  
  77. fromAddress = args.fromAddress
  78. if fromAddress is None:
  79. print "From Address must be specified"
  80. sys.exit(1)
  81.  
  82. toList = args.to.split(",") if args.to is not None else []
  83. ccList = args.cc.split(",") if args.cc is not None else []
  84. bccList = args.bcc.split(",") if args.bcc is not None else []
  85.  
  86. recipents = toList + ccList + bccList
  87.  
  88. if len(recipents) < 1:
  89. print "No recipents specified"
  90. sys.exit(1)
  91.  
  92. htmlFile = args.htmlFile
  93. htmlPart = None
  94. try:
  95. if htmlFile:
  96. with open(htmlFile, "r") as fp:
  97. html = fp.read()
  98. htmlPart = MIMEText(html, "html")
  99. except Exception as e:
  100. print "Could not read HTML file"
  101. if verbosity >= VERBOSITY_LOW:
  102. print e
  103. sys.exit(1)
  104.  
  105. plainFile = args.plainFile
  106. plainPart = None
  107. try:
  108. if plainFile:
  109. with open(plainFile, "r") as fp:
  110. plain = fp.read()
  111. plainPart = MIMEText(plain, "plain")
  112. except Exception as e:
  113. print "Could not read plain file"
  114. if verbosity >= VERBOSITY_LOW:
  115. print e
  116. sys.exit(1)
  117.  
  118. if not htmlPart and not plainPart:
  119. print "No message (html or plain) specified"
  120. sys.exit(1)
  121.  
  122. msg = MIMEMultipart("alternative")
  123. msg["Subject"] = subject
  124. msg["From"] = fromAddress
  125. msg["To"] = ",".join(toList)
  126. msg["Cc"] = ",".join(ccList)
  127. msg["Bcc"] = ",".join(bccList)
  128.  
  129. if htmlPart:
  130. msg.attach(htmlPart)
  131. if plainPart:
  132. msg.attach(plainPart)
  133.  
  134. s = smtplib.SMTP(host, port)
  135.  
  136. if verbosity >= VERBOSITY_HIGH:
  137. s.set_debuglevel(1)
  138.  
  139. try:
  140. if userName and password:
  141. s.login(userName, password)
  142.  
  143. if not dryRun:
  144. s.sendmail(fromAddress, recipents, msg.as_string())
  145. except Exception as e:
  146. print "Error sending email"
  147. if verbosity >= VERBOSITY_LOW:
  148. print e
  149.  
  150. s.quit()
  151.  
  152. if __name__ == "__main__":
  153. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement