Subversion Repositories SmartDukaan

Rev

Rev 2123 | Rev 5631 | 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
 
3316 chandransh 20
def mail(from_user, from_pwd, to, subject, text, parts=[]):
1137 chandransh 21
    msg = MIMEMultipart()
22
 
23
    msg['From'] = from_user
3316 chandransh 24
    msg['To'] = COMMASPACE.join(to)
1137 chandransh 25
    msg['Subject'] = subject
26
 
27
    msg.attach(MIMEText(text))
28
 
3316 chandransh 29
    for part in parts:
1137 chandransh 30
        msg.attach(part)
31
 
32
    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
33
    mailServer.ehlo()
34
    mailServer.starttls()
35
    mailServer.ehlo()
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
if __name__ == '__main__':
2123 varun.gupt 42
    parser = optparse.OptionParser()
3316 chandransh 43
    parser.add_option("-f", "--from",
44
                      dest="sender",
45
                      type="string",
46
                      help="Send the mail as this user")
47
    parser.add_option("-p", "--password",
48
                      dest="password",
49
                      type="string",
50
                      help="Email account password of the sender")
51
    parser.add_option("-t", "--to",
52
                      action="append",
53
                      dest="to_addresses",
54
                      type="string",
55
                      metavar="EMAIL",
56
                      help="Send the mail to EMAIL")
57
    parser.add_option("-s", "--subject",
58
                      dest="subject",
59
                      type="string",
60
                      help="Subject line of the email")
2123 varun.gupt 61
    parser.add_option("-x", "--text", dest="text",
3316 chandransh 62
                      type="string",
63
                      help="Email body text")
64
    parser.add_option("-a", "--attach",
65
                      dest="attachments",
66
                      action="append",
67
                      type="string",
68
                      metavar="FILE",
69
                      help="send FILE as an attachment. Use this option multiple times to send multiple attachments.")
2123 varun.gupt 70
    parser.set_defaults(sender="help@shop2020.in", password="5h0p2o2o")
71
    (options, args) = parser.parse_args()
72
 
3316 chandransh 73
    parts = []
74
    if options.attachments:
75
        parts = [get_attachment_part(attachment) for attachment in options.attachments]
2123 varun.gupt 76
 
3316 chandransh 77
    mail(options.sender, options.password, options.to_addresses, options.subject, options.text, parts)