Hello!
So I managed to integrate Anymail with Django and send an email from the command line by sending a test email (python manage.py sendtestmail user@example.com). The email was sent to my outlook inbox.
This email is sent when I switch to my production settings where I have my production email settings and Anymail set up. Everything works just fine in development as well.
However, when I create a user account on the actual live website, the email is not sent. I see the email content in the Heroku logs, but not in my inbox and there are no signs of any error of any form. The account is created and the user is asked to sign in again.
This is the CBV that handles user registration and user register confirmation:
class SignUpView(CreateView):
model = Member
template_name = "auth_app/signup.html"
form_class = CustomUserCreationForm
success_url = reverse_lazy("auth_app:signin")
def dispatch(self, request, *args, **kwargs):
""" Redirect authenticated users """
if request.user.is_authenticated:
return redirect("main_app:roomsPage")
return super().dispatch(request, *args, **kwargs)
def account_created_confirmation(self, username, user_email):
subject = f"Welcome, {username}"
from_email = os.environ.get("DEFAULT_FROM_EMAIL", "user@example.com")
to_new_user = user_email
text_content = render_to_string(template_name='auth_app/receipt_email.txt', context = {"name": username})
html_content = render_to_string(template_name='auth_app/receipt_email.html', context = {"name": username})
try:
email = EmailMultiAlternatives(subject=subject,
body=text_content,
from_email=from_email,
to=[user_email])
email.attach_alternative(html_content, "text/html")
email.send(fail_silently=False)
except Exception as e:
confirmation_email_sending_failed.error(f"Account register confirmation failed for user {username} with email: {user_email}. Error: {e}")
raise Exception(f"Email sending failed to {to_new_user}. Error: {e}")
def form_invalid(self, form):
# Override form_invalid to ensure self.object is not accessed
context = self.get_context_data(form=form)
context.pop('object', None) # Remove 'object' from context if it exists
return self.render_to_response(context)
def form_valid(self, form):
policy_approved = form.cleaned_data["approve_policy"]
user_profile_type = form.cleaned_data["profile_type"]
username = form.cleaned_data["username"]
user_email = form.cleaned_data["email"]
if policy_approved:
response = super().form_valid(form)
self.account_created_confirmation(username, user_email)
messages.success(self.request, _("Your account have been created. Please sign in to continue."))
return response
else:
form.add_error("approve_policy", _("Please accept the user policy"))
return self.form_invalid(form)
And here is the email settings:
# Email back-end
EMAIL_BACKEND = 'anymail.backends.mailgun.EmailBackend'
ANYMAIL = {
'MAILGUN_API_KEY': os.environ.get("MAILGUN_API_KEY"),
'MAILGUN_SENDER_DOMAIN': os.environ.get("MAILGUN_SENDER_DOMAIN"),
'MAILGUN_API_URL': os.environ.get("MAILGUN_API_URL"),
}
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = os.environ.get("DEFAULT_FROM_EMAIL")
SERVER_EMAIL = os.environ.get("SERVER_EMAIL")
What is wrong here?
Appriciate all suggestions!