python 发送文本邮件
主要用到smtplib和email模块
'''
Created on 2014-6-3
@author: jyp
@module: pro1.sendEmail}
'''
import smtplib
from email.Message import Message
from time import sleep
smtpserver = 'smtp.163.com'
username = '[email protected]'
pswd = 'xxxxxx'
from_addr = '[email protected]'
to_addr = '[email protected]'
cc_addr = '[email protected]'
message = Message()
message['Subject'] = 'test of python send email'
message['From'] = from_addr
message['To'] = to_addr
message['Cc'] = cc_addr
message.set_payload('test of python emall function')
msg = message.as_string()
sm = smtplib.SMTP(smtpserver, port=25, timeout=20)
sm.set_debuglevel(1)
sm.ehlo()
sm.starttls()
sm.ehlo()
sm.login(username, pswd)
sm.sendmail(from_addr, to_addr, msg)
sleep(5)
sm.quit()
'''
# 文本格式
import smtplib
from email.mime.text import MIMEText
mail_to_list =['[email protected]', '[email protected]']
host = 'smtp.163.com'
user = 'yongpan66'
pswd = 'xxxxxx'
mail_postfix = '163.com'
subject = 'test python sendmail function'
content = 'hello , this mail is send by python ! good...中文。。。...test'
def send_mail(mail_to_list, subject, content):
from_addr = "hello" + "<" + user + "@" + mail_postfix + ">"
msg = MIMEText(content, _subtype='plain', _charset='utf-8')
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = ";".join(mail_to_list)
try:
mail_server = smtplib.SMTP()
mail_server.connect(host)
mail_server.login(user, pswd)
mail_server.sendmail(from_addr, mail_to_list, msg.as_string())
mail_server.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
if send_mail(mail_to_list, subject, content):
print '发送成功'
else:
print '发送失败'
'''
python 发送HTML格式邮件
#coding=utf-8
'''
Created on 2014-6-3
@author: jyp
@module: pro1.sendEmail_ofHTML}
'''
'''
import smtplib
from email.mime.text import MIMEText
mail_to_list =['[email protected]', '[email protected]']
mail_host = 'smtp.163.com'
mail_user = '[email protected]'
mail_pass = 'xxxxxx'
subject = 'test python'
content = "<a href='http://www.cnblogs.com/yongpan666/'>透明的空气</a>"
def send_mail(mail_to_list, subject, content):
msg = MIMEText(content, _subtype='html', _charset='utf-8')
msg['Subject'] = subject
msg['From'] = mail_user
msg['To'] = ";".join(mail_to_list)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user, mail_pass)
s.sendmail(mail_user, mail_to_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
if send_mail(mail_to_list, subject, content):
print "成功"
else:
print "失败"
'''
'''
# 带附件的email
'''
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import time
mail_to_list =['[email protected]', '[email protected]']
mail_host = 'smtp.163.com'
mail_user = '[email protected]'
mail_pass = 'xxxxxx'
subject = 'test python'
content = "test attachment ......."
def send_mail(mail_to_list, subject, content):
msg = MIMEMultipart()
body = MIMEText(content, _subtype='plain', _charset='utf-8')
msg.attach(body)
att1 = MIMEText(open('d:\\test.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="123.txt"'
msg.attach(att1)
att2 = MIMEText(open('d:\\test.docx', 'rb').read(), 'base64', 'gb2312')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="123.docx"'
msg.attach(att2)
# msg = MIMEText(content, _subtype='plain', _charset='utf-8')
msg['Subject'] = subject
msg['From'] = mail_user
msg['To'] = ";".join(mail_to_list)
msg['date']=time.strftime('%a, %d %b %Y %H:%M:%S %z')
try:
s = smtplib.SMTP()
s.connect(mail_host)
print 'connect success...'
s.login(mail_user, mail_pass)
print 'login..'
s.sendmail(mail_user, mail_to_list, msg.as_string())
print 'send...'
s.close()
return True
except Exception, e:
# print str(e)
return False
if __name__ == '__main__':
if send_mail(mail_to_list, subject, content):
print "成功"
else:
print "失败"
参考:http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html
:http://88fly.blog.163.com/blog/static/12268039020131193271774/