2016-11-16 2 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 = Trueの 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() 
関連する問題