1 | # -*- coding:utf-8 -*- |
2 |
3 | import smtplib |
4 | from email.mime.text import MIMEText |
5 |
6 | class sendmail: |
7 | def __init__( self ): |
8 | self .mail_host = "smtp.gmail.com" |
9 | self .mail_user = "帳號" |
10 | self .mail_passwd = "密碼" |
11 | self .postfix = "gmail.com" |
12 | self .mailto = "收件者信箱" |
13 | def send( self ,subject,content): |
14 | gmailuser = self .mail_user + "@" + self .postfix |
15 | msg = MIMEText(content) |
16 | msg[ 'Subject' ] = subject |
17 | msg[ 'From' ] = "我" |
18 | msg[ 'To' ] = self .mailto |
19 | smtp = smtplib.SMTP( 'smtp.gmail.com' , 587 ) |
20 | smtp.ehlo() |
21 | smtp.starttls() |
22 | smtp.ehlo() |
23 | smtp.login( self .mail_user, self .mail_passwd) |
24 | smtp.sendmail(gmailuser, self .mailto,msg.as_string()) |
25 | smtp.close() |
26 | print "寄信成功" |
27 |
28 | spam_block = sendmail() |
29 | spam_block.send( "這是測試信" , "成功之內文" ) |