I tried to send some files in mail using Django EmailMessage
class
The files attachment that I want to send are in DB, provided by each user during registration to my site.
I'have tried this but it does not work
myapp/views.py:
from django.core.mail import EmailMessage
def contactUber(request,id):
user = User.objects.get(id=id)
msg = EmailMessage(
'Hello', #subject
'Body goes here', #content
'vitalysweb@gmail.com', #from
['uber@uber.com'] #to
)
msg.attach_file(user.profile.id_card.url) #the file that i want to attach
msg.send()
messages.success(request, "Email envoyé avec succes") #success msg
return redirect(user_detail, id=str(id))
myapp/models:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
birth_date = models.DateField(('Date de naissance'), null=True, blank=True)
#DOCUMENTS TO UPLOAD
id_card = models.FileField(('Carte Nationale d\'Identité'), upload_to='documents/CNI')
drive_licence = models.FileField(('Permis de conduire'), upload_to='documents/RIB')
uploaded_at = models.DateTimeField(('Ajouté le'), auto_now=True)
docs_are_checked = models.BooleanField(('Documents validés'), default=False)
Traceback:
Traceback (most recent call last):
File "C:\venvs\env1\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\venvs\env1\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\venvs\env1\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\djangoprojects\mysite\mysite\core\views.py", line 272, in contactUber
msg.attach_file(user.profile.id_card.url) #the file that i want to attach
File "C:\venvs\env1\lib\site-packages\django\core\mail\message.py", line 394, in attach_file
with open(path, 'rb') as file:
FileNotFoundError: [Errno 2] No such file or directory: '/media/documents/CNI/1847635-2524541_FZLHlIr.jpg'
[16/Aug/2017 12:10:27] "GET /core/send_mail_uber/16/ HTTP/1.1" 500 73307
My question is : How to fix this