django send_mail不发送邮件

时间:2016-11-16 14:27:59

标签: python django

我只是想发送电子邮件作为django联系页面的一部分。

from django.shortcuts import render
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django import forms
from django.core.mail import send_mail, EmailMessage
from StudioHanel.forms import ContactForm
import traceback
import time

def index(request):
    return render(request, 'StudioHanel/index.html')

def contact(request):

    send_mail(
    'Subject here',
    'Here is the message.',
    'xx@gmail.com',
    ['xx@gmail.com'],
    fail_silently=False,
    )

    mystack = ''
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid() or True:
            subject = form.cleaned_data['subject']
            message = form.cleaned_data['message']
            sender = form.cleaned_data['sender']

            # recipients = ['cordelia@studio-hanel.com', 'sylvia@studio-hanel.com', 'admin@studio-hanel.com'] 
            recipients = [ 'xx@gmail.com'] 
            send_mail(subject, message, sender, recipients, fail_silently=False)
            time.wait(10)
            # EmailMessage(subject, message, sender, to = recipients)
            return HttpResponse('success')

    else:
        form = ContactForm()

    return render(request, 'StudioHanel/contact.html', {
        'form': form, 'mystack': mystack 
    })        

它没有做任何事情。对请求的响应是200.没有堆栈跟踪。

我在settings.py中有以下设置。

# Email settings
DEFAULT_FROM_EMAIL = 'xx@gmail.com'
SERVER_EMAIL = 'xx@gmail.com'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'xx@gmail.com'
EMAIL_HOST_PASSWORD = 'xx'

电子邮件帐户可以通过软件发送电子邮件。我已经用

进行了测试
import smtplib
fromaddr = 'xx@gmail.com'
toaddrs  = 'xx@gmail.com'
msg = 'Why,Oh why!'
username = 'xx@gmail.com'
password = 'xx'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

我不知道它为什么不起作用。

任何帮助都非常感激。 麦克

1 个答案:

答案 0 :(得分:0)

在djgano 1.7中,你只能添加回复字段throgh标题,如下所示:

如果有效,请告诉我。欢呼声

            recipients = [ 'mike.xxx@gmail.com'] 
            bcc = []
            logger.debug('Contact Before Sending Email!') 
            headers = {'Reply-To': sender}
            email = EmailMessage(
                subject,
                message,
                sender,
                recipients,
                bcc,
                headers = headers,                    
            )

            email.send()
相关问题