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.
Related
I have used the Laravel Mail attach method to specify file attachment for the mail being sent. The file attached successfully but cannot be viewed.
->attach(route('download_attachment', 'file=' . $attachment->name));
One thing I noticed though is that if I generate the link in the email being sent, people can also only download it if they are authenticated. So, I am thinking it has something to do with auth/role as the files are stored in the Storage on the server not public_html
I have solved this still with help from Stackoverflow though. I first get the actual file from the storage, then I attached it as shown below.
$getThisFile = storage_path('app/' . $attachment->name);
return (new MailMessage)
->subject('Download Ready')
->line('Some texts')
->line('Thank you!')
->attach($getThisFile);
I have been stuck on this for hours. Maybe my mind is tired. I hope someone can assist me. I am developing a Laravel application which connects to an external application that creates and edits invoices. One of my routes is supposed to allow a user to download a PDF invoice from this external application. My controller resembles the code below:
public function download(Invoice $invoice)
{
// Creates an instance of the remote invoice
$remoteInvoiceFile = RemoteInvoiceSoftware::find($invoice->id);
return response()->streamDownload(function () use ($remoteInvoiceFile ) {
// This calls the remote server and the server responds with the PDF file contents
$file = $remoteInvoiceFile->download();
echo $file;
}, 'file.pdf');
}
I read about the streamDownoad function from the Laravel documentation and implemented it the same way it was displayed. But I am getting an error where I am able to download a PDF file, however, not only is the file less than 5KB (the original invoice file is about 60KB), I also get an error when I try to open it. Something about the file being corrupt or not parsed well.
When I echo $remoteInvoiceFile->download() without using the streamDownload I get something like this:
Please help me figure what's going on and how I can fix this. Thank you!!
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
I want to detect someone opened their email from me.
Right now I'm using approach put 1x1.gif transparent image.
I followed this link: Detecting image load on server
My current html:
<img src="api.example.com/track?id=1">
and my server only return simple response:
return response('https://cdn.example.com/images/1x1.gif', 200)->header('Content-Type', 'image/gif');
I've tried change the return response to return response()->json() but stil fail.
My third tried I just echo it out: echo https://cdn.example.com/images/1x1.gif
But it still shown a broken image on the email. I've tried view it without email it return me broken image too. I've tried put the image value directly it works properly (to make sure image is there).
Is there any way I can achieve this?
Solved by using:
return redirect()->secure('https://cdn.example.com/images/1x1.gif');
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!