| 1336 |
rajveer |
1 |
import smtplib
|
|
|
2 |
from email.MIMEMultipart import MIMEMultipart
|
|
|
3 |
from email.MIMEBase import MIMEBase
|
|
|
4 |
from email.MIMEText import MIMEText
|
|
|
5 |
from email import Encoders
|
|
|
6 |
import os
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
gmail_user = "gaurav.jain@shop2020.in"
|
|
|
10 |
gmail_pwd = "gaurav"
|
|
|
11 |
|
|
|
12 |
def mail(to, subject, text, attach=None):
|
|
|
13 |
"""
|
|
|
14 |
Documentation for method mail
|
|
|
15 |
This is the method called by test.py for mailing csv files
|
|
|
16 |
"""
|
|
|
17 |
|
|
|
18 |
msg = MIMEMultipart()
|
|
|
19 |
msg['From'] = gmail_user
|
|
|
20 |
msg['To'] = to
|
|
|
21 |
msg['Subject'] = subject
|
|
|
22 |
|
|
|
23 |
msg.attach(MIMEText(text))
|
|
|
24 |
for f in attach:
|
|
|
25 |
#print f
|
|
|
26 |
part = MIMEBase('application', 'octet-stream')
|
|
|
27 |
part.set_payload(open(f, 'rb').read())
|
|
|
28 |
Encoders.encode_base64(part)
|
|
|
29 |
part.add_header('Content-Disposition',
|
|
|
30 |
'attachment; filename="%s"' % os.path.basename(f))
|
|
|
31 |
msg.attach(part)
|
|
|
32 |
|
|
|
33 |
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
|
|
|
34 |
mailServer.ehlo()
|
|
|
35 |
mailServer.starttls()
|
|
|
36 |
mailServer.ehlo()
|
|
|
37 |
mailServer.login(gmail_user, gmail_pwd)
|
|
|
38 |
mailServer.sendmail(gmail_user, to, msg.as_string())
|
|
|
39 |
mailServer.close()
|