通过邮件方式发送文章到Wordpress一直显示乱码,应该是编码问题,但没有调试成功
############################################################导入模块
import requests
import json
import urllib.parse
import smtplib
import sys
from email.mime.text import MIMEText
from email.header import Header
############################################################关键常量定义
sender = 'xxxx@dxxxx' # 设置邮件发件方
sender_pass = "xxxx" # 密码
receivers = 'xxxx@xxxx' # 接收邮件列表,设置多个形如-- receivers = ['user1@126.com','user1@qq.com']
reportlog = open('xxxx.log','r') # 打开日志文件
reportcontent = reportlog.read()
reportlog.close() #关闭日志文件
Webhook = 'https://oapi.dingtalk.com/robot/send?access_token=xxxx' #钉钉机器人webhook地址
DateHeaders = {"Content-Type": "application/json"} #改动会造成发送失败
DingMsg = "今天份的日报已经发送咯,请注意查收" #钉钉机器人消息内容
AtList = ["xxxx"] #消息At列表,必须为手机号 如["185xxxx7741","177xxxx0394"]
DingNotifi = {"msgtype": "text", "text": {"content": DingMsg},"at": {"atMobiles": AtList, "isAtAll": False}} #生成POST data
############################################################定义发邮件方式
class SendEmail():
def login_and_send(self,send_content):
try:
smtpObj = smtplib.SMTP()
smtpObj.connect("smtp.mxhichina.com", 25) # 设置smtp服务器地址及端口号
smtpObj.login(sender, sender_pass)
smtpObj.sendmail(sender, receivers, send_content.as_string())
print ("邮件发送成功")
except smtplib.SMTPException:
print ("Error: 无法发送邮件,请联系管理员")
def send_words_content(self):
message = MIMEText(reportcontent, 'html', 'utf-8') #纯文本写plain,HTML写html
message['From'] = Header("日报发送邮箱", 'utf-8')
message['To'] = Header("日报接收邮箱", 'utf-8')
message['Subject'] = Header("今天的日报", 'utf-8')
self.login_and_send(send_content=message)
return
############################################################发送邮件及钉钉提醒
send_email=SendEmail()
send_email.send_words_content()
DingString = json.dumps(DingNotifi)
res = requests.post(Webhook, data=DingString, headers=DateHeaders)
if res.text == '{"errcode":0,"errmsg":"ok"}':
print ("钉钉提醒成功")
else:
print (res.text)
依然是未成品,继续优化中