简体   繁体   English

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

[英]Sendgrid sending mail with attachment using web api v3

I'm new to using sendgrid web api v3.我是使用 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.使用 api 'POST https://api.sendgrid.com/v3/mail/send ' 发送纯 html 很容易,但我有这个实例,我们将附加一个文件(csv/xls,pdf),我可以似乎没有做对。

Here is my code below:这是我的代码如下:

My function postSendMail我的函数 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:说到要点,您需要通过 curl 请求或 file_get_content 获取文件内容,然后将该内容编码为附件->内容参数,请检查以下对我有用的代码:

'attachments' => [ '附件' => [

                [

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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