Subversion Repositories SmartDukaan

Rev

Rev 2123 | Rev 5631 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2123 Rev 3316
Line 6... Line 6...
6
from email.MIMEText import MIMEText
6
from email.MIMEText import MIMEText
7
from email import Encoders
7
from email import Encoders
8
import os
8
import os
9
import optparse
9
import optparse
10
 
10
 
-
 
11
COMMASPACE = ', '
-
 
12
 
11
def get_attachment_part(filename):
13
def get_attachment_part(filename):
12
    part = MIMEBase('application', 'octet-stream')
14
    part = MIMEBase('application', 'octet-stream')
13
    part.set_payload(open(filename, 'rb').read())
15
    part.set_payload(open(filename, 'rb').read())
14
    Encoders.encode_base64(part)
16
    Encoders.encode_base64(part)
15
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filename))
17
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filename))
16
    return part
18
    return part
17
    
19
    
18
def mail(from_user, from_pwd, to, subject, text, part=None):
20
def mail(from_user, from_pwd, to, subject, text, parts=[]):
19
    msg = MIMEMultipart()
21
    msg = MIMEMultipart()
20
    
22
    
21
    msg['From'] = from_user
23
    msg['From'] = from_user
22
    msg['To'] = to
24
    msg['To'] = COMMASPACE.join(to)
23
    msg['Subject'] = subject
25
    msg['Subject'] = subject
24
    
26
    
25
    msg.attach(MIMEText(text))
27
    msg.attach(MIMEText(text))
26
    
28
    
27
    if part:
29
    for part in parts:
28
        msg.attach(part)
30
        msg.attach(part)
29
    
31
    
30
    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
32
    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
31
    mailServer.ehlo()
33
    mailServer.ehlo()
32
    mailServer.starttls()
34
    mailServer.starttls()
Line 36... Line 38...
36
    # Should be mailServer.quit(), but that crashes...
38
    # Should be mailServer.quit(), but that crashes...
37
    mailServer.close()
39
    mailServer.close()
38
 
40
 
39
if __name__ == '__main__':
41
if __name__ == '__main__':
40
    parser = optparse.OptionParser()
42
    parser = optparse.OptionParser()
41
    parser.add_option("-f", "--from", dest="sender",
43
    parser.add_option("-f", "--from",
-
 
44
                      dest="sender",
42
                   type="string",
45
                      type="string",
43
                   help="Send the mail as this user")
46
                      help="Send the mail as this user")
44
    parser.add_option("-p", "--password", dest="password",
47
    parser.add_option("-p", "--password",
-
 
48
                      dest="password",
45
                   type="string",
49
                      type="string",
46
                   help="Email account password of the sender")
50
                      help="Email account password of the sender")
47
    parser.add_option("-t", "--to", dest="to",
51
    parser.add_option("-t", "--to",
-
 
52
                      action="append",
-
 
53
                      dest="to_addresses",
48
                   type="string",
54
                      type="string",
-
 
55
                      metavar="EMAIL",
49
                   help="Send the mail to this user")
56
                      help="Send the mail to EMAIL")
50
    parser.add_option("-s", "--subject", dest="subject",
57
    parser.add_option("-s", "--subject",
-
 
58
                      dest="subject",
51
                   type="string",
59
                      type="string",
52
                   help="Subject line of the email")
60
                      help="Subject line of the email")
53
    parser.add_option("-x", "--text", dest="text",
61
    parser.add_option("-x", "--text", dest="text",
54
                   type="string",
62
                      type="string",
55
                   help="Email body text")
63
                      help="Email body text")
56
    parser.add_option("-a", "--attach", dest="attachment",
64
    parser.add_option("-a", "--attach",
-
 
65
                      dest="attachments",
-
 
66
                      action="append",
-
 
67
                      type="string",
57
                    metavar="FILE",
68
                      metavar="FILE",
58
                    help="send FILE as an attachment")
69
                      help="send FILE as an attachment. Use this option multiple times to send multiple attachments.")
59
    parser.set_defaults(sender="help@shop2020.in", password="5h0p2o2o")
70
    parser.set_defaults(sender="help@shop2020.in", password="5h0p2o2o")
60
    (options, args) = parser.parse_args()
71
    (options, args) = parser.parse_args()
61
    
72
    
-
 
73
    parts = []
62
    if options.attachment:
74
    if options.attachments:
63
        part = get_attachment_part(options.attachment)
75
        parts = [get_attachment_part(attachment) for attachment in options.attachments]
64
    else:
-
 
65
        part = None
-
 
66
    
76
    
67
    mail(options.sender, options.password, options.to, options.subject, options.text, part)
-
 
68
77
    mail(options.sender, options.password, options.to_addresses, options.subject, options.text, parts)
-
 
78
69
79