您的位置:首页 > 编程语言 > Python开发

python3 发邮件,从文件读邮件内容和发送html内容

2011-08-03 17:03 721 查看
#!/usr/bin/python
#coding=gbk
import smtplib
from email.mime.text import MIMEText
from urllib.request import urlopen

#从文件读
file_ob=open('/home/mailcontent.txt')
try:
content=file_ob.read()
finally:
file_ob.close()
msg=MIMEText(content,'plain','utf-8')
msg['Subject']='this is a test from python'
msg['From']='from@163.com'
msg['To']='to@qq.com'

#从网页读
f=urlopen('http://www.pcesen.com')
msg=MIMEText(f.read(),'html','utf-8')
msg['Subject']='this is a test from python'
msg['From']='from@163.com'
msg['To']='to@qq.com'

smtp=smtplib.SMTP()
smtp.connect("smtp.163.com","25")
smtp.login('from','password')
smtp.sendmail('from@163.com','to@qq.com',msg.as_string())
smtp.quit()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: