SHARE
TWEET

send mail via command line with t-online mail

DeaD_EyE Aug 17th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2. import sys
  3. import smtplib
  4. from argparse import ArgumentParser
  5. from configparser import ConfigParser
  6. from email.message import EmailMessage
  7. from pathlib import Path
  8.  
  9.  
  10. def make_mail(body, subject, to_, creds):
  11.     msg = EmailMessage()
  12.     msg.set_content(body)
  13.     msg['Subject'] = subject
  14.     msg['From'] = creds['email']
  15.     msg['To'] = to_
  16.     return msg
  17.  
  18.  
  19. def send(message, creds, debug=False, verbose=False):
  20.     with smtplib.SMTP(creds['host'], creds['port']) as smtp:
  21.         if verbose:
  22.             smtp.set_debuglevel(1)
  23.         code, reply = smtp.starttls()
  24.         if debug:
  25.             print(reply.decode(), file=sys.stderr)
  26.         code, reply = smtp.login(creds['user'], creds['password'])
  27.         if debug:
  28.             print(reply.decode(), file=sys.stderr)
  29.         smtp.send_message(message)
  30.  
  31.  
  32. def load_config():
  33.     script_path = Path(sys.argv[0]).absolute().parent
  34.     config_name = '.email-creds'
  35.     config = script_path / config_name
  36.     cfg = ConfigParser()
  37.     if config.exists():
  38.         cfg.read([config])
  39.         return cfg
  40.     else:
  41.         cfg['email'] = {
  42.             'host': 'securesmtp.t-online.de',
  43.             'port': 587,
  44.             'timeout': 5,
  45.             'email': 'email@domain.tld',
  46.             'user': 'email@domain.tld',
  47.             'password': 'not set',
  48.             }
  49.         with config.open('w') as fd:
  50.             cfg.write(fd)
  51.             print('Written config to', config, file=sys.stderr)
  52.             print('Change the settings inside this config and execute the program again', file=sys.stderr)
  53.         return None
  54.  
  55.  
  56. def main(message, subject, to, debug=False, verbose=False):
  57.     cfg = load_config()
  58.     if cfg is None:
  59.         sys.exit(1)
  60.     msg = make_mail(message, subject, to, cfg['email'])
  61.     send(msg, cfg['email'], debug, verbose)
  62.  
  63.  
  64. if __name__ == '__main__':
  65.     parser = ArgumentParser()
  66.     parser.add_argument('message', help="Message you want to send")
  67.     parser.add_argument('subject', help="The subject of the message")
  68.     parser.add_argument('to', help="Receiver of the e-mail")
  69.     parser.add_argument('-d', '--debug', action='store_true', help='Show debug info about login and starttls.')
  70.     parser.add_argument('-v', '--verbose', action='store_true', help='Show messages form smtp server')
  71.     args = parser.parse_args()
  72.     main(**vars(args))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top