0

我想使用两封电子邮件,一封用于验证电子邮件,另一封用于发送正常的信息电子邮件。我可以使用以下代码发送电子邮件以进行验证,并且我想将我的另一封电子邮件用于第二个目的。

视图.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>'

请帮忙!!!

4

1 回答 1

1

如果您希望将不同的电子邮件作为“收件人电子邮件”发送,只需在参数中添加不同的电子邮件。更多信息在这里Django 电子邮件

如果您想要一个不同的电子邮件服务器来发送电子邮件,请考虑制作您自己的电子邮件后端,该后端基于特定的方法/功能/参数使用您正在寻找的特定电子邮件服务器。例如:

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()

希望这可以帮助 :)

于 2019-06-15T21:36:15.580 回答