Mike IT Mike IT - 21 days ago 8
Python Question

django send_mail not sending mail

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

Answer

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