Skip to content

Instantly share code, notes, and snippets.

@yszou
Created Dec 9, 2013
Embed
What would you like to do?
发出信息的工具
# -*- coding: utf-8 -*-
import sys, os, re
from lib.eml_parse import parse
from lib.smtpclient import SMTPClient
import tempfile
import email.utils
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import make_header
import tornado.ioloop
from config import ACCOUNT
#_, _, to= sys.argv
to = sys.argv[2:]
eml = sys.stdin.read()
msg = parse(eml)
plain = msg['plain'].read().encode('utf8').strip()
tf = tempfile.mktemp()
out_tf = tempfile.mktemp()
header = '''%s
%%!encoding: utf-8
%%!options(xhtml):
%%%%toc
''' % msg['subject'].encode('utf8')
o_plain = plain
plain = header + plain
if msg['in_reply_to'].startswith('<Discuz'):
header = '''%s
''' % msg['subject'].encode('utf8')
plain = header + o_plain
with open(tf, 'w') as f:
f.write(plain)
os.system('txt2tags -t bbcode --mask-email -o %s %s' % (out_tf, tf))
with open(out_tf, 'r') as f:
html = f.read()
#优化格式
for tag in ['list', 'quote', 'table']:
html = html.replace('\n\n[%s]' % tag, '\n[%s]' % tag)
html = html.replace('[/%s]\n\n' % tag, '[/%s]\n' % tag)
html = html.replace('\n\n[hr]', '\n\n[hr]')
html = html.replace('[hr]\n\n', '[hr]\n')
_, fid, thread, post, app_id, url = re.findall('<.*?>', msg['in_reply_to'])[0].split('@', 1)[0].split('-')
url = url.decode('hex')
from lib.sina_sso import reply
IL = tornado.ioloop.IOLoop.instance()
@tornado.gen.engine
def func():
pid, error = yield tornado.gen.Task(reply, url, 'xxx@gmail.com', '***',
int(fid), int(thread), int(post), html)
IL.stop()
IL.close()
sys.exit(error)
func()
IL.start()
with open(tf, 'w') as f:
f.write(plain)
os.system('txt2tags -t xhtml --toc-level=2 --encoding=utf-8 --enum-title --mask-email -o %s %s' % (out_tf, tf))
with open(out_tf, 'r') as f:
html = f.read()
html = html.replace('onload="prettyPrint()" ', '')
html = html.replace('background-image: url(http://zouyesheng.com/static/bg.jpg); ', '')
html = html.replace('<script type="text/javascript">var prettyPrint = function(){}</script>', '')
html = html.replace('&copy;2010-2013 zouyesheng.com All rights reserved. Powered by ', '')
html = html.replace('<a href="https://github.com/" target="_blank">GitHub</a> , ', '')
html = html.replace('<a href="http://txt2tags.org/" target="_blank">txt2tags</a> , ', '')
html = html.replace('<a href="http://www.mathjax.org/" target="_blank">MathJax</a>', '')
html = html.replace(' style=" margin: 0px; padding: 0px; font-family: Arial,Helvetica,sans-serif; "', '')
html = html.replace('<h1 style=" color: black; letter-spacing: 0.2em; text-align: center; font-size: x-large; font-weight: bold; text-shadow: 2px 2px 2px gray; margin: 30px auto 30px auto; ">%s</h1>' % msg['subject'].encode('utf8'), '')
html = html.replace(' style=" width: 700px; margin: auto; font-size: 15px; line-height: 1.6em; color: #333; letter-spacing: 1px; padding: 30px; background-color: white; "', '')
html = html.replace(' style=" float: right; font-size: 13px; color: #777; "', '')
html = html.replace('--\n', '--\n<br />')
#html = html.replace('邹业盛 - 搜狐邮件中心', '<br />邹业盛 - 搜狐邮件中心')
html = html.replace('电话: (010) 6272 6602', '<br />电话: (010) 6272 6602<br />')
#html = html.replace('进出自由才是游戏者的生存之道。', '<br />进出自由才是游戏者的生存之道。')
html_msg = MIMEText(html, _subtype='html', _charset="utf-8")
plain_msg = MIMEBase(_maintype='text', _subtype='plain', charset="utf-8", format="flowed")
plain_msg.set_payload(o_plain, "utf-8")
alt_msg = MIMEMultipart('alternative')
alt_msg.attach(plain_msg)
alt_msg.attach(html_msg)
if msg['attachment']:
all_msg = MIMEMultipart()
all_msg.attach(alt_msg)
for i in range(1, len(msg['attachment']) + 1):
all_msg.attach(msg['obj'].get_payload(i))
else:
all_msg = alt_msg
for k in msg['obj'].keys():
if k not in ['Content-Type', 'Content-Disposition', 'Content-Transfer-Encoding']:
all_msg[k] = msg['obj'][k]
IL = tornado.ioloop.IOLoop.instance()
def callback(code, msg, response):
for cmd, value in response:
print '%s %s' % (cmd, value)
IL.stop()
IL.close()
sys.exit(0)
def errback(code, msg, response):
for cmd, value in response:
print '%s %s' % (cmd, value)
IL.stop()
IL.close()
sys.exit(1)
if 'sohu-inc' in to[0]:
a = ACCOUNT['sohu-inc']
else:
a = ACCOUNT['gmail']
client = SMTPClient(a['host'], a['port'], a['user'], a['pass'], a['ssl'])
client.send_mail(a['user'], to, all_msg, callback, errback)
IL.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment