1 # -*- coding: UTF-8 -*- 2 ''' 3 發(fā)送txt文本郵件 4 http://m.survivalescaperooms.com/liu-ke 5 ''' 6 import smtplib 7 from email.mime.text import MIMEText 8 mailto_list=['***@**.***'] 9 mail_host="smtp.****.com" #設(shè)置服務(wù)器10 mail_user="***@**.**" #用戶名11 mail_pass="********" #口令 12 mail_postfix="***.com" #發(fā)件箱的后綴13 14 def send_mail(to_list,sub,content): 15 me="hello"+"<"+mail_user+"@"+mail_postfix+">" 16 msg = MIMEText(content,_subtype='plain',_charset='gb2312') 17 msg['Subject'] = sub 18 msg['From'] = me 19 msg['To'] = ";".join(to_list) 20 try: 21 server = smtplib.SMTP() 22 server.connect(mail_host) 23 server.login(mail_user,mail_pass) 24 server.sendmail(me, to_list, msg.as_string()) 25 server.close() 26 return True 27 except Exception, e: 28 PRint str(e) 29 return False 30 if __name__ == '__main__': 31 if send_mail(mailto_list,"hello","hello world!"): 32 print "發(fā)送成功" 33 else: 34 print "發(fā)送失敗"
1 # -*- coding: utf-8 -*- 2 ''' 3 發(fā)送html文本郵件 4 http://m.survivalescaperooms.com/liu-ke 5 ''' 6 import smtplib 7 from email.mime.text import MIMEText 8 mailto_list=["*****"] 9 mail_host="smtp.***.com" #設(shè)置服務(wù)器10 mail_user="****" #用戶名11 mail_pass="****" #口令 12 mail_postfix="***.com" #發(fā)件箱的后綴13 14 def send_mail(to_list,sub,content): #to_list:收件人;sub:主題;content:郵件內(nèi)容15 me="hello"+"<"+mail_user+"@"+mail_postfix+">" #這里的hello可以任意設(shè)置,收到信后,將按照設(shè)置顯示16 msg = MIMEText(content,_subtype='html',_charset='gb2312') #創(chuàng)建一個實例,這里設(shè)置為html格式郵件17 msg['Subject'] = sub #設(shè)置主題18 msg['From'] = me 19 msg['To'] = ";".join(to_list) 20 try: 21 s = smtplib.SMTP() 22 s.connect(mail_host) #連接smtp服務(wù)器23 s.login(mail_user,mail_pass) #登陸服務(wù)器24 s.sendmail(me, to_list, msg.as_string()) #發(fā)送郵件25 s.close() 26 return True 27 except Exception, e: 28 print str(e) 29 return False 30 if __name__ == '__main__': 31 if send_mail(mailto_list,"hello","<a href='http://m.survivalescaperooms.com/xiaowuyi'>小五義</a>"): 32 print "發(fā)送成功" ,mailto_list 33 else: 34 print "發(fā)送失敗"
新聞熱點
疑難解答