使用两个不同的电子邮件在 Django 中发送电子邮件

Using two different email fo sending eemail in Django

我想使用两封电子邮件,一封用于验证电子邮件,另一封用于发送普通的信息性电子邮件。我可以使用以下代码发送电子邮件进行验证,并且我想使用我的另一封电子邮件进行第二次验证。

views.py

    current_site = get_current_site(request)
    subject = 'Welcome to MySite! Confirm Your email.'
    htmly     = get_template('account_activation_email.html')

    d = { 'user': user, 'domain':current_site.domain, 'uemail':urlsafe_base64_encode(force_bytes(user.email)), 'uid':urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user)}

    text_content = ""
    html_content = htmly.render(d)
    msg = EmailMultiAlternatives(subject, text_content, '', [user.email])
    msg.attach_alternative(html_content, "text/html")
    try:
        msg.send()
    except BadHeaderError:
        print("Error while sending email!")
    user.save()

并在设置中:

EMAIL_USE_TLS = True
EMAIL_HOST = config('EMAIL_HOST')
EMAIL_HOST_USER = verify@mysite.com
EMAIL_HOST_PASSWORD = 'randompass'
EMAIL_PORT = config('EMAIL_PORT')
DEFAULT_FROM_EMAIL = 'MySte Verification <verify@mysite.com>'

请帮忙!!!

如果您希望将不同的电子邮件发送为 "To email",只需在参数中添加不同的电子邮件。更多信息请点击此处 Django Email

如果您想要一个不同的电子邮件服务器来发送电子邮件,请考虑制作您自己的 email backend,它基于特定 methods/functions/params 使用您正在寻找的特定电子邮件服务器。例如:

from django.core.mail import get_connection, send_mail
from django.core.mail.message import EmailMessage

with get_connection(
    host=my_host, 
    port=my_port, 
    username=my_username, 
    password=my_password, 
    use_tls=my_use_tls
) as connection:
    EmailMessage(subject1, body1, from1, [to1],
                 connection=connection).send()

希望这对您有所帮助:)