标签:user content .text block end expec 代码 邮件 code
import os
import smtplib
from email.mime.text import MIMEText
def send_mail(receiver):
mail_server = ‘smtp.163.com‘
sender_useranme = os.getenv(‘MAIL_USERNAME‘)
sender_password = os.getenv(‘MAIL_PASSWORD‘)
content = ‘<a href="www.baidu.com">点击完成验证</a>‘
message = MIMEText(content, ‘html‘)
message[‘Subject‘] = ‘用户激活‘
message[‘From‘] = sender_useranme
mail = smtplib.SMTP(mail_server)
mail.login(sender_useranme, sender_password)
mail.sendmail(sender_useranme, receiver, message)
mail.quit()
bug : TypeError: expected string or bytes-like object
需要将message对象格式化字符串就OK了
mail.sendmail(sender_useranme, receiver, message.as_string())
有时候服务器会把你发送的邮件当成垃圾邮件处理,当检查完代码都对的时候改一下message[‘Subject‘] = ‘用户激活‘这个的内容试试。
标签:user content .text block end expec 代码 邮件 code
原文地址:http://blog.51cto.com/14094286/2327734