1

I'm using the Symfony Mailer package to upload files via email. I'm using PHP 8.2 and v6.2 of symfony/mailer. My form allows users to add a maximum of 4 files for upload. The docs on file attachments show how this can be done statically but I need to add them dynamically by parsing the uploaded $_FILES array. Not surprisingly this example results in a 500 error because you can't add a loop within a method chain.

<?php

require('lib/composer/vendor/autoload.php');

use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Address;

use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File;

$user = '';
$password = '';

$transport = Transport::fromDsn('smtp://' . $user . ':' . $password . '@domain.co.uk:25');
$mailer = new Mailer($transport);

$email = (new Email())
    ->from(new Address('from@email.co.uk', 'Name'))
    ->to('my@email.co.uk')
    ->subject('Hi this is a test using Symfony Mailer!')
    ->text('Sending emails is fun again!')
    ->addPart(new DataPart(new File('test.png'), 'Attached File'))
    ->html('<p>See Twig integration for better HTML integration!</p>');

    // Check for an uploaded file
    for($i=0; $i<=3; $i++) {

        if(!empty($_FILES['uploaded_file']['tmp_name'][$i])) {  
            $email->addPart(new DataPart(new File($_FILES['uploaded_file']['tmp_name'][$i]), $_FILES['uploaded_file']['name'][$i]));
        }           
    }

$mailer->send($email);

?>

<form method='post' enctype='multipart/form-data' action=''>
    <input class='upload_file' type='file' name='uploaded_file[]' id='upload_file1'>
    <input class='upload_file' type='file' name='uploaded_file[]' id='upload_file2'>
    <input class='upload_file' type='file' name='uploaded_file[]' id='upload_file3'>
    <input class='upload_file' type='file' name='uploaded_file[]' id='upload_file4'>
    <input type='submit'>
</form>
Mark
  • 778
  • 2
  • 11
  • 21
  • Hi, request you to try like ``` $email->addPart(new DataPart(new File('test.png'), 'Privacy Policy')); ``` cause i see you have ended the method chain.. and ->addPart is not linked to instansiated class $email.. this could be the reason... and as far as i know you cannot run for loop in between method chain... – Anant V Mar 27 '23 at 17:29
  • @AnantV I did try``` $email->addPart(new DataPart(new File('test.png'), 'Privacy Policy'))``` but no luck. And I'm sure you are right when you say that you can't add a loop within the method. I can't see how it can work then. Strange. – Mark Mar 27 '23 at 17:55
  • Yes, it is possible to attach multiple files in the email. Please explain the problem you are having when you say, *"The above doesn't work"*?. Is there an error message? Is it the wrong file being sent, only one of the four files, different name, does nothing get sent, etc? – Will B. Mar 27 '23 at 18:29
  • @WillB. I have updated my code example so you can see what I'm trying to do. Hopefully that is clearer. – Mark Mar 27 '23 at 19:16
  • You broke the method chain by not ending the declaration with `;` on the line of `->html('

    See Twig integration for better HTML integration!

    ');`. So simple typo there.
    – Will B. Mar 27 '23 at 21:41
  • The next issue is with `new DataPart(new File('test.png'), 'Privacy Policy')` not referencing the uploaded file info `new DataPart(new File($_FILES['uploaded_file']['tmp_name'][$i]), $_FILES['uploaded_file']['name'][$i])` Both issues are syntax related, not really specific to Symfony. – Will B. Mar 27 '23 at 21:43
  • @WillB. I've added a working example, thanks. – Mark Mar 28 '23 at 06:54

0 Answers0