0

I'm sending a PDF file via CURL in Laravel:

<?php

namespace App\Http\Controllers\EmailSender;


use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class EmailAttachments  extends Mailable{
    use Queueable, SerializesModels;

    function __construct($from, $to, $subject, $description, $attachments, $file_names = null){
        if(str_contains($to, 'email.com')){
            return;
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

        curl_setopt($ch, CURLOPT_URL, 'https://api.eu.mailgun.net/v3/'.config('app.mailgun_domain').'/messages');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));

        $post = array(
            'from' => $from,
            'to' => $to,
            'subject' => $subject,
            'text'    => 'Your mail do not support HTML',
            'html' => $description
        );
        
        // Attach all my files
        // Note that this files are with the full path, since the public_path to the file
        $i = 0;
        foreach ($attachments as $attachment) {
            echo $attachment;
            $post['attachment[' . $i . ']'] = curl_file_create($attachment, null, $file_names == null ? 'Report_' . date('W') .'.pdf' : $file_names[$i]);
            $i++;
        }  
        
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_USERPWD, 'api' . ':' . config('app.mailgun_secret'));

        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close($ch);
    }
}


?>

And I'm getting this message:

libpng warning: iCCP: known incorrect sRGB profile
Error:operation aborted by callback

I am doing the same way in other project and its working.

I checked the files and the paths are ok. I double checked. The path is ok and the extension file is ".pdf".

Example:

/home/root/backend.app.com/storage/app\works\obras\Report_0.pdf

The file is created and ready to be attached. I searched for solutions but all the topics are talking about PNG and other formats. And I didn't find the solution.

I tried to update Curl but still no success.

2
  • Does this help? stackoverflow.com/questions/22745076/… ? The question is about PNG files but there seems to be over lap with the PDF issue.
    – admcfajn
    Commented Sep 4, 2024 at 16:33
  • Which line specifically is giving you the "libpng warning"? Is it echo $attachment;? If so, is it possible that some command is generated a PDF but failing and returning an error message, and you are seeing it here?
    – Chris Haas
    Commented Sep 4, 2024 at 17:35

1 Answer 1

0

Maybe the answers is stupid and the problem also was. I will let the solution to my problem. When defining the path to the attachments I used // backslashes. For example:

"path//" . $variable . "//other_folder//a.pdf"

But I should be doing as:

"path/" . $variable . "other_folder/a.pdf"

It seems he doesn't like the double backslashes. Funny, it was working locally. But on the server it was not working

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.