Skip to content
Snippets Groups Projects
Commit 5dacb367 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

added MailHandler

parent 5ebfb82c
Branches
No related tags found
No related merge requests found
import smtplib
import ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from typing import List, Dict
class MailHandler:
@staticmethod
def send_from_config(config: Dict, subject: str, content: str):
MailHandler.send(config['host'],
config['port'],
config['userName'],
config['password'],
config['receiverMails'],
subject,
content)
@staticmethod
def send(host: str, port: int, userName: str, password: str, receiverMails: List[str], subject: str, content: str):
message = MIMEMultipart('alternative')
message['Subject'] = subject
message['From'] = userName
message['To'] = receiverMails
# Create the plain-text and HTML version of your message
text = subject
html = '''\
<html>
<body>
<h2>{subject}</h2>
{content}
</body>
</html>
'''.format(subject=subject, content=content)
# Turn these into plain/html MIMEText objects
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Add HTML/plain-text parts to MIMEMultipart message
# The email client will try to render the last part first
message.attach(part1)
message.attach(part2)
# Create secure connection with server and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL(host, port, context=context) as server:
server.login(userName, password)
server.sendmail(userName, receiverMails, message.as_string())
from PythonLibs.DefaultLogger import DefaultLogger
from PythonLibs.MailHandler import MailHandler
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment