add send to mail
This commit is contained in:
parent
bc27460564
commit
c0f6a46f2a
|
@ -80,6 +80,19 @@ class ConfigGit:
|
|||
username: str = os.getenv("GIT_USERNAME")
|
||||
token: str = os.getenv("GIT_TOKEN")
|
||||
|
||||
@dataclass
|
||||
class ConfigMail:
|
||||
send: bool = config["mail"].getboolean("send")
|
||||
if send:
|
||||
user: str = os.getenv('MAIL_USER')
|
||||
pwd: str = os.getenv('MAIL_PWD')
|
||||
server: str = config['mail'].get('server')
|
||||
port: int = config['mail'].getint('port')
|
||||
from_address : str = config['mail'].get('from_address')
|
||||
to_address : str = config['mail'].get('to_address')
|
||||
subject : str = config['mail'].get('subject')
|
||||
|
||||
|
||||
|
||||
@dataclass()
|
||||
class ConfigAll:
|
||||
|
@ -88,6 +101,7 @@ class ConfigAll:
|
|||
bcp: ConfigBcp = ConfigBcp
|
||||
net_dev: ConfigNetDev = ConfigNetDev
|
||||
git: ConfigGit = ConfigGit
|
||||
mail: ConfigMail = ConfigMail
|
||||
|
||||
|
||||
cfg = ConfigAll
|
||||
|
|
|
@ -8,4 +8,10 @@ NET_DEV_PWD=password
|
|||
# название токена от для подключения к репозиторию
|
||||
GIT_USERNAME=git_user
|
||||
# токен для подключения к репозиторию
|
||||
GIT_TOKEN=bla-bla-lba
|
||||
GIT_TOKEN=bla-bla-lba
|
||||
|
||||
|
||||
# пользователь и пароль для отправки почты
|
||||
MAIL_USER=mail_user
|
||||
# токен для подключения к репозиторию
|
||||
MAIL_PWD=bla-bla-lba
|
|
@ -3,6 +3,16 @@
|
|||
# False \ True - включение\отключение отправки оповещения в телеграм
|
||||
send: False
|
||||
|
||||
|
||||
[mail]
|
||||
# False \ True - включение\отключение отправки оповещения на почту
|
||||
send: False
|
||||
server: smtp.example.com
|
||||
port: 25
|
||||
from_address: net-bcp@example.com
|
||||
to_address: engineer@example.com, network_manager@example.com
|
||||
subject: Warning in backup network device
|
||||
|
||||
# Конфиг гит репозитория
|
||||
[git]
|
||||
# False \ True - включение\отключение отправки файлов в удалённый репозиторий
|
||||
|
|
10
app/main.py
10
app/main.py
|
@ -7,10 +7,11 @@ from backup import (
|
|||
save_cisco_sb_bcp,
|
||||
save_cisco_bcp,
|
||||
)
|
||||
from messages import send_mail
|
||||
from repo import check_bcp_repo, push_to_remote
|
||||
import schedule
|
||||
import time
|
||||
|
||||
import os
|
||||
|
||||
def main():
|
||||
check_bcp_repo()
|
||||
|
@ -40,6 +41,13 @@ def main():
|
|||
save_cisco_bcp(host=current_device["ip"], name=current_device["name"])
|
||||
if cfg.git.push:
|
||||
push_to_remote()
|
||||
if cfg.mail.send:
|
||||
log_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "logs")
|
||||
file_path: str = os.path.join(log_dir, "logfile.log")
|
||||
if os.path.isfile(file_path):
|
||||
with open(file_path, 'r', encoding='utf-8') as file:
|
||||
text = file.read()
|
||||
send_mail(text)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
import smtplib
|
||||
from email.message import EmailMessage
|
||||
import logging as log
|
||||
from config import cfg
|
||||
|
||||
def send_mail(text: str) -> None:
|
||||
log.info('sending a message')
|
||||
msg = EmailMessage()
|
||||
msg['Subject'] = cfg.mail.subject
|
||||
msg['From'] = cfg.mail.from_address
|
||||
msg['To'] = cfg.mail.to_address
|
||||
msg.set_content(text)
|
||||
try:
|
||||
smtp = smtplib.SMTP(cfg.mail.server, cfg.mail.port)
|
||||
smtp.starttls()
|
||||
smtp.ehlo()
|
||||
smtp.login(cfg.mail.user, cfg.mail.pwd)
|
||||
|
||||
smtp.send_message(msg)
|
||||
smtp.quit()
|
||||
log.info('message sent successfully')
|
||||
except smtplib.SMTPException as err:
|
||||
log.warning(err)
|
Loading…
Reference in New Issue