CURL – Sending PDF failing with Laravel

  Kiến thức lập trình

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

<?php

namespace AppHttpControllersEmailSender;


use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;

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/appworksobrasReport_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

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

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT