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

Introducing the embed() 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

As I expressed in the section that you just read, there's another method included with Swift Mailer that allows you to insert files into an email message. It's called "embed()." This one is extremely intuitive. A concrete example that shows how to use it has been coded below.

Take a look at the pertinent code sample:

// example on sending a basic email message with Swift Mailer (uses the embed() 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('<html><head></head><body><img src="'. $message->embed(Swift_Image::fromPath('/path/to/file/image1.jpg')). '"</body>');

 

// send the email message

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

{

echo 'The message was sent successfully!';

}

else

{

echo 'Error sending email message';

}

As depicted above, a sample image file has been inserted directly into a message formatted in HTML via the "embed()" method. For example purposes, I decided to use an instance of the brand new "Swift_Image" class to embed a JPG image within the message's markup, but naturally it's also possible to utilize other file types and obtain the same result.

And with this last example I'm finishing up this ninth tutorial exploring the Swift Mailer library. As always, feel free to tweak the code of all the examples developed in this article, so you can get a more solid background in working with inline attachments.

Final thoughts

That's all for now. In this ninth installment of the series I taught you how to use a couple of methods provided by Swift Mailer, which allow you to directly embed attachments into email messages. These can be very helpful, particularly 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.

In the final tutorial I'm going to explore another method of the library called "batchSend()," which allows you to send email messages in a subtly different way.

Here's my final piece of advice: don't miss the last article!



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap

Dev Shed Tutorial Topics: