#发送邮件
mailto_list = ["xxx@163.com"]
mail_user = "xxx@163.com"
mail_host = "smtp.163.com"
mail_pass = "xxx"
now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def send_mail(to_list,sub,content):
me = "lynn" + "<" + mail_user + ">"
msg = MIMEText(now_time + "\r\n" + content, _subtype="plain", _charset="utf-8")
msg["Subject"] = sub
msg["From"] = me
msg["To"] = ";".join(to_list)
try:
server = smtplib.SMTP(mail_host, 25)
server.login(mail_user, mail_pass)
server.sendmail(me, to_list, msg.as_string())
server.close()
log.log("邮件发送成功","info")
return True
except Exception as e:
log.log(str(e),"error")
send_mail(mailto_list,"邮件发送","python邮件发送")
主要方法网上比较丰富,需要注意的一点是,要使用163等其他邮箱的时候需要在邮箱的设置里面放开smtp设置,并且此处使用的密码是邮箱开放smtp的授权码。
另外可以根据需求发送html,图片,添加附件等。
网友评论