55 lines
1.4 KiB
Python
55 lines
1.4 KiB
Python
import os
|
|
import smtplib
|
|
import ssl
|
|
|
|
from email.message import EmailMessage
|
|
|
|
from config import Config
|
|
from log import log
|
|
|
|
|
|
def ask_yes_no(title: str, question: str) -> bool:
|
|
"""Ask question; return True for yes, False for no"""
|
|
|
|
button_reply = QMessageBox.question(None, title, question)
|
|
|
|
return button_reply == QMessageBox.Yes
|
|
|
|
|
|
def send_mail(to_addr, from_addr, subj, body):
|
|
# From https://docs.python.org/3/library/email.examples.html
|
|
|
|
# Create a text/plain message
|
|
msg = EmailMessage()
|
|
msg.set_content(body)
|
|
|
|
msg['Subject'] = subj
|
|
msg['From'] = from_addr
|
|
msg['To'] = to_addr
|
|
|
|
# Send the message via SMTP server.
|
|
context = ssl.create_default_context()
|
|
try:
|
|
s = smtplib.SMTP(host=Config.MAIL_SERVER, port=Config.MAIL_PORT)
|
|
if Config.MAIL_USE_TLS:
|
|
s.starttls(context=context)
|
|
if Config.MAIL_USERNAME and Config.MAIL_PASSWORD:
|
|
s.login(Config.MAIL_USERNAME, Config.MAIL_PASSWORD)
|
|
s.send_message(msg)
|
|
except Exception as e:
|
|
print(e)
|
|
finally:
|
|
s.quit()
|
|
|
|
|
|
def show_OK(title: str, msg: str) -> None:
|
|
"""Display a message to user"""
|
|
|
|
QMessageBox.information(None, title, msg, buttons=QMessageBox.Ok)
|
|
|
|
|
|
def show_warning(title: str, msg: str) -> None:
|
|
"""Display a warning to user"""
|
|
|
|
QMessageBox.warning(None, title, msg, buttons=QMessageBox.Cancel)
|