Laravel Mail attach method - Attached file not opening after downloading - php

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);

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.

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"));

png attachment generated by an API in phpmailer

I'm trying to send a .png image to my user via phpmailer. The image is shown when I use <img> tags, but I want it to display as a real attachment that the user can open/save/print (like in this screenshot). I read that I can use $mail->addStringAttachment for this. So I tried this, and it does send an attachment with the email, but when I try to open it, it says that Windows Picture Viewer can't open the file. Also saving to my computer and then opening with Paint doesn't work, it tells me thats not a valid file or something. I think this is because it's no static image, but an image generated by an API, namely:
$qr = 'http://api.qrserver.com/v1/create-qr-code/?data=' . $guid . '&size=250x250';
So this image should be sent as an attachment. Does anyone know how I can make this work?
I got it to work fine as an attachment by doing the following:
$qr = file_get_contents("https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Example");
$mail->addStringAttachment($qr, "qr.png");
The reason it's failing is that you're trying to attach the URL as image data. You need to fetch the data from the URL first, then attach it to something.
Go one step at a time - make sure that you're getting back valid image before trying to email it - e.g.
file_put_contents('qr.png', file_get_contents($qr));
and make sure you get a valid image saved in there. When you know that's working, then try and email it with
$mail->addStringAttachment(file_get_contents($qr) 'qr.png');
Though perhaps with a bit more error checking!

How to specify an email drop location

I'm using Drupal CMS on top of PHP and IIS. When I send emails containing embed images, the images are not displayed in Outlook.
I'm trying to isolate the problem.
There is nothing in Drupal or Outlook which will allow me to view the complete message body with headers.
Is there a way to configure PHP to write the email to a folder on disk instead of sending the email?
you can write it to a text file instead of sending, just need to find the place where it happens:
$folder=dirname(__FILE__)."/emaildir";
$txtfilename=time().'.txt';
$emailstr=$header . "/n" . $message . "/n";
instead of mailto() or whatever function just write to file
$fh=fopen($txtfilename,"w");
$fwrite($fh,$emailstr);
fclose($fh);
This is written from my head, you might want to check for mistakes but you get the picture

Email image stored as a variable with mosMail()

I would like to send an email with an image attachment using mosMail(). Everything works perfectly when I attach a file located on the hard drive, but because the image that I want to attach is generated on the fly, I would rather not have to store it. Is it possible to attach an image stored in a variable?
$attachment = $im;
mosMail(..., $attachment);
You can't, but the underlying mailer library is PHPMailer. Simply include this and use it directly. You will have to reproduce how mail sending configuration settings are read from Joomla's configuration, simply copy and paste from MosMail

Categories