-
Notifications
You must be signed in to change notification settings - Fork 0
/
emailops.py
37 lines (31 loc) · 1.02 KB
/
emailops.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
from os.path import basename
import smtplib
from smtplib import SMTPException
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
def send_mail(send_from, send_from_pass, send_to, subject, text, files=None, server="smtp.gmail.com"):
assert isinstance(send_to, list)
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
for f in files or []:
with open(f, "rb") as fil:
part = MIMEApplication(
fil.read(),
Name=basename(f)
)
# After the file is closed
part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
msg.attach(part)
try:
ssl_server = smtplib.SMTP_SSL(server, 465)
ssl_server.login(send_from, send_from_pass)
ssl_server.sendmail(send_from, send_to, msg.as_string())
ssl_server.close()
except SMTPException as e:
print(e)