| 1137 |
chandransh |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
import smtplib
|
|
|
4 |
from email.MIMEMultipart import MIMEMultipart
|
|
|
5 |
from email.MIMEBase import MIMEBase
|
|
|
6 |
from email.MIMEText import MIMEText
|
|
|
7 |
from email import Encoders
|
|
|
8 |
import os
|
| 2123 |
varun.gupt |
9 |
import optparse
|
| 1137 |
chandransh |
10 |
|
| 3316 |
chandransh |
11 |
COMMASPACE = ', '
|
|
|
12 |
|
| 1246 |
chandransh |
13 |
def get_attachment_part(filename):
|
|
|
14 |
part = MIMEBase('application', 'octet-stream')
|
|
|
15 |
part.set_payload(open(filename, 'rb').read())
|
|
|
16 |
Encoders.encode_base64(part)
|
|
|
17 |
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filename))
|
|
|
18 |
return part
|
|
|
19 |
|
| 5631 |
amar.kumar |
20 |
def mail_html(from_user, from_pwd, to, subject, html_text, parts=[]):
|
|
|
21 |
msg = MIMEMultipart()
|
|
|
22 |
|
|
|
23 |
msg['From'] = from_user
|
|
|
24 |
msg['To'] = COMMASPACE.join(to)
|
|
|
25 |
msg['Subject'] = subject
|
|
|
26 |
|
|
|
27 |
html_msg = MIMEText(html_text, 'html')
|
|
|
28 |
msg.attach(html_msg)
|
|
|
29 |
|
|
|
30 |
for part in parts:
|
|
|
31 |
msg.attach(part)
|
|
|
32 |
|
|
|
33 |
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
|
|
|
34 |
mailServer.ehlo()
|
|
|
35 |
mailServer.starttls()
|
|
|
36 |
mailServer.login(from_user, from_pwd)
|
|
|
37 |
mailServer.sendmail(from_user, to, msg.as_string())
|
|
|
38 |
# Should be mailServer.quit(), but that crashes...
|
|
|
39 |
mailServer.close()
|
|
|
40 |
|
|
|
41 |
|
| 5893 |
rajveer |
42 |
def mail(from_user, from_pwd, to, subject, text, parts=[], cc=[], bcc=[]):
|
| 1137 |
chandransh |
43 |
msg = MIMEMultipart()
|
|
|
44 |
|
|
|
45 |
msg['From'] = from_user
|
| 3316 |
chandransh |
46 |
msg['To'] = COMMASPACE.join(to)
|
| 5893 |
rajveer |
47 |
if cc:
|
|
|
48 |
to = to + cc
|
|
|
49 |
msg['Cc'] = COMMASPACE.join(cc)
|
|
|
50 |
if bcc:
|
|
|
51 |
to = to + bcc
|
| 1137 |
chandransh |
52 |
msg['Subject'] = subject
|
|
|
53 |
|
|
|
54 |
msg.attach(MIMEText(text))
|
|
|
55 |
|
| 3316 |
chandransh |
56 |
for part in parts:
|
| 1137 |
chandransh |
57 |
msg.attach(part)
|
|
|
58 |
|
|
|
59 |
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
|
|
|
60 |
mailServer.ehlo()
|
|
|
61 |
mailServer.starttls()
|
|
|
62 |
mailServer.ehlo()
|
|
|
63 |
mailServer.login(from_user, from_pwd)
|
|
|
64 |
mailServer.sendmail(from_user, to, msg.as_string())
|
|
|
65 |
# Should be mailServer.quit(), but that crashes...
|
|
|
66 |
mailServer.close()
|
|
|
67 |
|
| 21137 |
kshitij.so |
68 |
def mail_send_grid(from_email, from_user, from_pwd, to, subject, text, parts=[], cc=[], bcc=[]):
|
| 21100 |
kshitij.so |
69 |
msg = MIMEMultipart()
|
|
|
70 |
msg['From'] = from_email
|
|
|
71 |
msg['To'] = COMMASPACE.join(to)
|
|
|
72 |
if cc:
|
|
|
73 |
to = to + cc
|
|
|
74 |
msg['Cc'] = COMMASPACE.join(cc)
|
|
|
75 |
if bcc:
|
|
|
76 |
to = to + bcc
|
|
|
77 |
msg['Subject'] = subject
|
|
|
78 |
html_msg = MIMEText(text, 'html')
|
|
|
79 |
msg.attach(html_msg)
|
|
|
80 |
for part in parts:
|
|
|
81 |
msg.attach(part)
|
|
|
82 |
mailServer = smtplib.SMTP("smtp.sendgrid.net", 587)
|
|
|
83 |
mailServer.ehlo()
|
|
|
84 |
mailServer.starttls()
|
|
|
85 |
mailServer.ehlo()
|
|
|
86 |
mailServer.login(from_user, from_pwd)
|
|
|
87 |
mailServer.sendmail(from_user, to, msg.as_string())
|
|
|
88 |
# Should be mailServer.quit(), but that crashes...
|
|
|
89 |
mailServer.close()
|
|
|
90 |
|
| 1137 |
chandransh |
91 |
if __name__ == '__main__':
|
| 23839 |
amit.gupta |
92 |
mail_send_grid("sales-associates@smartdukaan.com","apikey", "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", ["kshitij.sood@saholic.com"], "hi", "text", [get_attachment_part("/home/kshitij/sa/test.xls")],["thekshitijsood@gmail.com","kshitijsood.hpu@gmail.com"],["amit.gupta@saholic.com","kshitijsood@ymail.com"])
|