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