Sendgrid PHP 文件附件 0KB

问题描述 投票:0回答:1

使用下面的内容,我可以将附件添加到来自 $_FILES 帖子的电子邮件中,但是它们的附件大小为 0kb / null。

名称和内容类型似乎没有问题?想知道是否有人可以对此有所启发。

我发送的表格类型如下:

enctype="multipart/form-data"

我从 HTML 表单输入的文件是:

HTML

<input type="file" name="file_input_name" id="file_input_name"/>

if (array_key_exists('file_input_name', $_FILES))
      {


            $filecontent = $_FILES["file_input_name"]["tmp_name"];
            $filename = $_FILES["file_input_name"]["name"];
            $filetype = $_FILES['file_input_name']['type'];

            $mail->addAttachment($filecontent, $filetype, $filename, 'attachment');

      }

我正在使用 Sendgrid PHP 库 v3。除了文件本身之外,所有部分都可以工作。在文档中,Sendgrid 您必须指定:

addAttachment(内容、类型、文件名、组成)

php phpmailer sendgrid
1个回答
0
投票

请更改您的代码如下

if (array_key_exists('file_input_name', $_FILES)){
            $filecontent = file_get_contents($_FILES["file_input_name"]["tmp_name"]);
            $filename = $_FILES["file_input_name"]["name"];
            $filetype = $_FILES['file_input_name']['type'];

            $mail->addAttachment($filecontent, $filetype, $filename, 'attachment');
}

as

$_FILES["file_input_name"]["tmp_name"]
仅返回临时文件路径,其中该方法的第一个参数需要文件的内容。

© www.soinside.com 2019 - 2024. All rights reserved.