Sendgrid PHP File Attachment 0KB

30 Views Asked by At

Using the below, I am able to ADD attachments to email from $_FILES posts however, they are 0kb / null attached.

The name and content type seems to pickup without issues? Wondering if anyone can shine some light to this.

I am sending the form type as so:

enctype="multipart/form-data"

My file input from HTML form is:

HTML

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

PHP

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

      }

I am using the Sendgrid PHP Library v3. All parts works besides the file itself. In the docs of Sendgrid, you must specify:

addAttachment( content, type, file name, composition )

1

There are 1 best solutions below

0
GoharSahi On

Please change your code as below

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"] only returns the temporary file path where as the first argument of the method requires contents of the file.