16

As always here is the place where I have learned a lot. And I have now a new things to learn:

I have a html form:

<tr><td width="16%">File attachment</td><td width="2%">:</td><td><input type="file" name="fileatt" /></td></tr>

and a mail.php:

$attachfile=$_POST["fileatt"];

and a correct swiftmailer code to send emails out;

I have googled and I found many examples how to send attachment with a file stored on the website but I would like to do it on the fly. So when you submit the button it would send it to peoples out rather than uploading the file.

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.server.co.uk', 25)
->setUsername('user')
->setPassword('pass')
;

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance($subject)
  ->setFrom(array('emai@emai.com' => 'name'))

   ->setBody($html, 'text/html')
  ;
// Add alternative parts with addPart()
$message->addPart(strip_tags($html), 'text/plain');

// Send the message
$result = $mailer->send($message);

could anyone help me how to do the on the fly file uploading, please? Thanks in advance!!!

TryHarder
  • 750
  • 1
  • 9
  • 22

3 Answers3

42

There's a simple way to do this, here you go:

$message->attach(
Swift_Attachment::fromPath('/path/to/image.jpg')->setFilename('myfilename.jpg')
);

That's one way SwiftMail can do this, now just the /tmp file, and turn the above into the following:

Assuming: fileatt is the variable for the $_FILE, ['tmp_name'] actually is the tmp file that PHP creates from the form upload.

$message->attach(
Swift_Attachment::fromPath($_FILES['fileatt']['tmp_name'])->setFilename($_FILES['fileatt']['name'])
);

More information on SwiftMail Attachments can be found on this docs page

More information on $_FILES can be found here on w3schools, despite I don't like w3schools, this page is solid.

Community
  • 1
  • 1
André Catita
  • 1,313
  • 16
  • 19
  • I am using Swiftmailer to send attachments but when I try to submit my form without an attachment I get the `Swift_IoException: The path cannot be empty` error. How do I allow for attachments to be optional using SwiftMailer? – CodeConnoisseur Sep 18 '19 at 20:21
  • Is there a way to add attachments one at a time from the file upload opener? – CodeConnoisseur Dec 17 '19 at 18:09
5

Another way to do this, using only a single variable for path and filename is:

$message->attach(Swift_Attachment::fromPath('full-path-with-attachment-name'));
Aris
  • 4,643
  • 1
  • 41
  • 38
1

Single Attachment

My answer is similar to that of André Catita. However, in Laravel 6 you can use $request instead of $_FILES. Let me simplify the code above:

$path = $request->file('import')->getPathName();
$fileName = $request->file('import')->getClientOriginalName();      

$message->attach(
        Swift_Attachment::fromPath($path)->setFilename($fileName)
        );

Here I assume that the name of your file tag is import. For eg: <input type="file" name="import" />

Multiple Attachments

Now, lets say instead of single attachment you need multiple attachments. Then the code needs to be changed.

First your html code will become: <input type="file" name="import[]" multiple />

And for backend or laravel; code will be:

$files = $request->file('import');
foreach($files as $file){

        $path = $file->getPathName();
        $fileName = $file->getClientOriginalName();      

        $message->attach(
            Swift_Attachment::fromPath($path)->setFilename($fileName)
            );
}
Bimal
  • 865
  • 10
  • 18