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:
Exception Type:
FileNotFoundErrorException Value:
[Errno 2] No such file or directory: '/media/logo256.png'

I have tried everything and is now out of ideas, hoping someone here ran 
into the same issue and can help me please.

Here is my code from the views.py file:

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,
                'domain': 'teloscentral.com',
                '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})

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/52bd056e-421c-4e5e-8777-ee4288899f06n%40googlegroups.com.

Reply via email to