Swift Mailer’s Batchsend Method and Other Features

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.

Frankly speaking, building a full-featured email application with PHP has always been a challenging task, even for seasoned developers. The process often demands that developers deal directly with the internals of both the SMTP and POP3 protocols, and it’s worse if the application must provide support for IMAP servers.

Of course, there’s no need to reinvent the wheel (or become a mixture of virtual postman and programmer) every time you need to create email programs in PHP. There are many well-trusted libraries available on the web that will do the job for you very efficiently.

One of the most powerful email packages that allows you to send email in a truly straightforward fashion is Swift Mailer, which you can download freely at http://swiftmailer.org. In case you haven’t had the chance to give it a try, this PHP 5-based package will let you send MIME email in all sorts of clever ways, through a cohesive and cleverly designed API that uses a strict object-oriented approach.

Naturally, if you’ve already read all of the previous articles of the series, then you probably have a solid background in using the most relevant features packaged with Swift Mailer, ranging from working with different SMTP transports and sending email messages in HTML, to using inline attachments.

Nevertheless, there are a few characteristics of the library that deserve a closer look. These include the proper usage of its “batchSend()” method, and its ability to manipulate MIME headers in a truly simple manner.

Ready to tackle this last chapter of the series? Then start reading right now!

{mospagebreak title=Review the setDisposition() and embed() methods}

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.

{mospagebreak title=The batchSend() method}

As I expressed in the introduction, the Swift Mailer library includes another method that allows you to send email messages, but in a slightly different way. What does it do differently from its counterpart “send()”? It’ll simply send a completely separate message to each recipient specified within the “To” header. This means the targeted recipient will see only their email address, which can be really useful in certain situations.

To show how to use this method, below I modified the script developed in the previous section, so you can grasp how it works. Here it is:

// example on sending a basic email message with Swift Mailer (uses the batchSend() 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(‘susan@domain.com’, ‘mary@domain.com’ => ‘Hello from here’))

// build the body part of the message

->setBody(‘I am sending an email message with the cool Swift Mailer library’);

 

// send the email message

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

{

echo ‘The message was sent successfully!';

}

else

{

echo ‘Error sending email message';

}

Even though the script that sends the above email looks nearly identical to the previous ones, in this case my friends Susan and Mary will see only their email addresses when receiving the message (and hopefully, this will make them feel a bit less jealous, too).

Having already spotted the subtle differences between using the “send()” and “batchSend()” methods, it’s time for the last part of this tutorial. I’ll be coding a final script, which will show how to specify a return path when sending emails.

Please go ahead and read the last section.

{mospagebreak title=The setReturnPath() method}

Even though Swift Mailer has many methods that allow you to manipulate MIME headers in useful and creative ways, in this last section of the tutorial I’m going to show you the use of only one. Called “setReturnPath(),” it comes in handy for assigning a value to the “Return-Path” header.

Since the method is extremely simple to utilize, the following sample script should dissipate any doubts that you may have regarding its use:  

// example on sending a basic email message with Swift Mailer (uses the setReturnPath() 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’, ‘mary@domain.com’ => ‘Hello from here’))

// build the body part of the message

->setBody(‘I am sending an email message with the cool Swift Mailer library’)

->setReturnPath(‘alejandro@domain.com’);

 

// send the email message

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

{

echo ‘The message was sent successfully!';

}

else

{

echo ‘Error sending email message';

}

As shown above, the utilization of the “setReturnPath()” method offers no major difficulties, and it’s only a small example of how easy it is to manipulate MIME headers with Swift Mailer. Of course, there’s the possibility that you may want to look at other methods that directly affect other popular MIME headers, so I recommend that you read the library’s official documentation.

Final thoughts

Sad but true, we’ve come to the end of this series. But I hope you enjoyed reading it as much as I did writing it. The series attempted to introduce you as gently as possible to using the most important methods provided by Swift Mailer, but the library has much more to offer; it boasts plenty of features that haven’t been covered here.

So, if you’re looking for a complete guide that covers all of the aspects of this email package, the best place to look is undoubtedly its official documentation, which not only is thorough, but very easy to digest.

See you in the next PHP development tutorial!

[gp-comments width="770" linklove="off" ]

chat