python向服务器发送邮件事例

import os
import sys
import re

__author__ = 'xiaoming'

import requests
tstr = '<div>\n<ul>\n'
with open('/home/tvapk/run_sh/handle_log/playout/t.txt','r') as f:
for line in f:
tstr += '<li>'+ line+'</li>'
tstr+='</ul>\n</div>'
print tstr
rt = {"message":tstr}
import urllib
import json
mt = json.dumps(rt)
print type(mt)
print mt
t = urllib.quote(mt)
print type(t)
print t

sendm = {'template':'play_stat','data':t,'to':'log@tvapk.net','title':sys.argv[1]}
r = requests.post('http://api.tvall.cn:8888/mail/',sendm)

 

 

 

 

------------------------通用方法-----------------------------------

# coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = 'xxxxx@163.com'
receiver = 'xxxxxx@qq.com'
subject = '放假通知'
smtpserver = 'smtp.163.com'
username = 'xxxxx@163.com'
password = 'xxxx'

msg = MIMEText('大家关好窗户', 'plain', 'utf-8')  # 中文需参数‘utf-8',单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'xxxx@163.com'
msg['To'] = "xxxxx@qq.com"
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

 

posted @ 2016-11-12 14:06  白开水加糖  阅读(260)  评论(0)    收藏  举报