利用邮件模块,模拟发送邮件的过程
#!/usr/bin/python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from config import emailc
def send_mail(context=None,host=emailc.host,sender=emailc.sender,receivers=emailc.receivers,subject=emailc.subject):
message = MIMEText(context,"html","UTF8")
message["from"]=Header(sender,'UTF8')
for receiver in receivers:
message["to"]=Header(receiver,'UTF8')
message["subject"]=Header(subject,"GBK")
Smtp_Obj = smtplib.SMTP(host)
Smtp_Obj.sendmail(sender, receivers, message.as_string())
return "邮件发送成功"
if __name__ == "__main__":
print(send_mail("到饭点了哎"))
解析:
- 发送邮件需要几个要素:邮箱服务器地址,发件人,收件人,发送内容。其实还需要发件人的账号,密码;也可加上邮件头信息(发件人,收件人,主题)
- 示例代码中收件人以列表的方式给出,遍历将所有的收件人都写到头信息的收件人中