Sendgrid sends mail with application using web api v3

I am new to using sendgrid web api v3. link here

Right now. It was easy to send a simple html using the api 'POST https://api.sendgrid.com/v3/mail/send ', but I have this instance where we will attach the file (csv / xls, pdf), and I don’t I can understand that this is correct.

Here is my code below:

My postSendMail function

public function postSendMail($data = [])
{
    if ( ! arrayHasValue($data) ) $this->error(__METHOD__, "Data is empty.");

    $request = Curl::to( $this->apiUrl.'mail/send' )        
        ->withHeader('Authorization: Bearer '. $this->apiKey)
        ->withData( $data )
        ->asJson(true)
        ->enableDebug(storage_path('logs/laravel-'.php_sapi_name().'.log'))
        ->post();


    return $request;
}

//my instance
$sendgrid = new Sendgrid;
    $data = [
                'personalizations' => [
                        [
                            'to' => [
                                [ 'email' => 'myemail@gmail.com' ]
                            ],
                            'subject' => 'Hello, World!'
                         ]
                    ],
                'from' => [
                        'email' => 'myemail@gmail.com',
                        'name' => 'my_site'
                    ],
                'content' => [
                        [
                            'type' => 'text',
                            'value' => 'Hello, World!'
                         ]
                    ],
                'track_settings' => [
                        [
                            'click_tracking' => true,
                            'open_tracking' => true
                        ]
                    ],
                'attachments' => [
                        [
                            'content' => base64_encode(config('global.UPLOAD_PATH') . '/my_file.pdf'),
                            'type' => 'application/pdf',
                            'filename' => 'my_file.pdf',
                            'disposition' => 'attachment'
                        ]
                    ]
                ];

    $lists = $sendgrid->postSendMail($data);

The mail was sent successfully, but when I view the attached file, it was damaged / unable to view. Can someone help me ?: (

Please, help.

+4
source share
1 answer

, , ; , .

'content' => base64_encode(config('global.UPLOAD_PATH') . '/my_file.pdf')

, , , .

- :

$imagedata = file_get_contents(config('global.UPLOAD_PATH') . '/my_file.pdf');
$base64 = base64_encode($imagedata);
+2

Source: https://habr.com/ru/post/1651354/


All Articles