Attaching Existing Files

Files that already exist, either on disk or at a URL can be attached to a message with just one line of code, using Swift_Attachment::fromPath().

You can attach files that exist locally, or if your PHP installation has allow_url_fopen turned on you can attach files from other websites.

To attach an existing file:

  1. Create an attachment with Swift_Attachment::fromPath().
  2. Add the attachment to the message with attach().

The attachment will be presented to the recipient as a downloadable file with the same filename as the one you attached.

//Create the attachment
// * Note that you can technically leave the content-type parameter out
$attachment = Swift_Attachment::fromPath('/path/to/image.jpg', 'image/jpeg');  

//Attach it to the message
$message->attach($attachment);


//The two statements above could be written in one line instead
$message->attach(Swift_Attachment::fromPath('/path/to/image.jpg'));


//You can attach files from a URL if allow_url_fopen is on in php.ini
$message->attach(Swift_Attachment::fromPath('http://site.tld/logo.png'));

NOTE: Documentation for version 3 is in the wiki.