Advertisement
Guest User

Untitled

a guest
Jan 9th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import argparse
  4. import logging
  5. import smtplib
  6. import sys
  7.  
  8. logging.basicConfig(stream=sys.stdout, level=logging.INFO)
  9. logger = logging.getLogger(__name__)
  10.  
  11. def parse_args():
  12.     parser = argparse.ArgumentParser(
  13.             description='mailtest!')
  14.  
  15.     parser.add_argument('-v', '--verbose', action='store_true')
  16.  
  17.     args = parser.parse_args()
  18.     return args
  19.  
  20. if __name__ == '__main__':
  21.     args = parse_args()
  22.  
  23.     if args.verbose:
  24.         logger.level = logging.DEBUG
  25.  
  26.     logger.debug('debug enabled for {debug}'.format(debug=args.verbose))
  27.  
  28.     message_template = str()
  29.     message_template += "From: Test <{mail_from}>\n"
  30.     message_template += "To: <{recipient}>\n"
  31.     message_template += "Subject: {subject}"
  32.     message_template += '\n\n{message_body}'
  33.  
  34.     # EDIT THESE SETTINGS HERE
  35.     #############################################################
  36.     recipients = ['user1@example.com', 'user2@example.com']
  37.     subject = 'A subject!'
  38.     body = 'The message body'
  39.  
  40.     SMTP_HOST = 'smtp.example.com'
  41.     SMTP_PORT = '587'
  42.     SMTP_USER = 'username_possible_including_@example.com'
  43.     SMTP_PASS = 'secret_password'
  44.     #############################################################
  45.  
  46.     message = message_template.format(
  47.             mail_from=SMTP_USER,
  48.             recipient=','.join(recipients),
  49.             subject=subject,
  50.             message_body=body)
  51.  
  52.     logger.debug('using these mail details:')
  53.     logger.debug(' * SMTP_HOST: {smtp_host}'.format(smtp_host = SMTP_HOST))
  54.     logger.debug(' * SMTP_PORT: {smtp_port}'.format(smtp_port = SMTP_PORT))
  55.     logger.debug(' * SMTP_USER: {smtp_user}'.format(smtp_user = SMTP_USER))
  56.     logger.debug(' * SMTP_PASS: {smtp_pass}'.format(smtp_pass = SMTP_PASS))
  57.  
  58.     logger.debug('instantiating SMTP')
  59.     smtpObj = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
  60.     logger.debug('starttls')
  61.     smtpObj.starttls()
  62.     logger.debug('authentication')
  63.     smtpObj.login(SMTP_USER, SMTP_PASS)
  64.     logger.info('sending mail to:')
  65.     for r in recipients:
  66.         logger.info('  * {r}'.format(r=r))
  67.     smtpObj.sendmail(SMTP_USER, recipients, message)
  68.  
  69.     logger.info('exiting!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement