Révision | abc366b09e0480fc4f594f41eab6c3766de065ab (tree) |
---|---|
l'heure | 2008-10-27 20:12:58 |
Auteur | iselllo |
Commiter | iselllo |
I added a python script which allows me to send emails using smtplib.
@@ -0,0 +1,41 @@ | ||
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 | +gmail_user = "lorenzo.isella@gmail.com" | |
11 | +gmail_pwd = "mypasswd" | |
12 | + | |
13 | +def mail(to, subject, text, attach): | |
14 | + msg = MIMEMultipart() | |
15 | + | |
16 | + msg['From'] = gmail_user | |
17 | + msg['To'] = to | |
18 | + msg['Subject'] = subject | |
19 | + | |
20 | + msg.attach(MIMEText(text)) | |
21 | + | |
22 | + part = MIMEBase('application', 'octet-stream') | |
23 | + part.set_payload(open(attach, 'rb').read()) | |
24 | + Encoders.encode_base64(part) | |
25 | + part.add_header('Content-Disposition', | |
26 | + 'attachment; filename="%s"' % os.path.basename(attach)) | |
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(gmail_user, gmail_pwd) | |
34 | + mailServer.sendmail(gmail_user, to, msg.as_string()) | |
35 | + # Should be mailServer.quit(), but that crashes... | |
36 | + mailServer.close() | |
37 | + | |
38 | +mail("isella@lorenzo.com", | |
39 | + "Hello from python!", | |
40 | + "This is a email sent with python", | |
41 | + "temp.dat") |