Django发送电子邮件

2024-10-02 12:30:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道有类似的问题,但我的问题是很奇怪。那个我在联系人页面中提交的电子邮件无法获得已发送。取而代之我收到一封电子邮件。你知道吗

这是我在伦敦做的设置.py地址:

              EMAIL_HOST='smtp.gmail.com'
              EMAIL_HOST_USER= 'my gmail adress'
              EMAIL_HOST_PASSWORD= 'my password'
              EMAIL_PORT='587'
              EMAIL_USE_TLS=True

在视图.py-联系人

    from django.shortcuts import render
    from django.core.mail import send_mail
    from django.conf import settings
    from .forms import contactForm
    # Create your views here.
    def contact(request):
        title = 'Contact Us'
        form = contactForm(request.POST or None)
        comfirm_message=None

        if form.is_valid():
            name = form.cleaned_data['name']
            comment = form.cleaned_data['comment']
            subject = 'Message from a user of Archives Manager'
            emailFrom = form.cleaned_data['email']

            message = '%s %s' % (comment, name)

            emailTo = [settings.EMAIL_HOST_USER]
            send_mail(subject, message, emailFrom, emailTo, fail_silently=True)
            title = "Thanks!"
            comfirm_message='Thanks for the message. we will get right back to you.'
            form=None

        context = {'title': title, 'form' : form, 'comfirm_message': comfirm_message, }
        template = 'contact.html'
        return render(request,template,context)

我在我的gmail账户里收到的是来自我邮箱地址的信息,而不是我在EmailField()中提交的信息?你知道吗

    from django import forms

    class contactForm(forms.Form):
        name = forms.CharField(required=False,max_length=100, help_text="100 is the maximum length" )
        email = forms.EmailField(required=True)
        comment = forms.CharField(required=True,widget=forms.Textarea)

Tags: djangonamefromimportformtruehostmessage

热门问题