如何在django发送电子邮件

时间:2018-02-14 07:12:50

标签: django email

我在settings.py中的设置,但没有向gmail帐户发送任何邮件。 请你告诉我如何发送确认邮件到邮件enter code here

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587

我在cmd中收到成功邮件,但没有向我的身份证件发送电子邮件

def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.save()
            current_site = get_current_site(request)
            print(current_site)
            subject = 'Activate Your MySite Account'
            message = render_to_string('account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            })
            from_email =settings.EMAIL_HOST_USER
            print(from_email,"email here")
            # to_email = [from_email,'sridharadhi50@gmail.com']
            to_email = form.cleaned_data.get('email')
            email = EmailMessage(
                subject, message, to=[to_email]
            )
            email.send()
            # user.email_user(subject, message)
            return redirect('account_activation_sent')enter code here

2 个答案:

答案 0 :(得分:0)

在你的python文件中

def get_email_connection():
 use_tls = True
 use_ssl = False
 fail_silently=False
 connection = get_connection(host=config.email_host, 
                        port=settings.EMAIL_PORT, 
                        username=settings.EMAIL_HOST_USER, 
                        password=settings.EMAIL_HOST_PASSWORD, 
                        use_tls=use_tls,
                        use_ssl=use_ssl,
            fail_silently=fail_silently)
 return connection



def signup(request):
 if request.method == 'POST':
    form = SignUpForm(request.POST)
    if form.is_valid():
        user = form.save(commit=False)
        user.save()
        connection=get_email_connection()
        current_site = get_current_site(request)
        print(current_site)
        subject = 'Activate Your MySite Account'
        message = render_to_string('account_activation_email.html', {
            'user': user,
            'domain': current_site.domain,
            'uid': urlsafe_base64_encode(force_bytes(user.pk)),
            'token': account_activation_token.make_token(user),
        })
        from_email =settings.EMAIL_HOST_USER
        print(from_email,"email here")
        # to_email = [from_email,'sridharadhi50@gmail.com']
        to_email = form.cleaned_data.get('email')
        email = EmailMessage(
            subject, message,settings.EMAIL_HOST_USER, [to_email],connection=connection
        )
        email.send()
        # user.email_user(subject, message)
        return redirect('account_activation_sent')enter code here

答案 1 :(得分:0)

您应该提供EMAIL_BACKEND

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'mail@gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587

工作正常。