-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsend_email.py
39 lines (35 loc) · 1.21 KB
/
send_email.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
# -*- coding: utf-8 -*-
import re
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
def send_email(server, port, ssl, user, password, to, subject, body):
"""Send html or text email.
Args:
server (str): SMTP server.
port (int): Port.
ssl (bool): True if use ssl and False if use tls.
user (str): User address.
password (str): Password to authenticate.
to (str/list): Recipients.
subject (str): Subject.
body (str): Message, can be string or html.
"""
if ssl:
smtp_server = smtplib.SMTP_SSL(server, port)
else:
smtp_server = smtplib.SMTP(server, port)
smtp_server.ehlo()
smtp_server.starttls()
smtp_server.ehlo()
smtp_server.login(user, password)
types = 'plain'
regex_html = '(?i)<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|\'.*?\'|[^\'">\s]+))?)+\s*|\s*)\/?>'
if re.match(regex_html, body):
types = 'html'
msg = MIMEMultipart('alternative')
msg['Subject'] = Header(subject, 'utf-8')
msg.attach(MIMEText(body, types, 'utf-8'))
smtp_server.sendmail(user, to, msg.as_string())
smtp_server.close()