编程语言
首页 > 编程语言> > php – Sendgrid使用web api v3发送带附件的邮件

php – Sendgrid使用web api v3发送带附件的邮件

作者:互联网

我是使用sendgrid web api v3的新手.链接here

马上.使用api’POST https://api.sendgrid.com/v3/mail/send‘很容易发送一个普通的HTML,但我有这个实例,我们将附加一个文件(csv / xls,pdf),我似乎无法正确.

这是我的代码如下:

我的函数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);

邮件已成功发送,但当我查看附件时,它已损坏/无法查看.谁能帮我?

标签:php,laravel,email,sendgrid
来源: https://codeday.me/bug/20190702/1353223.html