Created
August 12, 2021 12:38
-
-
Save growvv/a0cc69df948b197f3e283d8a353a55d6 to your computer and use it in GitHub Desktop.
发送邮件Python函数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from smtplib import SMTPException, SMTP_SSL | |
from email.mime.text import MIMEText | |
from email.header import Header | |
email_to = "xx@qq.com" | |
email_from = "xxx@qq.com" | |
smtp = "xxxx" | |
# 使用邮箱发送通知功能 | |
def send_email(subject, msg): | |
# 三个参数:第一个为文本内容,第二个为plain设置文本格式,第三个为utf-8设置编码 | |
message = MIMEText(msg,"plain",'utf-8') | |
message ['From'] = Header(email_from,'utf-8') | |
message ['To'] = Header(email_to,'utf-8') | |
message["Subject"] = Header(subject,"utf-8") | |
try: | |
# 使用非本地服务器,需要建立ssl连接 | |
smtpObj = SMTP_SSL("smtp.qq.com",465) | |
smtpObj.login(email_from,smtp) | |
smtpObj.sendmail(email_from,email_to,message.as_string()) | |
print("邮件发送成功") | |
except SMTPException as e: | |
print("Error:无法发送邮件.Case:%s"%e) | |
send_email("测试", "hello world") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment