-
-
Save renmu123/18dcdc0798712ae3e8c900cd75d3e94f to your computer and use it in GitHub Desktop.
tools
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
import smtplib | |
from email.header import Header | |
from email.mime.application import MIMEApplication | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.utils import formataddr | |
from os import path | |
from .config import * | |
from typing import List, Optional | |
def send_mail( | |
addresses: List[str], | |
subject: str, | |
content: str, | |
attached_files: List = None, | |
cc_addresses=None, | |
): | |
if cc_addresses is None: | |
cc_addresses = [] | |
sender = SENDER # 发件人邮箱账号 | |
password = PASSWORD # 发件人邮箱密码(当时申请smtp给的口令) | |
msg = MIMEMultipart() | |
msg["From"] = formataddr([FROM, sender]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号 | |
msg["To"] = ",".join(addresses) # 括号里的对应收件人邮箱昵称、收件人邮箱账号 | |
msg["Cc"] = ",".join(cc_addresses) | |
msg["Subject"] = subject # 邮件的主题,也可以说是标题 | |
msg.attach(MIMEText(content, "html", "utf-8")) | |
if attached_files: | |
# 构造附件 | |
for attached_file in attached_files: | |
att1 = MIMEApplication(open(attached_file, "rb").read()) | |
att1["Content-Type"] = "application/octet-stream" | |
attached_file_name = path.basename(attached_file) | |
att1.add_header( | |
"Content-Disposition", | |
"attachment", | |
filename=(Header(attached_file_name, "utf-8").encode()), | |
) | |
msg.attach(att1) | |
with smtplib.SMTP_SSL(SMTP, SMTP_PROT) as server: | |
server.login(sender, password) # 括号中对应的是发件人邮箱账号、邮箱密码 | |
server.sendmail( | |
sender, addresses + cc_addresses, msg.as_string() | |
) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件 | |
print("success") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment