Home arrow PHP arrow Page 2 - Swift Mailer's Batchsend Method and Other Features

Review the setDisposition() and embed() methods - PHP

Welcome to the final part of a ten-part series that shows you how to use the Swift Mailer library to build email applications. This final part of the series will explain how to use the batchsend() method, and how to specify the Return-path MIME header within emails.

TABLE OF CONTENTS:
  1. Swift Mailer's Batchsend Method and Other Features
  2. Review the setDisposition() and embed() methods
  3. The batchSend() method
  4. The setReturnPath() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 01, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before I start explaining how to use the alternative “batchSend()” method to send email with Swift Mailer, it would be helpful to review its “setDisposition()” and “embed()” methods, discussed in the previous tutorial. They are used for  attaching inline files to messages.

So take a look at the code sample that uses the “setDisposition()” method to insert an image file into a basic email:

// example on sending a basic email message with Swift Mailer (uses the 'addPart()' method and 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';

}

As its name clearly suggests, the “setDisposition()” method can be used for specifying whether a specific file will be inserted inline into a message or annexed as a separate part of it. In this particular case, since my intention here was to illustrate how to embed an image into a simple message, the value passed to the pertinent method is obviously “inline().”

On the other hand, Swift Mailer includes the “embed()” method, which can be used to perform a similar task when creating an email message. A basic example of its use is shown below:

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

}

There you have it. By means of the handy “embed()” method, it’s pretty easy to insert images or other MIME-supported file types into a message, without having to deal directly with the complexities of the SMTP protocol.

So far, so good. At this point, you hopefully recalled how to send inline attachments with Swift Mailer, which means that it’s time to explore a few additional features offered by the library. Therefore, in the upcoming section I’m going to take a closer look at the alternative “batchSend()” method, which behaves subtly differently from its “send()” counterpart.

To learn more about this brand new method, read the following segment. I’ll be there, waiting for you.



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