Subversion Repositories SmartDukaan

Rev

Rev 1180 | Rev 2123 | 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
9
 
1246 chandransh 10
def get_attachment_part(filename):
11
    part = MIMEBase('application', 'octet-stream')
12
    part.set_payload(open(filename, 'rb').read())
13
    Encoders.encode_base64(part)
14
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filename))
15
    return part
16
 
1137 chandransh 17
def mail(from_user, from_pwd, to, subject, text, part=None):
18
    msg = MIMEMultipart()
19
 
20
    msg['From'] = from_user
21
    msg['To'] = to
22
    msg['Subject'] = subject
23
 
24
    msg.attach(MIMEText(text))
25
 
26
    if part:
27
        msg.attach(part)
28
 
29
    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
30
    mailServer.ehlo()
31
    mailServer.starttls()
32
    mailServer.ehlo()
33
    mailServer.login(from_user, from_pwd)
34
    mailServer.sendmail(from_user, to, msg.as_string())
35
    # Should be mailServer.quit(), but that crashes...
36
    mailServer.close()
37
 
38
if __name__ == '__main__':
39
    mail("cnc.center@shop2020.in",
1180 chandransh 40
         "puttherightpasswdhere",
1137 chandransh 41
         "some.person@some.address.com",
42
         "Hello from python!",
1180 chandransh 43
         "This is an email sent with python",
1137 chandransh 44
         None)