smtplib,發送郵件時的bug
阿新 • • 發佈:2018-12-07
get 格式化 ike 需要 sender baidu con 發送郵件 sendmail
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‘] = ‘用戶激活‘這個的內容試試。
smtplib,發送郵件時的bug