本文介绍了“ from_email”未与“ send_mail” smtp一起显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用gmail设置了smtp。当我使用send_mail时,发件人的电子邮件不会显示在接收电子邮件的帐户中。

I have set up smtp with gmail. When I use send_mail the from email is not showing up in the account receiving the email.

Django settings.py

Django settings.py

# DEFAULT_FROM_EMAIL = '[email protected]'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = '**********'
EMAIL_USE_TLS = True

使用

$ python manage.py shell

我按以下方式发送邮件,

I send the mail as follows,

>>> from django.core.mail import send_mail
>>> send_mail('subject is', 'message is and is not 12342', '[email protected]', ['[email protected]'])
1
>>>

我在我的gmail帐户(这是用于smtp的gmail帐户)中收到此电子邮件),但发件人电子邮件显示为[email protected],应该是[email protected]

I am receiving this email in my gmail account, (which is the same gmail account used for the smtp), but the from email is showing up as the [email protected] and should be [email protected]

推荐答案

I使用以下视图解决了该问题:

I solved the problem using this view:

def contact(request):
    form = ContactForm(data=request.POST or None)

    if form.is_valid():
        subject = form.cleaned_data['sujet']
        message = form.cleaned_data['message']
        sender = form.cleaned_data['envoyeur']

        msg_mail = str(message) + " " + str(sender)

        send_mail(sujet, msg_mail, sender, ['[email protected]'], fail_silently=False)

    return render(request, 'blog/contact.html', locals())

我实际上将发件人的电子邮件附加到邮件中。您甚至可以从send_mail中删除sender参数。您只需要使EmailField为必填项即可确保您将获得发件人的电子邮件地址。

I actually append the email of the sender to the message. You could even remove the sender argument from send_mail. You just have to make your EmailField mandatory to make sure you'll get the sender's email address.

这篇关于“ from_email”未与“ send_mail” smtp一起显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 18:10