终于搞点这段代码了,还是自己控制好的,不过同样依靠定时任务来执行!
通过python收邮件,再通过wordpress的xmlrpc来发表,记得在wordpress的后台选上xmlrpc发表那项.
废话不说了,以下是python代码:
#coding=gbk
import poplib
import cStringIO
import email
import base64
import string
import time
import xmlrpclib
def send_data_to_wp(wp_url,wp_username,wp_password,title,content,tags,fenlei,fabiao):
#wp_url = “http://192.168.0.25/wordpress/xmlrpc.php”
#wp_username = “admin”
#wp_password = “123456789”
wp_blogid=”
status_published = 0
#status_published = 1
server = xmlrpclib.ServerProxy(wp_url)
aa=fenlei
aa=aa.decode(‘gbk’,’ignore’).encode(‘utf8′,’ignore’)
categories = [aa]
#中文的gbk转换成utf8格式
title=title.decode(‘gbk’,’ignore’).encode(‘utf8′,’ignore’)
#中文的gbk转换成utf8格式
content=content.decode(‘gbk’,’ignore’).encode(‘utf8′,’ignore’)
#tags = [“sometag”, “othertag”]
#fabiao等于1就立刻发表文章,不是的话就是草稿
if fabiao==1:
xpush = ‘publish’
else:
xpush = ‘draft’
data = {‘title’: title, ‘description’: content, ‘categories’: categories, ‘mt_keywords’: tags,’post_status’:xpush}
print data
post_id = server.metaWeblog.newPost(wp_blogid, wp_username, wp_password, data, status_published)
return post_id
#邮箱用户
mailuser=’xxxx’
#邮箱密码
mailpwd=’123456789′
#wordpress xmlrpc.php位置
wpurl =’http://192.168.5.25/wordpress/xmlrpc.php’
#wordpress 用户名
wpuser=’admin’
#wordpress 登录密码
wppwd =’123456789′
#pop收信的地址
server = poplib.POP3(“xxx.lpfrx.com”)
server.user(mailuser)
server.pass_(mailpwd)
#print server.stat()
numMessages = len(server.list()[1])
print ‘num of messages =>’,numMessages
addressList=[]
mailnum,mailsize=server.stat()
#删除邮件 (poplib.POP3.dele)
for i in range(numMessages):
m = server.retr(i+1)
buf = cStringIO.StringIO()
mail=email.message_from_string(string.join(m[1],’\n’))
atitle=email.Header.decode_header(mail[‘subject’])[0][0]
try:
ff=atitle.decode(‘utf8’).encode(‘gbk’,’ignore’)
except:
ff=atitle
print ff
#time.sleep(1)
for j in m[1]:
print >>buf, j
buf.seek(0)
#解析信件内容
msg = email.message_from_file(buf)
for part in msg.walk():
contenttype = part.get_content_type()
filename = part.get_filename()
#显示文件类型
#print ‘type: ‘,contenttype
if contenttype == ‘text/plain’:
# 保存正文
mailstr=base64.decodestring(part.get_payload())
#print mailstr
try:
newmailstr=mailstr.decode(‘utf8’).encode(‘gbk’,’ignore’)
except:
newmailstr=mailstr
if ff !=”:
tags=[“”]
#需要提交的分类 wordpress 分类的名称
xfenlei=’一般想到的’
print send_data_to_wp(wpurl,wpuser,wppwd,ff,newmailstr,tags,xfenlei,0)
# 写入文件
f = open(“fmail%d.txt” % (i+1),’wb’)
f.write(newmailstr)
f.close()
buf.close()
time.sleep(1)
#删除邮件
server.dele(selected+1)
server.quit()