-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmailer.py
More file actions
40 lines (35 loc) · 1.06 KB
/
mailer.py
File metadata and controls
40 lines (35 loc) · 1.06 KB
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
#!/usr/bin/env python3
"""
This script is used to send emails
"""
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os
import smtplib
DEFAULT_FROM = os.environ.get('DEFAULT_FROM')
DEFAULT_SERVER = os.environ.get('DEFAULT_SERVER')
def send_mail(recipients, subject, text, cc):
"""
Sends email to recipients.
:param List recipients: recipients of email
:param String subject: subject of the email
:param String text: HTML text
:param String cc: cc of the email
:param String text: text of the email
:returns: Nothing
"""
_cfg = {}
_cfg.setdefault("server", DEFAULT_SERVER)
_cfg.setdefault("from", DEFAULT_FROM)
sender = _cfg["from"]
msg = MIMEMultipart('related')
msg["Subject"] = subject
msg["From"] = sender
msg["To"] = ", ".join(recipients)
if cc:
msg['Cc'] = ", ".join(cc)
server = smtplib.SMTP(_cfg["server"])
part = MIMEText(text, 'html', 'utf-8')
msg.attach(part)
server.sendmail(sender, recipients, msg.as_string())
server.quit()