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

Working with inline attachments with the setDisposition() method - 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

Until now, all of the scripts that worked with attachments used by default the "Content-disposition: attachment" MIME header to annex a file to the message being sent. However, as you may know, the SMTP protocol permits you to handle inline attachments very easily, which has been taken into account by the Swift Mailer development team as well.

The library provides basically two ways of embedding files into messages. Both use methods that belong to different classes. The first option comes with the "setDisposition()" method, which is available within the "Swift_Attachment" class, while the second choice is the "embed()" method, which is included with the "Swift_Message" class.

Does this sound a bit confusing to you? Well, to clear things up, let me start by showing you a script that uses the "setDisposition()" method to embed an image file into an email message. Take a look at it:

// example on sending a basic email message with Swift Mailer (uses the 'addPart()' method, the Swift_Attachment class and the setDisposition() 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('/path/to/file/image1.jpg')->setDisposition('inline'));

 

// send the email message

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

{

echo 'The message was sent successfully!';

}

else

{

echo 'Error sending email message';

}

See how simple is to work with inline attachments? This process requires only that a value of "inline" be passed to the corresponding "setDisposition()" method before sending the message to the specified recipients.

The rest of the script remains practically the same, so I'm not going to waste your time with pointless explanations. Instead, now that you've learned the first option for working with an inline attachment, in the following section I'm going to demonstrate how to embed the same image file into a message using the previously mentioned "embed()" method.

Now, click on the link below and read the next segment. We're almost finished!



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