2016-11-16 61 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()