2015-10-19 161 views
4

我的應用程序使用django-wkhtmltopdf生成pdf報告。我希望能夠將pdf附加到電子郵件併發送。附加pdf到django的電子郵件

這裏是我的PDF查看:

class Report(DetailView): 
    template = 'pdf_reports/report.html' 
    model = Model 

    def get(self, request, *args, **kwargs): 
     self.context['model'] = self.get_object() 

     response=PDFTemplateResponse(request=request, 
            template=self.template, 
            filename ="report.pdf", 
            context=self.context, 
            show_content_in_browser=False, 
            cmd_options={'margin-top': 0, 
                'margin-left': 0, 
                'margin-right': 0} 
            ) 
     return response 

這裏是我的電子郵件看法:

def email_view(request, pk): 
    model = Model.objects.get(pk=pk) 
    email_to = model.email 
    send_mail('Subject here', 'Here is the message.', 'from', 
    [email_to], fail_silently=False) 

    response = HttpResponse(content_type='text/plain') 
    return redirect('dashboard') 
+0

看到這裏如何將文件附加到電子郵件:http://stackoverflow.com/questions/9541837/attach-a-txt-file -in的Python-的smtplib – Anentropic

回答

2

文檔說(https://docs.djangoproject.com/en/1.8/topics/email/#the-emailmessage-class):

並非所有的EmailMessage的特點類可以通過send_mail()和相關的包裝函數獲得。如果您希望使用高級功能,例如BCC的收件人,文件附件或多部分電子郵件,則需要直接創建EmailMessage實例。

所以,你必須創建一個EmailMessage

email = EmailMessage(
    'Subject here', 'Here is the message.', '[email protected]', [[email protected]]) 
email.attach_file('Document.pdf') 
email.send() 
相關問題