Sending email with Mailgun and Anymail

Hello!

I try to send email from a website that I’ve deployed but the emails are not sent.

Could someone point out my error?

In my production settings, I have the following set up:

# Email settings
EMAIL_BACKEND = 'anymail.backends.mailgun.EmailBackend'

ANYMAIL = {
    'MAILGUN_API_KEY': os.environ.get("MAILGUN_API_KEY"),
    'MAILGUN_SENDER_DOMAIN': os.environ.get("MAILGUN_DOMAIN"),
}

EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = os.environ.get("DEFAULT_FROM_EMAIL")
SERVER_EMAIL = os.environ.get("SERVER_EMAIL")

And in the .env file, I have set up the following:

# Email settings
MAILGUN_API_KEY = "AAAAAAAAAAAAAAAAAA"
MAILGUN_DOMAIN = "AAAAAAAAAAAAAAAAAA"
MAILGUN_PUBLIC_KEY = "AAAAAAAAAAAAAAAAAA"

# These variables are used in the settings file
MAILGUN_SMTP_LOGIN = "AAAAAAAAAAAAAAAAAA"
MAILGUN_SMTP_PASSWORD = "AAAAAAAAAAAAAAAAAA"
MAILGUN_SMTP_PORT = 587
MAILGUN_SMTP_SERVER = "smtp.mailgun.org"

ADMIN_EMAIL_ADDRESS = "AAAAAAAA@AAAA.com"
DEFAULT_FROM_EMAIL = "admin@AAAAA.com"
SERVER_EMAIL = "error-reporting@AAAAA.com"

And this is the view for handling the contact section:

def contact_view(request):
    if request.method == "POST":
        form = ContactForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data["email"]
            subject = form.cleaned_data["subject"]
            message = form.cleaned_data["message"]
                        
            send_mail(
                subject=subject,
                message=message,
                from_email=email,
                recipient_list=[os.environ.get("ADMIN_EMAIL_ADDRESS")],
                fail_silently=True,
            )
            messages.success(request, "Thank you! We'll reach to you in a few days.")
            return HttpResponseRedirect(reverse("main_app:contact"))
    else:
        form = ContactForm()
    
    context = {"contact_form": form}
    return render(request, "main_app/contact.html", context)

During the development, I ran the python manage.py sendtestemail testuser@outlook.com and received a success response.

I haven’t used Mailgun before, but I can see an issue with how you’re sending the email. I assume this is a contact form on your website that let’s people send you messages - is that correct?

If so, the problem will be with this line: from_email=email. I highly doubt this will work with any Mail provider. Usually when setting up an account with a mail provider, you need to validate which domains or email addresses are permitted to send mail through your account.

But the way you’ve set up the form, anyone who submits the form will have their email address in the “from” field, and their email domain wouldn’t have been authorised with Mailgun, resulting in the email being blocked. The fix will be to change the from_email field to one of your own email addresses using a domain that you have authorised with Mailgun.

So when you receive the email, it will look like it is being sent from you, to you. But in the body of the email you can customise the content so you know who sent the email. e.g. something like this:

message=f"Message sent from: {email}\nMessage content: {message}"

Hope that helps.

(edited for clarity)

Hello!

You are correct! I want every visitor to my page to be able to email me.

I read the documentation for Mailgun and cPanel. My domain provider use cPanel in which I configured the DNS settings for Mailgun.

Now I have to wait 24-48 h in order for the changes to propagate.

Hopefully everything works in a few days.

Thank you for guiding me!