Home arrow PHP arrow Page 2 - Embedding Attachments into Email Messages with Swift Mailer

Review: attaching dynamically generated files with Swift Mailer - PHP

In this ninth part of a ten-part series, you'll learn how to use a couple of methods provided by Swift Mailer to directly embed attachments into email messages. These can be very helpful when sending newsletters that contain numerous images, which you donít want to be filtered and discarded by popular web-based email systems such as Hotmail and Yahoo.

TABLE OF CONTENTS:
  1. Embedding Attachments into Email Messages with Swift Mailer
  2. Review: attaching dynamically generated files with Swift Mailer
  3. Working with inline attachments with the setDisposition() method
  4. Introducing the embed() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
February 25, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Just in case you haven't read the prior tutorial of this series, where I discussed how to attach a dynamically generated file to an email message, below I included the two sample scripts developed in that article. They show how to perform this task in a simple manner, in addition to illustrating how to change on the fly the name of the file.

So, first, here's the example that attaches a dynamic image to a trivial message:

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

// $image has been generated previously using for instance the GD library or a similar package

$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::newInstance($image, 'sample_image.png'));

 

// send the email message

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

{

echo 'The message was sent successfully!';

}

else

{

echo 'Error sending email message';

}

From the above code sample, it's clear to see how easy it is to attach an image that has been created with a server-side library to an email message, thanks to the flexibility provided by the "newInstance()" method that belongs to the "Swift_Attachment" class. In this particular case the $image variable would hold the image in question, which has been generated as a PNG graphic.

Now, moving forward, it's time to look at the second example that shows how to modify the name of an attachment. This time we use another handy method of Swift Mailer, not surprisingly called "setFileName()."

Here's how this example looks:

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

 

// 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('/image1.jpg')->setFileName('otherimage.jpg'));

 

// send the email message

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

{

echo 'The message was sent successfully!';

}

else

{

echo 'Error sending email message';

}

Nothing really spectacular is happening here, right? As shown before, the "setFileName()" method allows you to change the default name of an attached file dynamically, before sending the email message via the corresponding "send()" method. It's that simple, really.

Well, at this point you hopefully understand how these two scripts do their business, so it's time to dig deeper into the capabilities offered by Swift Mailer to manipulate attachments in different ways.

In the following section I'm going to demonstrate how to directly embed those attachments into an email message, first by using one new method called "setDisposition()" and finally by means of another one, named simply "embed()."

To learn how the first of these methods can be used, jump ahead and read the next segment. It's only one click away.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: