2016-11-16 62 views
1

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

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.', 
    '[email protected]', 
    ['[email protected]'], 
    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 = ['[email protected]', '[email protected]', '[email protected]'] 
      recipients = [ '[email protected]'] 
      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 = '[email protected]' 
SERVER_EMAIL = '[email protected]' 
EMAIL_USE_TLS = True 
EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_PORT = 587 
EMAIL_HOST_USER = '[email protected]' 
EMAIL_HOST_PASSWORD = 'xx' 

该电子邮件帐户能够通过软件发送电子邮件。我测试了一下已经与

import smtplib 
fromaddr = '[email protected]' 
toaddrs = '[email protected]' 
msg = 'Why,Oh why!' 
username = '[email protected]' 
password = 'xx' 
server = smtplib.SMTP('smtp.gmail.com:587') 
server.ehlo() 
server.starttls() 
server.login(username,password) 
server.sendmail(fromaddr, toaddrs, msg) 
server.quit() 

我不知道为什么它不工作。

任何帮助是非常感谢它。 迈克

在djgano 1.7
+0

什么是电子邮件后端? https://docs.djangoproject.com/en/1.10/topics/email/#email-backends –

+0

我正在使用django 1.7。显然reply_to不可用。 –

+0

@brunodesthuilliers我应该放置后端配置。我已经把所有这些放在settings.py中。 DEFAULT_FROM_EMAIL = '[email protected]' SERVER_EMAIL = '[email protected]' EMAIL_USE_TLS =真 EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_HOST_USER = '[email protected]' EMAIL_HOST_PASSWORD = 'xx' –

回答

0

,你只能添加回复到现场throgh头象下面这样:

让我知道是否可行。欢呼声

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

      email.send()