简体   繁体   中英

Sendgrid sending mail with attachment using web api v3

I'm new to using sendgrid web api v3. link here

Right now. It was easy to send a plain html using there api 'POST https://api.sendgrid.com/v3/mail/send ' but I have this instance where we will attach a file (csv/xls,pdf) and I can't seem to get it right.

Here is my code below:

My function postSendMail

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

Mail was successfully sent but when I view the attached file, it was corrupted/unable to view. Can anyone help me? :(

Please help.

The problem is that you are not reading the file into an object and then encoding that object; you're encoding a string containing the file path.

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

All of your attachments in the tests are probably the same size, and smaller than the actual file as a result.

Try something like:

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

Coming to main point you need to get file content either by curl request or by file_get_content then encode the that content into attachments->content parameter, Please check following code which works for me:

'attachments' => [

                [

                        'content' => base64_encode(file_get_contents(config('global.UPLOAD_PATH') . '/my_file.pdf')),
                        'type' => 'application/pdf',
                        'filename' => 'my_file.pdf',
                        'disposition' => 'attachment'
                    ]
                ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM