Home arrow PHP arrow Page 4 - Using Different Paths for Attachments with Swift Mailer

Attaching files from a remote host - PHP

Welcome to the seventh tutorial of a series on Swift Mailer. Comprised of ten parts, this series introduces you gently to using this thorough PHP5-based email package. It complements the corresponding theory with numerous code samples that you can incorporate into your own PHP applications.

  1. Using Different Paths for Attachments with Swift Mailer
  2. Review: handling file attachments with Swift Mailer
  3. Attaching files from a different location with the fromPath() method
  4. Attaching files from a remote host
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
February 23, 2010

print this article



Certainly, a good conclusion to this tutorial will demonstrate how flexible Swift Mailer can be for attaching files from different locations. In the next few lines Iím going to code another script, which will attach an image file from a specified URL that is not a local server.

Take a look at the following script: 

  // example on sending a basic email message with Swift Mailer (uses the 'addPart()' method and Swift_Attachment class and a URL for the attachment)




// include required files

require_once 'lib/swift_required.php';




// create the mail transport using the 'newInstance()' method

$transport = Swift_SmtpTransport::newInstance('mail.domain.com', 25)






// create the mailer using the 'newInstance()' method

$mailer = Swift_Mailer::newInstance($transport);




// create a simple message using the 'newInstance()' method

$message = Swift_Message::newInstance()

   // specify the subject of the message

   ->setSubject('Testing Swift Mailer')

  // specify the From argument

  ->setFrom(array('alejandro@domain.com' => 'Alejandro Gervasio'))

  // specify the To argument

  ->setTo(array('john@domain.com' =>' John Doe'))

  ->setCc(array('mary@domain.com' => 'Mary Jackson'))

  ->setBcc(array('susan@domain.com' => 'Susan Norton'))

  // build the body part of the message

  ->setBody('Hey, how are you? I am sending you a message with the cool Swift Mailer library. Make sure to check the attached file!')

  ->addPart('<p>This part of the message has been formatted as HTML to make it look nicer</p>', 'text/html')




// send the email message

if ($mailer->send($message))


   echo 'The message was sent successfully!';




   echo 'Error sending email message';


There you have it. As you can see by the previous code sample, not only is it possible to send HTML email messages in a truly simple manner with Swift Mailer, but it makes attaching files from a remote server a breeze. What else can you ask for?

Well, much more actually. All of the examples developed so far showed how to attach files that have been created with an image editor. But what if the file that needs to be attached is generated dynamically with a graphic library? Is it possible to accomplish this with Swift Mailer?

Well, the answer is a resounding yes! But this topic and others will be covered in the section to come. Meanwhile, feel free to edit all the examples shown in this tutorial, to gain practice in working with file attachments.

Final thoughts

Thatís all for the moment. In this seventh part of the series, I explained how to use the Swift Mailer library to attach files from different locations, including a remote host. In all of these cases, the ďfromPath()Ē method shown earlier allowed us to do this painlessly. 

In the next article, Iím going to show you how to attach files that have been created dynamically, and change their names on the fly as well.

Donít miss the upcoming part!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: