blog 文章

2011年11月25日 星期五

python send mail (smtp) by gmail server

send_mail.py
1 #!/usr/bin/env python
2 # ref: http://docs.python.org/library/email-examples.html
3 # Import smtplib for the actual sending function
4 import smtplib
5
6 # Import the email modules we'll need
7 from email.mime.text import MIMEText
8
9 # Open a plain text file for reading. For this example, assume that
10 # the text file contains only ASCII characters.
11 #fp = open(textfile, 'rb')
12 # Create a text/plain message
13 #msg = MIMEText(fp.read())
14 #fp.close()
15 msg = MIMEText("python mail test")
16
17 me = 'my@gmail.com'
18
19 you = 'you@gmail.com'
20
21 # me == the sender's email address
22 # you == the recipient's email address
23 #msg['Subject'] = 'The contents of %s' % textfile
24 msg['Subject'] = 'The contents of 111'
25 msg['From'] = me
26 msg['To'] = you
27
28 print 'send...'
29
30 # Send the message via our own SMTP server, but don't include the
31 # envelope header.
32 #s = smtplib.SMTP('localhost')
33 s = smtplib.SMTP_SSL('smtp.gmail.com', 465) #port 465 or 587
34 s.login('me@gmail.com','my_passwd')
35 s.sendmail(me, [you], msg.as_string())
36 s.quit()
37 print 'end send'

in my test, I cannot use port 587.
ref:
http://docs.python.org/library/smtplib.html

沒有留言:

張貼留言

使用 google 的 reCAPTCHA 驗證碼, 總算可以輕鬆留言了。

我實在受不了 spam 了, 又不想讓大家的眼睛花掉, 只好放棄匿名留言。這是沒辦法中的辦法了。留言的朋友需要有 google 帳號。