使用Python发送邮件
导读:使用Python发送邮件之前写了一个小工具从远程服务器下载文件,然后通过附件方式发送给自己的邮箱,中间用到了Python里跟邮件相关的两个标准库email和smtp,使用也并不复杂。import smtplib import ssl fro...
使用Python发送邮件
之前写了一个小工具从远程服务器下载文件,然后通过附件方式发送给自己的邮箱,中间用到了Python里跟邮件相关的两个标准库email
和smtp
,使用也并不复杂。
import smtplib
import ssl
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from typing import List, Tuple
from app.settings import config
context = ssl.create_default_context() # 初始化ssl上下文
def make_message(sender_email: str, receiver_email: str, subject: str,
content: str,
attachments: List[Tuple[str, bytes]]) ->
MIMEBase:
message = MIMEMultipart() # 创建一个Message对象并设置邮件的基本信息
message['From'] = sender_email
message['To'] = receiver_email
message['Subject'] = subject
message['Bcc'] = receiver_email
message.attach(MIMEText(content, 'plain'))
for name, binary_content in attachments: # 添加附件
attachment = MIMEBase("application", "octet-stream")
attachment.set_payload(binary_content)
encoders.encode_base64(attachment)
attachment.add_header(
"Content-Disposition",
f"attachment;
filename= {
name}
",
)
message.attach(attachment)
return message
def send_email(receiver_email: str, subject: str, content: str,
attachments: List[Tuple[str, bytes]]):
with smtplib.SMTP_SSL(config.host, config.port, context=context) as server: # 配置smtp发送服务器
sender_email, password = config.email, config.password
server.login(sender_email, password) # 登录邮箱账户
message = make_message(sender_email, receiver_email, subject, content,
attachments)
server.send_message(message, sender_email, receiver_email)
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 使用Python发送邮件
本文地址: https://pptw.com/jishu/6117.html