Reputation: 1270
I am building a BlogApp and I am stuck on an Error.
When i try to send email with generated pdf
then it is sending only the filename. BUT i am trying to send generated pdf
in email
def send_pdf(request,pk):
profiles = get_object_or_404(Profile,pk=pk)
sent = False
template_path = 'pdf2.html'
context = {'profiles': profiles}
# Create a Django response object, and specify content_type as pdf
response = HttpResponse(content_type='application/pdf')
# # if downlaod:
# # response['Content-Disposition'] = 'attachment; filename="report.pdf"'
# # if display:
# response['Content-Disposition'] = 'filename="report.pdf"'
template = get_template(template_path)
html = template.render(context)
pisa_status = pisa.CreatePDF(
html, dest=response)
if request.method == 'POST':
form = DownloadDataForm(request.POST)
if form.is_valid():
subject = f"{request.user.username}'s Data"
message = response['Content-Disposition'] = 'filename="report.pdf"'
send_mail(subject,message,'[email protected]',
[request.user.email])
sent = True
else:
form = DownloadDataForm()
context = {'sent':sent,'form':form}
return render(request,'download_data.html', context)
I am trying to attach file which is in message. ( I don't want to read before send )
What would be in the File ?
The file will contain the data of the user like Name and Email
.
I have no idea what i am doing wrong. Any help would be much appreciated.
Upvotes: 1
Views: 1201
Reputation: 427
You can refer this function to attach pdf in your email
from django.core.mail import EmailMultiAlternatives
def send_email(subject, text_content, from_email, recepients):
mail = EmailMultiAlternatives(subject, text_content, from_email, recepients)
file_path = "/usr/Downloads/my.pdf"
file = open(file_path, "r+")
attachment = file.read()
file.close()
mail.attach("my.pdf", attachment, "application/pdf")
mail.send()
Moreover, you can refer from https://docs.djangoproject.com/en/3.1/topics/email/#emailmessage-objects
Upvotes: 1