如何即时为每封电子邮件设置 SMTP 设置?
How to set SMTP settings for each email on the fly?
我们可以通过在 settings.py
:
中定义这些变量来全局配置 SMTP 设置
EMAIL_HOST = 'SMTP_HOST'
EMAIL_PORT = 'SMTP_PORT'
EMAIL_HOST_USER = 'SMTP_USER'
EMAIL_HOST_PASSWORD = 'SMTP_PASSWORD'
我想为每封电子邮件设置不同的参数。如何即时定义 SMTP 设置?我的意思是在发送之前。
EmailMessage
class 为您提供此选项
第一步是配置您的电子邮件后端
from django.core.mail import EmailMessage
from django.core.mail.backends.smtp import EmailBackend
connection = EmailBackend(
host=your_host, port=your_port, username=your_username,
password=your_password, use_tls=True, fail_silently=fail_silently
)
然后您所要做的就是使用您的connection
设置
email = EmailMessage(
subject=subject,
body=body,
from_email=from_email,
to=[list_of_recipents, ],
connection=connection
)
email.send()
我们可以通过在 settings.py
:
EMAIL_HOST = 'SMTP_HOST'
EMAIL_PORT = 'SMTP_PORT'
EMAIL_HOST_USER = 'SMTP_USER'
EMAIL_HOST_PASSWORD = 'SMTP_PASSWORD'
我想为每封电子邮件设置不同的参数。如何即时定义 SMTP 设置?我的意思是在发送之前。
EmailMessage
class 为您提供此选项
第一步是配置您的电子邮件后端
from django.core.mail import EmailMessage
from django.core.mail.backends.smtp import EmailBackend
connection = EmailBackend(
host=your_host, port=your_port, username=your_username,
password=your_password, use_tls=True, fail_silently=fail_silently
)
然后您所要做的就是使用您的connection
设置
email = EmailMessage(
subject=subject,
body=body,
from_email=from_email,
to=[list_of_recipents, ],
connection=connection
)
email.send()