Subversion Repositories SmartDukaan

Rev

Rev 5631 | Rev 21100 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
68
if __name__ == '__main__':
2123 varun.gupt 69
    parser = optparse.OptionParser()
3316 chandransh 70
    parser.add_option("-f", "--from",
71
                      dest="sender",
72
                      type="string",
73
                      help="Send the mail as this user")
74
    parser.add_option("-p", "--password",
75
                      dest="password",
76
                      type="string",
77
                      help="Email account password of the sender")
78
    parser.add_option("-t", "--to",
79
                      action="append",
80
                      dest="to_addresses",
81
                      type="string",
82
                      metavar="EMAIL",
83
                      help="Send the mail to EMAIL")
84
    parser.add_option("-s", "--subject",
85
                      dest="subject",
86
                      type="string",
87
                      help="Subject line of the email")
2123 varun.gupt 88
    parser.add_option("-x", "--text", dest="text",
3316 chandransh 89
                      type="string",
90
                      help="Email body text")
91
    parser.add_option("-a", "--attach",
92
                      dest="attachments",
93
                      action="append",
94
                      type="string",
95
                      metavar="FILE",
96
                      help="send FILE as an attachment. Use this option multiple times to send multiple attachments.")
2123 varun.gupt 97
    parser.set_defaults(sender="help@shop2020.in", password="5h0p2o2o")
98
    (options, args) = parser.parse_args()
99
 
3316 chandransh 100
    parts = []
101
    if options.attachments:
102
        parts = [get_attachment_part(attachment) for attachment in options.attachments]
2123 varun.gupt 103
 
3316 chandransh 104
    mail(options.sender, options.password, options.to_addresses, options.subject, options.text, parts)