1、

由于毯毯狐没有单独的显示器,要是出现什么状况很难第一时间发现……于是就想到了让毯毯狐在发生什么事情的时候发邮件到邮箱。

2、完整代码

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


email_host = ''#邮件服务器smtp地址,例如outlook为 smtp.office365.com
email_port = 587

sender = ''#发送者的邮箱地址
email_passwd = ''#发送者的邮箱密码


def send_mail(Subject,Content,receivers):
	msg = MIMEMultipart()
	msg['Subject'] = Subject
	msg['From'] = sender
	msg['To'] = ';'.join(receivers)

	msg_text = MIMEText(_text=Content, _subtype='plain', _charset='utf-8')
	msg.attach(msg_text)

	smtpObj = smtplib.SMTP(host=email_host, port=email_port)
	smtpObj.ehlo()
	smtpObj.starttls()

	try:
		smtpObj.login(sender, email_passwd)
		smtpObj.sendmail(sender, receivers, msg.as_string())
		print("Successfully sent email")
		smtpObj.close()
	except smtplib.SMTPException as e:
		print("Error: unable to send email")
		print(e)

然后将文件保存为mail.py,放在合适的路径就可以啦

3、简单使用

在需要发送邮件的脚本里,先import mail,然后就可以调用mail.send_mail来发送邮件啦

import mail

mail.send_mail('hello world!','这是毯毯狐的邮件!',['dim****g@*******.com'])

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注