| 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 |
|
| 3316 |
chandransh |
42 |
def mail(from_user, from_pwd, to, subject, text, parts=[]):
|
| 1137 |
chandransh |
43 |
msg = MIMEMultipart()
|
|
|
44 |
|
|
|
45 |
msg['From'] = from_user
|
| 3316 |
chandransh |
46 |
msg['To'] = COMMASPACE.join(to)
|
| 1137 |
chandransh |
47 |
msg['Subject'] = subject
|
|
|
48 |
|
|
|
49 |
msg.attach(MIMEText(text))
|
|
|
50 |
|
| 3316 |
chandransh |
51 |
for part in parts:
|
| 1137 |
chandransh |
52 |
msg.attach(part)
|
|
|
53 |
|
|
|
54 |
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
|
|
|
55 |
mailServer.ehlo()
|
|
|
56 |
mailServer.starttls()
|
|
|
57 |
mailServer.ehlo()
|
|
|
58 |
mailServer.login(from_user, from_pwd)
|
|
|
59 |
mailServer.sendmail(from_user, to, msg.as_string())
|
|
|
60 |
# Should be mailServer.quit(), but that crashes...
|
|
|
61 |
mailServer.close()
|
|
|
62 |
|
|
|
63 |
if __name__ == '__main__':
|
| 2123 |
varun.gupt |
64 |
parser = optparse.OptionParser()
|
| 3316 |
chandransh |
65 |
parser.add_option("-f", "--from",
|
|
|
66 |
dest="sender",
|
|
|
67 |
type="string",
|
|
|
68 |
help="Send the mail as this user")
|
|
|
69 |
parser.add_option("-p", "--password",
|
|
|
70 |
dest="password",
|
|
|
71 |
type="string",
|
|
|
72 |
help="Email account password of the sender")
|
|
|
73 |
parser.add_option("-t", "--to",
|
|
|
74 |
action="append",
|
|
|
75 |
dest="to_addresses",
|
|
|
76 |
type="string",
|
|
|
77 |
metavar="EMAIL",
|
|
|
78 |
help="Send the mail to EMAIL")
|
|
|
79 |
parser.add_option("-s", "--subject",
|
|
|
80 |
dest="subject",
|
|
|
81 |
type="string",
|
|
|
82 |
help="Subject line of the email")
|
| 2123 |
varun.gupt |
83 |
parser.add_option("-x", "--text", dest="text",
|
| 3316 |
chandransh |
84 |
type="string",
|
|
|
85 |
help="Email body text")
|
|
|
86 |
parser.add_option("-a", "--attach",
|
|
|
87 |
dest="attachments",
|
|
|
88 |
action="append",
|
|
|
89 |
type="string",
|
|
|
90 |
metavar="FILE",
|
|
|
91 |
help="send FILE as an attachment. Use this option multiple times to send multiple attachments.")
|
| 2123 |
varun.gupt |
92 |
parser.set_defaults(sender="help@shop2020.in", password="5h0p2o2o")
|
|
|
93 |
(options, args) = parser.parse_args()
|
|
|
94 |
|
| 3316 |
chandransh |
95 |
parts = []
|
|
|
96 |
if options.attachments:
|
|
|
97 |
parts = [get_attachment_part(attachment) for attachment in options.attachments]
|
| 2123 |
varun.gupt |
98 |
|
| 3316 |
chandransh |
99 |
mail(options.sender, options.password, options.to_addresses, options.subject, options.text, parts)
|