👉 ​​自定义邮件后端​

主要针对 EmailMessage 内 connection 参数的使用

from django.core.mail import EmailMessage
from django.core.mail.backends.smtp import EmailBackend


def send(subject: str, content: str, send_mailer: str,
receive_mailer: list, host: str, username: str,
password: str) -> int:
em_obj = EmailMessage(
subject,
content,
send_mailer,
receive_mailer,
connection=EmailBackend(
host=host,
username=username,
password=password,
)
)
return em_obj.send()


if __name__ == '__main__':
params = {
'subject': '主题',
'content': '内容',
'send_mailer': '发件人',
'receive_mailer': ['收件人1', '收件人2'],
'host': 'smtp.qq,com', # 按需切换不同邮件服务器
'username': '认证账户名称',
'password': '认证账户密码',
}
send(**params)