Using Different Paths for Attachments with Swift Mailer

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.

If you’re a PHP developer who’s looking for an approachable guide that teaches you how to take advantage of the most relevant features provided by the powerful Swift Mailer email library (available at http://swiftmailer.org), then this group of tutorials might be the material that you really need.  

Now that you’re aware of the goal of this article series, it’s time to review the topics that were discussed in the last tutorial. In that part of the series I explained how to attach one single file to a simple email message, and how to sent the message in HTML format.

In the first case, the sample file was attached by using a separate class included with Swift Mailer, called “Swift_Attachment,” while the formatting of the message with HTML was done via the “addPart()” method that belongs to the already familiar “Swift_Message" class.

However, the Swift Mailer library has plenty of options for handling files, including the ability to attach them from different locations on the web server and even from remote hosts. So, this seventh installment of the series will be focused on illustrating how to attach a file to an email message from a distinct folder, thus demonstrating the flexibility of the library when manipulating attachments’ paths.

Now, it’s time to leave the preliminaries behind and continue exploring the numerous features offered by Swift Mailer. Let’s jump in! 

{mospagebreak title=Review: handling file attachments with Swift Mailer} 

In the preceding part of this series, I explained how to attach a file to an email message via the “fromPath()” method of the “Swift_Attachment” class included with Swift Mailer. Below, you can study a short example that demonstrates how to perform this task. Examine the following code sample:

// example on sending a basic email message with Swift Mailer (uses the ‘addPart()’ method and Swift_Attachment class)

 

 

// 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)

  ->setUsername(‘alejandro@domain.com’)

  ->setPassword(‘password’);

 

 

// 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 in HTML to make it look nicer</p>’, ‘text/html’)

  ->attach(Swift_Attachment::fromPath(‘image1.jpg’));

 

 

// send the email message

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

{

   echo ‘The message was sent successfully!’;

}

else

{

   echo ‘Error sending email message’;

}

The previous script not only uses the pertinent “fromPath()” method that belongs to the “Swift_Attachment” class to attach an image to a basic email message, but it also invokes another method, called “addPart().” As its name suggests, this methods allows you to add a new part to the message.

In this case, the part being annexed to the message is a single line of text formatted in HTML, which shows how easy it is to send HTML email with Swift Mailer.

Well, now that you’re familiar with the logic that drives the previous example, it’s time to explore other handy features of the library. Therefore, in the following section, I’m going to teach you how to use the “fromPath()” method to attach a file that resides on a different location on the web server.

Of course, the full details of this process will be covered in the upcoming segment, so read the next few lines.

{mospagebreak title=Attaching files from a different location with the fromPath() method}

As I stated in the introduction, Swift Mailer provides developers with the ability to attach files from locations other than where the email script or application is being executed.

To demonstrate this capability, below I rewrote the example shown in the previous section, which uses the “fromPath()” method to attach an image file from a distinct folder on the web server.

Here’s the corresponding code sample; examine it closely, please:

// example on sending a basic email message with Swift Mailer (uses the ‘addPart()’ method and Swift_Attachment class and an attachment path)

 

 

 

// 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)

  ->setUsername(‘alejandro@domain.com’)

  ->setPassword(‘password’);

 

 

 

// 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’)

  ->attach(Swift_Attachment::fromPath(‘path/to/file/image1.jpg’));

 

 

 

// send the email message

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

{

   echo ‘The message was sent successfully!’;

}

else

{

   echo ‘Error sending email message’;

}

As depicted above, thanks to the flexibility offered by the “fromPath()” method, it’s perfectly possible to attach a file from a different location on the web server. Naturally, for the sake of clarity, the file path specified in the above example is merely fictional, but when working in a production environment, the path obviously would be a real one.

So far, everything looks good, right? At this point, you’ve learned how to use Swift Mailer’s abilities for attaching files from a different folder on the local web server. Nevertheless, the library also permits you to perform this same task by using a remote host. In the upcoming segment I’m going to build a final script, which will recreate this particular scenario for you.

To see how this last example will be developed, click on the link below and keep reading.

{mospagebreak title=Attaching files from a remote host}

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)

  ->setUsername(‘alejandro@domain.com’)

  ->setPassword(‘password’);

 

 

 

// 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’)

   ->attach(Swift_Attachment::fromPath(‘http://www.domain.com/images/image1.jpg’));

 

 

// send the email message

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

{

   echo ‘The message was sent successfully!’;

}

else

{

   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!

Google+ Comments

Google+ Comments