Subversion Repositories SmartDukaan

Rev

Rev 1246 | Rev 3316 | 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
 
1246 chandransh 11
def get_attachment_part(filename):
12
    part = MIMEBase('application', 'octet-stream')
13
    part.set_payload(open(filename, 'rb').read())
14
    Encoders.encode_base64(part)
15
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filename))
16
    return part
17
 
1137 chandransh 18
def mail(from_user, from_pwd, to, subject, text, part=None):
19
    msg = MIMEMultipart()
20
 
21
    msg['From'] = from_user
22
    msg['To'] = to
23
    msg['Subject'] = subject
24
 
25
    msg.attach(MIMEText(text))
26
 
27
    if part:
28
        msg.attach(part)
29
 
30
    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
31
    mailServer.ehlo()
32
    mailServer.starttls()
33
    mailServer.ehlo()
34
    mailServer.login(from_user, from_pwd)
35
    mailServer.sendmail(from_user, to, msg.as_string())
36
    # Should be mailServer.quit(), but that crashes...
37
    mailServer.close()
38
 
39
if __name__ == '__main__':
2123 varun.gupt 40
    parser = optparse.OptionParser()
41
    parser.add_option("-f", "--from", dest="sender",
42
                   type="string",
43
                   help="Send the mail as this user")
44
    parser.add_option("-p", "--password", dest="password",
45
                   type="string",
46
                   help="Email account password of the sender")
47
    parser.add_option("-t", "--to", dest="to",
48
                   type="string",
49
                   help="Send the mail to this user")
50
    parser.add_option("-s", "--subject", dest="subject",
51
                   type="string",
52
                   help="Subject line of the email")
53
    parser.add_option("-x", "--text", dest="text",
54
                   type="string",
55
                   help="Email body text")
56
    parser.add_option("-a", "--attach", dest="attachment",
57
                    metavar="FILE",
58
                    help="send FILE as an attachment")
59
    parser.set_defaults(sender="help@shop2020.in", password="5h0p2o2o")
60
    (options, args) = parser.parse_args()
61
 
62
    if options.attachment:
63
        part = get_attachment_part(options.attachment)
64
    else:
65
        part = None
66
 
67
    mail(options.sender, options.password, options.to, options.subject, options.text, part)