-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_utils.py
85 lines (73 loc) · 2.75 KB
/
email_utils.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# coding: utf-8
# Copyright 2011 Álvaro Justen [alvarojusten at gmail dot com]
# License: GPL <http://www.gnu.org/copyleft/gpl.html>
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from mimetypes import guess_type
from email.encoders import encode_base64
from getpass import getpass
from smtplib import SMTP
class Email(object):
def __init__(self, from_, to, subject, message, message_type='plain',
attachments=None):
self.email = MIMEMultipart()
self.email['From'] = from_
self.email['To'] = to
self.email['Subject'] = subject
text = MIMEText(message, message_type)
self.email.attach(text)
if attachments is not None:
for filename in attachments:
mimetype, encoding = guess_type(filename)
mimetype = mimetype.split('/', 1)
fp = open(filename, 'rb')
attachment = MIMEBase(mimetype[0], mimetype[1])
attachment.set_payload(fp.read())
fp.close()
encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment',
filename=os.path.basename(filename))
self.email.attach(attachment)
def __str__(self):
return self.email.as_string()
class EmailConnection(object):
def __init__(self, server, username, password):
if ':' in server:
data = server.split(':')
self.server = data[0]
self.port = int(data[1])
else:
self.server = server
self.port = 25
self.username = username
self.password = password
self.connect()
def connect(self):
self.connection = SMTP(self.server, self.port)
self.connection.ehlo()
self.connection.starttls()
self.connection.ehlo()
self.connection.login(self.username, self.password)
@staticmethod
def get_email(email):
if '<' in email:
data = email.split('<')
email = data[1].split('>')[0].strip()
return email.strip()
def send(self, message, from_=None, to=None):
if isinstance(message, str):
if from_ is None or to is None:
raise ValueError('You need to specify `from_` and `to`')
else:
from_ = EmailConnection.get_email(from_)
to = EmailConnection.get_email(to)
else:
from_ = message.email['From']
to = message.email['To']
message = str(message)
return self.connection.sendmail(from_, to, message)
def close(self):
self.connection.close()