1

I am just trying to send Emails as part of a django contact page.

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 
    })        

It doesn't do anything. The response to the request is 200. No stacktrace.

I have the following settings in 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'

The email account is able to send email via software. I tested that already with

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()

I have no idea why it is not working.

Any help is much appreciate it. Mike

jape
  • 2,861
  • 2
  • 26
  • 58
Mike IT
  • 329
  • 1
  • 3
  • 19
  • What's the email backend ? https://docs.djangoproject.com/en/1.10/topics/email/#email-backends – bruno desthuilliers Nov 16 '16 at 15:10
  • I am using django 1.7. apparently reply_to is not available. – Mike IT Nov 16 '16 at 15:28
  • @brunodesthuilliers where am supposed to put the back-end configurations. I have put all these in settings.py. 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' – Mike IT Nov 16 '16 at 15:36
  • I can send email but sender is always the same as EMAIL_HOST_USER and I am using Gmail. What would be the reason? I mean the smpt server is not allowing me to set the sender to some value other than EMAIL_HOST_USER. That is the only possible scenario I could think of. In a business smpt server I should not have the same issue. Correct ???? – Mike IT Nov 16 '16 at 15:39
  • > where am supposed to put the back-end configurations – bruno desthuilliers Nov 16 '16 at 15:49
  • With Django 1.7 you can set the `Reply-To` header. I've added the example back to my answer. Note that Django 1.7 has been end of life for nearly a year, and does not get security updates. – Alasdair Nov 16 '16 at 16:17
  • @Alasdair thank you it worked perfecto. I provide that as an answer I can upvote it. Regards – Mike IT Nov 16 '16 at 19:39

1 Answers1

0

in djgano 1.7, you only can add reply-to field throgh header like below:

Let me know if that works. cheers

            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()
user1941390
  • 510
  • 6
  • 18