sklarsa / django-sendgrid-v5

An implementation of Django's EmailBackend compatible with sendgrid-python v5+
MIT License
319 stars 54 forks source link

Not sending emails #4

Closed AdrienLemaire closed 6 years ago

AdrienLemaire commented 6 years ago

I've been trying to write a simple view to send emails, but I keep getting 0 as result from send_mail

'''
Backend views
'''
from smtplib import SMTPException

from django.http import JsonResponse
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.conf import settings
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt

@require_POST
@csrf_exempt
def contact_email(request):
    '''
    Send an email to the user when he press the contact form
    '''
    email = request.POST.get('email')
    subject = 'Subject'
    message = ''  # Using html message instead
    html_message = render_to_string('email.html', {'email': email})
    from_email = settings.DEFAULT_FROM_EMAIL

    try:
        result = send_mail(subject, message, from_email, [email],
                  html_message=html_message,
                  fail_silently=False)
        if result == 0:
            return JsonResponse({
                'status': 'error',
                'errorMessage': 'Could not send email',
            }, status=400)
        return JsonResponse({
            'status': 'success'
        }, status=200)
    except SMTPException:
        return JsonResponse({
            'status': 'error',
            'errorMessage': 'Could not send email',
        }, status=400)

Might you have any idea as to why it happens, and how to debug this issue ?

AdrienLemaire commented 6 years ago

stupid me, wasn't doing the curl query correctly when testing it :)