Skip to content

Commit f56517c

Browse files
Add files via upload
1 parent dc8a663 commit f56517c

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

easy_mailer/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from send_outlook_mail import send_mail

easy_mailer/utils.py

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
# In[17]:
5+
6+
7+
import smtplib
8+
from email.mime.multipart import MIMEMultipart
9+
from email.mime.text import MIMEText
10+
from email.mime.base import MIMEBase
11+
from email import encoders
12+
import logging
13+
14+
15+
# In[18]:
16+
17+
18+
logger = logging.getLogger()
19+
20+
21+
# In[2]:
22+
23+
24+
def appendAttachments(file,msg):
25+
# open the file to be sent
26+
27+
attachment = open(filename, "rb")
28+
29+
# instance of MIMEBase and named as p
30+
p = MIMEBase('application', 'octet-stream')
31+
32+
# To change the payload into encoded form
33+
p.set_payload((attachment).read())
34+
35+
# encode into base64
36+
encoders.encode_base64(p)
37+
38+
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
39+
40+
41+
# attach the instance 'p' to instance 'msg'
42+
msg.attach(p)
43+
44+
return msg
45+
46+
47+
# In[19]:
48+
49+
50+
def send_mail(username, password, recipient_list, subject, *args):
51+
"""
52+
This takes into input parameters as shown below
53+
54+
Args:
55+
username: required
56+
type: str
57+
The email id of outlook from which one needs to send an email.
58+
password: required
59+
type: str
60+
Password of outlook email for authentication.
61+
recipient list:
62+
type: list
63+
The recipients enclosed in a list
64+
subject: required
65+
type: str
66+
The subject line of the mailer
67+
message: optional
68+
type: str
69+
Any text to be displayed in body of the mailer. Please provide absolute local path of the attachment.
70+
files: optional
71+
type: Any
72+
Attachments to be uploaded in the mailer. Note mail restrictions of memory still applies.
73+
In case of no attachment
74+
75+
76+
Returns:
77+
A mail is sent to intended recipients. Can be used to automate sending of mails/reports.
78+
79+
Raises:
80+
KeyError: To be updated.
81+
"""
82+
msg = MIMEMultipart()
83+
msg['From'] = username
84+
msg['To'] = ', '.join(recipient_list)
85+
msg['Subject'] = subject
86+
if len(args) > 0:
87+
message, files = args
88+
msg.attach(MIMEText(message))
89+
if len(files) > 0:
90+
for i in files:
91+
msg = appendAttachments(i,msg)
92+
93+
94+
#print('sending mail to ' + recipient + ' on ' + subject)
95+
96+
#print('Sending mail')
97+
98+
#Setting the threshold of logger to DEBUG
99+
logger.setLevel(logging.DEBUG)
100+
logger.info('Sending mail')
101+
try:
102+
mailServer = smtplib.SMTP('smtp-mail.outlook.com', 587)
103+
mailServer.ehlo()
104+
mailServer.starttls()
105+
mailServer.ehlo()
106+
mailServer.login(username, password)
107+
mailServer.sendmail(username, recipient_list, msg.as_string())
108+
mailServer.close()
109+
logger.info('Mail sent.')
110+
except Exception as e:
111+
logger.error(e)
112+
113+
114+
115+
116+
# In[ ]:
117+
118+
119+
120+

0 commit comments

Comments
 (0)