【背景】
自动发送邮件通知是流成自动化系统必备的功能,今天介绍如何用Python实现。
【代码】
这里以163邮箱为例
def send_smtp_mail(host="smtp.163.com",content="",cont_type="plain",encode="utf-8",sender_name="My_Robot",reciver_name="user",subject="unkonwn subject",mail_user="[email protected]",mail_pass="Your Mail Pass" ):
import smtplib
from email.mime.text import MIMEText
from email.header import Header
with open("param.csv", encoding="utf8") as file:
mail_addrs = file.readlines()
sender = '[email protected]'
receivers = [ele.strip() for ele in mail_addrs]
message = MIMEText(content,cont_type,encode)
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = subject
try:
smtpObj = smtplib.SMTP_SSL(host,465)
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(sender,receivers,message.as_string())
print("mail sent success!")
except smtplib.SMTPException as e:
print(e)