I recently change my email from plain text to html.
This includes the registration verification email, as well as any other email regarding updates and notifications.
It works fine in development, but not in production.
In my production setting, django is unable to find the image and gives me the following error:
I have tried everything and is now out of ideas, hoping someone here ran into the same issue and can help me please.
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.user_email = user.email
user.is_active = False # Deactivate account till it is confirmed
user.save()
# Email with html template
from_email = settings.DEFAULT_FROM_EMAIL
to_email = user.email
subject = 'Activate Your Telos Central Account'
image_path = 'media'
image_name = 'logo256.png'
file_path = os.path.join(image_path, image_name)
text_content = "Text content: Hi {{ user.username }}, Please click on the link to confirm your registration, https://{{ domain }}{% url 'activate' uidb64=uid token=token %} If you think, it's not you, then just ignore this email."
html_tmp = get_template('email/account_activation_email.html')
ctx = {
'user': user,
'uid': urlsafe_base64_encode(force_bytes(
user.pk)),
'token': account_activation_token.make_token(user),
}
html_content = html_tmp.render(ctx)
msg = EmailMultiAlternatives(subject,
text_content,
from_email,
[to_email])
msg.attach_alternative(html_content, 'text/html')
msg.content_subtype = 'html' # set the primary content to be text/html
msg.mixed_subtype = 'related' # it is an important part that ensures embedding of an image
# attached image to the message before sending the mail
with open(file_path, mode='rb') as f:
image = MIMEImage(f.read())
msg.attach(image)
image.add_header('Content-ID', '<{name}>'.format(name=image_name))
image.add_header('Content-Disposition', 'inline', filename=image_name)
msg.send()
# Show message that mail has been sent
messages.success(request, ('Please Confirm your email to complete registration.'))
# Go to login page
return redirect('login')
return render(request, self.template_name, {'form': form})