I know that there have been questions like this on the site, but none of them have been able to answer my difficulty. So I have the code as following:
# custom_methods.py
from django.core.mail import EmailMultiAlternatives
from django.conf import settings
from pathlib import Path
from email.mime.image import MIMEImage
import threading, random, os
class EmailThread(threading.Thread):
def __init__(self, subject, body, from_email, recipient_list, fail_silently, html):
self.subject = subject
self.body = body
self.from_email = from_email
self.recipient_list = recipient_list
self.fail_silently = fail_silently
self.html = html
threading.Thread.__init__(self)
def run(self):
msg = EmailMultiAlternatives(self.subject, self.body, self.from_email, self.recipient_list)
image_path = os.path.join(settings.BASE_DIR, '/static/images/instagram_icon.png')
image_name = Path(image_path).name
if self.html:
msg.attach_alternative(self.html, "text/html")
msg.content_subtype = 'html'
msg.mixed_subtype = 'related'
with open(image_path, 'rb') as f:
image = MIMEImage(f.read())
image.add_header('Content-ID', f"<{image_name}>")
msg.attach(image)
msg.send(self.fail_silently)
def send_mail(subject, recipient_list, body='', from_email=settings.EMAIL_HOST_USER, fail_silently=False, html=None, *args, **kwargs):
EmailThread(subject, body, from_email, recipient_list, fail_silently, html).start()
# views.py
class UserRegisterView(CreateView):
model = User
form_class = CustomUserCreationForm
template_name = 'accounts/register.html'
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.is_active = False
self.object.save()
send_mail(
subject=f'{self.object.code} is your Instagram code',
recipient_list=[self.object.email],
html=render_to_string('accounts/register_email.html', {
'email':self.object.email,
'code':self.object.code,
})
)
return redirect('accounts:login')
# register_email.html - This is the html that is connected to the email that will be sent
{% load static %}
{% load inlinecss %}
{% inlinecss "css/email.css" %}
<img src="cid:{image_name}">
<h1>Hi,</h1>
<h2>Someone tried to sign up for an Instagram account with {{ email }}. If it was you, enter this confirmation code in the app:</h2>
<h2>{{ code }}</h2>
{% endinlinecss %}
So what suppose to happen is the following:
- Register the user
- When the user is registered, an email is sent to the user with the html template I've shown above.
Now everything works fine, but it goes wrong when I register the user, and in the Powershell, the following error pops:
with open(image_path, 'rb') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:/static/images/instagram_icon.png'
Of course the image cannot be found in the 'C:/static/images/instagram_icon.png'
, because the absolute directory of the image would be in 'C:Users/...(and so on).../static/images/instagram_icon.png'
. Then how do I fix this problem in the development stage? I couldn't find the answer for this and can anybody help??