Send pdf in attachment (Laravel Mail) - php

I have this code to get some HTML stored in the column "content" of the "badges" table in a pdf:
$pdf = app()->make('dompdf.wrapper');
$pdf->loadHTML($badgeContent->badge->content);
Do you know if is possible to send this pdf in a attachment using Laravel Mail?

You could sent almost any file in attachment
in laravel, create a new mailable
inside build paste this code
public function build()
{
return $this->view('emails.orders.shipped')
->attach('/path/to/file');
}
If you need more information on this check laravel's documentation
(this code is copied from the laravel's documentation
https://laravel.com/docs/5.6/mail#writing-mailables

Related

Attached PDF's are always empty - Laravel Mail

I am trying to send an email with an attached PDF. It works locally when using mailhog, but not in the live environment.
The mail sends and the PDF is there, but it's always just 0kb and will not open.
Here is the code:
public function build()
{
$this->from(config('mail.from.address'), config('mail.from.name'))
->subject('Your PDF')
->attach($this->filePath);
return $this->markdown('emails.pdf-download');
}
The path definitely does exist and the file is valid.
Here is what the file looks like:
Any help would be appreciated.

How to attach a PDF using DOMPDF in email in laravel

Hi i am trying to attach a pdf directly to an email without storing to storage using laravel mail function and sending it to the user. Everything is working fine as expected but the problem is the pdf instead of an attachment is coming as base64 encoded email content.
Can anyone please tell me what i am doing wrong?
Here is my code . This is inside mailer
$subject ='Hello! Your invoice is here';
$data['invoice_id']=$this->content['invoice_id'];
$pdf = PDF::loadView('frontend.pages.invoice.invoice',$data);
return $this->from('info#examplecompany.com')
->subject($subject)
->view('backend.emailtemplates.invoiceEmail')
->attachData($pdf->output(),'name.pdf', [
'mime' => 'application/pdf',
]) ;
I am following this How to attach a PDF using barryvdh/laravel-dompdf into an email in Laravel thread, even though i did the same way as its answer suggest , still not working.
Can anyone please help me on this.
Thank you.
Recommend to use this package for pdf.
https://github.com/niklasravnsborg/laravel-pdf
you can easily manage your pdf export:
output(): Outputs the PDF as a string.
save($filename): Save the PDF to a file
download($filename): Make the PDF downloadable by the user.
stream($filename): Return a response with the PDF to show in the browser.
I think you want to send pdf as an attachment in your email without saving it in your system .
your can refer the following code to do this .
$data["email"]='useremail#gmail.com';
$data["client_name"]='user_name';
$data["subject"]='sending pdf as attachment';
//Generating PDF with all the post details
$pdf = PDF::loadView('userdata'); // this view file will be converted to PDF
//Sending Mail to the corresponding user
Mail::send([], $data, function($message)use($data,$pdf) {
$message->to($data["email"], $data["client_name"])
->subject($data["subject"])
->attachData($pdf->output(), 'Your_Post_Detail.pdf', [
'mime' => 'application/pdf',
])
->setBody('Hi, welcome user! this is the body of the mail');
});
hope it helped you .
for better understanding you can follow this article
how to send pdf as an attachment in email in laravel

Pimcore how to create PDF and send via email

Anybody can give us a hint how to treat this best, if this can be achieved with „standards“ of pimcore?
Render a PDF with dynamic content and merge it with a PDF which has legal information.
Then send final pdf via email.
The PDF should be able to designed with HTML
You can use the Web2Print Documents to create and render the first PDF with dynamic content. To modify the PDF afterwards, you can hook into the PDF generation process (Pimcore 4 example):
\Pimcore::getEventManager()->attach("document.print.postPdfGeneration", function (\Zend_EventManager_Event $e) {
$document = $e->getTarget();
$pdf = $e->getParam("pdf");
Once you got that PDF, you can merge it with other PDFs like described here: Merge PDF files with PHP
Sending it via email should be easy at this point.

Phpmailer add PDF attachment from a link

I'm using PHPMailer to send automatic email (obviously using PHP as programming language).
I would like to add a PDF attachment generated with TCPdf.
Unfortunately I cannot generate the PDF inside the php page where I'm using PHPMailer, and I cannot create a function that generate it.
I can only use a link to generate it, like this:
www.mypage.com/app-pdf/link_generate_pdf.php?IDToGenerate=131&PDFOutput=I
I was thinking that I can recall the page with the PDFOutput=S and "return" in some way the text of the PDF and add it as attachment.
Otherwise I can call the page with PDFOutput=F and save it to a temp folder and then attach it to the email.
The problem is that I don't know how to "call" a page as it were a function and return what the recalling page actually returns.
Do you have some suggestion?
Thank you
edit: I now understand the problem! The problem is that the URL is accessible only from autentication (login page). I thought that as I was logged in, the script was automatically capable of read the page. How can I solve this?
Use this to save the file on the server first and then attach
file_put_contents("Tmpfile.pdf", fopen("http://example.com/file.pdf", 'r'));
Attach as
$mail->AddAttachment('path_to_pdf/Tmpfile.pdf', $name = 'Name_of_pdf_file',
$encoding = 'base64', $type = 'application/pdf');
Hope this helps
Edit: Try this. works fine over here
file_put_contents("path_to_pdf/Tmpfile.pdf",
file_get_contents("http://example.com/file.pdf"));

PHP Swift Mailer attach FPDF Problems

I am having some issues with trying to get SwiftMailer to attach a file I have created with FPDF. Basically I have a page called createPDF.php that is dynamically generated based on the ID number in the URL. This page is set to output the PDF inline using $pdf->Output("filename.pdf",I);. What I want to do is to be able to attach this file to an email using SwiftMailer from another page simply by calling my createPDF.php?id=xxx link.
From the PHP page where I want to send the email from, everything works, except the attachment. It attaches something, but not what I want and it is not viewable in a PDF viewer on my local machine. The line specific to the attaching the file is:
->attach(Swift_Attachment::fromPath('createPDF.php?id=xxxx'))
This does not work, but surely, it must be possible without saving the file on my web server by FPDF.
Is this possible? If so, how?
Thanks!
The problem here is Swiftmailer gets the file contents, it does not execute your php file. So the contents of your PDF will the code that is in createPDF.php.
why cant you safe the file first? You should be able to safe it and delete it when your email is sent.
<?php
$id = "xxx";
$fileName = "tmp/".sha1(time()+mt_rand(0,99999999));
include "createPDF.php"; //saves it to $fileName
->attach(Swift_Attachment::fromFile( $fileName )->setFilename('blaha.pdf'));
unlink($fileName);
Ok, so I just figured this out.
Basically I made a new PHP file with the bulk of my createPDF.php file as a function and simply passed in two variables into the function as my $id and an $output variable. $output is simply the way that FPDF outputs the file — inline, etc... I then set the function to return the output of the FPDF. In my createPDF.php file I simply call my function passing in $id and 'I' as the variables so it displays the correct PDF inline in the browser.
In my sendEmail function I simply pass in $id and 'S' and set it to a variable $content, which I pass into SwiftMailer as an attachment.
Works great.
Thanks for your help!

Categories