-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmimemail.py
51 lines (36 loc) · 1.2 KB
/
mimemail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
:Mod: mimemail
:Synopsis:
Provide MIME Multipart email support (see: https://realpython.com/python-send-email/)
:Author:
servilla
:Created:
4/3/22
"""
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formataddr
import smtplib
import daiquiri
import properties
logger = daiquiri.getLogger(__name__)
def send_mail(subject: str, msg: str) -> bool:
logger.info(f"{str}\n{msg}")
message = MIMEMultipart("alternative")
message["Subject"] = subject
message["From"] = formataddr((properties.FROM_NAME, properties.FROM))
message["To"] = formataddr((properties.TO_NAME, properties.TO))
message.add_header("X-SES-CONFIGURATION-SET", "edi-dedicated")
part = MIMEText(msg, "plain")
message.attach(part)
try:
with smtplib.SMTP(properties.RELAY_HOST, properties.RELAY_TLS_PORT) as server:
server.starttls()
server.login(properties.RELAY_USER, properties.RELAY_PASSWORD)
server.sendmail(properties.FROM, properties.TO, message.as_string())
return True
except Exception as e:
logger.error(e)
return False