Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import argparse
- import logging
- import smtplib
- import sys
- logging.basicConfig(stream=sys.stdout, level=logging.INFO)
- logger = logging.getLogger(__name__)
- def parse_args():
- parser = argparse.ArgumentParser(
- description='mailtest!')
- parser.add_argument('-v', '--verbose', action='store_true')
- args = parser.parse_args()
- return args
- if __name__ == '__main__':
- args = parse_args()
- if args.verbose:
- logger.level = logging.DEBUG
- logger.debug('debug enabled for {debug}'.format(debug=args.verbose))
- message_template = str()
- message_template += "From: Test <{mail_from}>\n"
- message_template += "To: <{recipient}>\n"
- message_template += "Subject: {subject}"
- message_template += '\n\n{message_body}'
- # EDIT THESE SETTINGS HERE
- #############################################################
- recipients = ['user1@example.com', 'user2@example.com']
- subject = 'A subject!'
- body = 'The message body'
- SMTP_HOST = 'smtp.example.com'
- SMTP_PORT = '587'
- SMTP_USER = 'username_possible_including_@example.com'
- SMTP_PASS = 'secret_password'
- #############################################################
- message = message_template.format(
- mail_from=SMTP_USER,
- recipient=','.join(recipients),
- subject=subject,
- message_body=body)
- logger.debug('using these mail details:')
- logger.debug(' * SMTP_HOST: {smtp_host}'.format(smtp_host = SMTP_HOST))
- logger.debug(' * SMTP_PORT: {smtp_port}'.format(smtp_port = SMTP_PORT))
- logger.debug(' * SMTP_USER: {smtp_user}'.format(smtp_user = SMTP_USER))
- logger.debug(' * SMTP_PASS: {smtp_pass}'.format(smtp_pass = SMTP_PASS))
- logger.debug('instantiating SMTP')
- smtpObj = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
- logger.debug('starttls')
- smtpObj.starttls()
- logger.debug('authentication')
- smtpObj.login(SMTP_USER, SMTP_PASS)
- logger.info('sending mail to:')
- for r in recipients:
- logger.info(' * {r}'.format(r=r))
- smtpObj.sendmail(SMTP_USER, recipients, message)
- logger.info('exiting!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement