2017-08-16 172 views
0

我试图使用Django EmailMessage的Django发送电子邮件与attachement

中的文件附件,我想送都在DB,注册到我的网站中每个用户提供发送邮件的一些文件。

I'have试过,但它不工作

的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 
     '[email protected]', #from 
     ['[email protected]'] #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 /型号:

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 (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 

我的问题是:如何解决这个问题

+0

和你有什么问题吗?它是如何解决这个问题的?为什么会发生?或者是什么?请在SO上发布时清楚。 –

+0

@ N.Ivanov编辑完成 –

回答

0

由于文件存储在您的数据库,它可能不会在这个位置保存/media/documents/CNI/1847635-2524541_FZLHlIr.jpg。所以你需要做的是使用attach()方法指定文件名,数据和MIME类型。这里是它应该如何工作的一个例子:

msg.attach(user.profile.id_card.name, user.profile.id_card.read(), user.profile.id_card.content_type) 

你可以在Django Docs了解更多。寻找attach(),并知道它是不同于attach_file()

编辑1

既然你在评论中提到id_card抛出一个错误,我假设你没有获取正确的模型。从我所看到的情况来看,我不确定你是否正确地做了这件事。你应该通过以下方式获取Profile模型实例:

someVar = Profile.objects.get(user=request.user) # assuming the current user is the one that you would like to be 

,然后以附加的文件使用:

msg.attach(someVar.id_card.name, someVar.id_card.read(), 'image/png') # assuming you will be attaching png's only 

希望这有助于!

+0

id_card没有定义(NameError) –

+0

@VITALYSWEB我已经更新了我的答案,应该是'user.profile.id_card',如果这不起作用,那么你可能没有获取正确的模型。 –

+0

@VITALYSWEB我在**编辑1 **下更新了我的答案。希望这可以帮助! –