Embedded Images in emails

17 views
Skip to first unread message

Bit Rate

unread,
Oct 4, 2021, 8:58:22 AM10/4/21
to Django users
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})

Kasper Laudrup

unread,
Oct 4, 2021, 1:05:40 PM10/4/21
to django...@googlegroups.com
On 04/10/2021 12.30, Bit Rate wrote:
> [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.
>

Have you tried checking if there is a file located at '/media/logo256.png'?

Is that part of "everything"?

Kind regards,

Kasper Laudrup
OpenPGP_0xE5D9CAC64AAA55EB.asc
OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages