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.