testmail ¶
Save into '/usr/local/bin/testmail', then:
chmod +x /usr/local/bin/testmail
#!/usr/bin/env python3 import os import configparser from pathlib import Path import argparse def run_command(command, dry_run): print("\x1b[1;37;40m" + command + "\x1b[0m") if not dry_run: os.system(command) class Config(): def __init__(self): self._config = configparser.ConfigParser() if not self.get_config_filename().exists(): self._config['general'] = { 'counter': 1, } self.save_config() print(f"Default config written to {self.get_config_filename()}") self.read_config() def get_config_filename(self): return Path(__file__).parent / "testmail.ini" def read_config(self): self._config.read(self.get_config_filename()) def save_config(self): with open(self.get_config_filename(), 'w') as f: self._config.write(f) def get_counter(self, increment=True): value = self._config.getint('general', 'counter') if increment: self.increment_counter() return value def increment_counter(self): self._config['general']['counter'] = str(self.get_counter(increment=False) + 1) self.save_config() def reset_counter(self): self._config['general']['counter'] = '1' self.save_config() if __name__ == '__main__': parser = argparse.ArgumentParser(description="Sends a test email to the specified recipient") parser.add_argument('recipient') parser.add_argument('--dry-run', '-d', action='store_true') args = parser.parse_args() config = Config() counter = config.get_counter(increment=not args.dry_run) command = 'echo "Test mail %d; created on: `date`" | mail -s "Test mail %d" %s' % ( counter, counter, args.recipient, ) run_command(command, args.dry_run)