mirror of
https://github.com/isjerryxiao/pacroller.git
synced 2024-11-26 01:30:42 +08:00
add test-smtp command
This commit is contained in:
parent
c9fb39c42c
commit
2a716bef60
3 changed files with 21 additions and 5 deletions
|
@ -24,7 +24,12 @@ else:
|
|||
_config = dict()
|
||||
|
||||
if (smtp_cfg := (CONFIG_DIR / CONFIG_FILE_SMTP)).exists():
|
||||
_smtp_config: dict = json.loads(smtp_cfg.read_text())
|
||||
try:
|
||||
_smtp_cfg_text = smtp_cfg.read_text()
|
||||
except PermissionError:
|
||||
_smtp_config = dict()
|
||||
else:
|
||||
_smtp_config: dict = json.loads(_smtp_cfg_text)
|
||||
else:
|
||||
_smtp_config = dict()
|
||||
|
||||
|
|
|
@ -17,9 +17,9 @@ class MailSender:
|
|||
self.mailfrom = SMTP_FROM
|
||||
self.mailto = SMTP_TO.split()
|
||||
self.smtp_cls = smtplib.SMTP_SSL if self.ssl else smtplib.SMTP
|
||||
def send_text_plain(self, text: str, subject: str = f"pacroller from {hostname}", mailto: List[str] = list()) -> None:
|
||||
def send_text_plain(self, text: str, subject: str = f"pacroller on {hostname}", mailto: List[str] = list()) -> bool:
|
||||
if not SMTP_ENABLED:
|
||||
return
|
||||
return None
|
||||
for _ in range(NETWORK_RETRY):
|
||||
try:
|
||||
server = self.smtp_cls(self.host, self.port)
|
||||
|
@ -40,6 +40,8 @@ class MailSender:
|
|||
break
|
||||
else:
|
||||
logger.error(f"unable to send email after {NETWORK_RETRY} attempts {text=}")
|
||||
return False
|
||||
return True
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(module)s - %(funcName)s - %(levelname)s - %(message)s')
|
||||
|
|
|
@ -264,8 +264,8 @@ def main() -> None:
|
|||
logger.debug(f'needrestart {p.stdout=}')
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser(description='Unattended Upgrades for Arch Linux')
|
||||
parser.add_argument('action', choices=['run', 'status', 'reset', 'fail-reset', 'reset-failed'],
|
||||
help="what to do", metavar="run / status / reset ")
|
||||
parser.add_argument('action', choices=['run', 'status', 'reset', 'fail-reset', 'reset-failed', 'test-smtp'],
|
||||
help="what to do", metavar="run / status / reset / test-smtp")
|
||||
parser.add_argument('-d', '--debug', action='store_true', help='enable debug mode')
|
||||
parser.add_argument('-v', '--verbose', action='store_true', help='show verbose report')
|
||||
parser.add_argument('-m', '--max', type=int, default=1, help='Number of upgrades to show')
|
||||
|
@ -321,6 +321,15 @@ def main() -> None:
|
|||
if PACMAN_SCC:
|
||||
clear_pkg_cache()
|
||||
|
||||
elif args.action == 'test-smtp':
|
||||
logger.info('sending test mail...')
|
||||
if _smtp_result := MailSender().send_text_plain("This is a test mail\nIf you see this email, your smtp config is working."):
|
||||
logger.info("success")
|
||||
elif _smtp_result is None:
|
||||
logger.warning("smtp is disabled")
|
||||
else:
|
||||
logger.error("fail")
|
||||
|
||||
elif args.action == 'status':
|
||||
count = 0
|
||||
failed = False
|
||||
|
|
Loading…
Reference in a new issue