Sending Blind Carbon Copies with Swift Mailer

This fifth part of the series expands your background in the Swift Mailer library. You will learn how to use it for sending blind carbon copies of a basic email message to a predefined recipient. The process is as simple as using its “setBcc()” method.

Undoubtedly, PHP programmers often need to add some kind of email-sending capability to the programs they create, either by using a proprietary library or by way of a third-party package.

While using a custom library (or even a custom framework) may work well when the whole development process of a web-based project is tackled by only one person, this approach won’t scale properly if there are many programmers involved in the project.

Naturally, in situations like these it’s necessary to look at a third-party email package that not only suits the requirements of a particular web application, but  can be used with the same ease and flexibility if the application grows in the future. Fortunately, there are a few well-trusted email libraries available nowadays that will do that job very efficiently, but in my personal opinion, one of the best is Swift Mailer, which you can download freely at http://swiftmailer.org.

To be frank, Swift Mailer will let you build email programs very quickly, through a solid programming interface that strictly implements several design patterns and cleverly applies some key software design principles, used frequently in object-oriented programming.

Logically, if you’ve already read the previous chapter of this series, you’ll be aware of the benefits of using Swift Mailer to add email capacities to a web application. In that tutorial I explained how to send a carbon copy along with an email message via its “setCc()” method. The whole process was a breeze, performed by using only some intuitive, chainable methods.

However, I’m only scratching the surface when it comes to exploring the handy features offered by Swift Mailer. It allows you to attach Bcc MIME headers (short for Blind Carbon Copies) in a snap as well. Thus, in this fifth part of the series I’m going to discuss in depth how to accomplish this through a couple of sample scripts.

Now, it’s time to learn how to send out blind carbon copies with Swift Mailer in a few simple steps. Let’s get started right away!

{mospagebreak title=Review: working with carbon copies}

It would be helpful to reintroduce the practical example developed in the preceding article of this series. In it, I discussed how to take advantage of the “setCc()” method provided by the Swift Mailer library to attach a carbon copy to a simple email message.

Below I listed the full source code corresponding to the example, so you can study it closely. Here it is:

// example on sending a basic email message with Swift Mailer (uses the ‘setCc()’ 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’))

// specify the Cc argument

->setCc(array(‘mary@domain.com’ => ‘Mary Jackson’))

// build the body part of the message

->setBody(‘Hey, how are you? I am sending you a message with the cool Swift Mailer library’);

 

// send the email message

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

{

echo ‘The message was sent successfully!’;

}

else

{

echo ‘Error sending email message’;

}

Certainly, you’ll have to agree with me that sending a carbon copy along with a plain-text email message is a simple process reduced to specifying the names and email addresses of the respective recipients via the “setTo() and “setCc()” methods of the “Swift_Message” class.

Finally, once the complete message has been created, the already familiar “send()” method takes care of sending the corresponding copies to the appropriate recipients, as has been clearly illustrated by the above code sample.

So far, everything looks good, since at this point you hopefully recalled how to use Swift Mailer to handle carbon copies of an email message in a truly simple manner. So, what’s the next step to take? Well, as I mentioned in the introduction, the library obviously allows us to work with blind carbon copies as well by using another intuitive method, not surprisingly called “setBcc().”

Thus, in the section to come I’m going to take a closer look at this brand new method, so you can learn how to use it very quickly.

Now, click on the link displayed below and proceed to read the next segment. I’ll be there, waiting for you.

{mospagebreak title=Using the setBcc() method}

As I said in the section that you just read, the Swift Mailer library permits you to send blind carbon copies of an email message in a very simple manner, as one would normally expect from a high-quality package. Performing this task is as easy as invoking a new method called “setBcc()” with the email address and name of the corresponding recipient of the copy.

However, the best way to understand how this handy method works is by example, so below I coded one for you. Have a look at it:

// example on sending a basic email message with Swift Mailer (uses the ‘setCc() and ‘setBcc() methods’)

 

// 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’);

Didn’t I tell you that attaching a blind carbon copy to an email message was a breeze with Swift Mailer? Well, the above example proves that I was right. As you can see, before building the body part of the message, the “setBcc()” method is used for specifying the email address and the name of the recipient of the copy, which is very similar to the process performed when sending normal copies.

But, wait a minute! The previous example is actually is incomplete. It’s not really sending any messages, because the corresponding “send()” method is not invoked at any point. So, in the final section I’m going to add to the script that missing part, thus completing the development of this example.

If you want to see how the example will look when finished, simply go ahead and read the following section. It’s only one click away.

{mospagebreak title=The complete script for sending multiple email copies}

As you’ll recall from the previous section, it’s necessary to add to the earlier script the block of code that really sends out the email message, along with the specified copies.

So, with that requirement in mind, below I listed the script’s full source code, this time including the part that calls the ”send()” method. Here it is:

// example on sending a basic email message with Swift Mailer (uses the ‘setCc() and ‘setBcc() methods’)

 

// 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’);

 

// send the email message

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

{

echo ‘The message was sent successfully!’;

}

else

{

echo ‘Error sending email message’;

}

There you have it. Thanks to the clean and tight interface provided by Swift Mailer, it’s possible to send multiple copies of an email message to a certain number of recipients. And regardless of the additional tasks that may be required by a particular web application, sending email with Swift Mailer is reduced most of the time to creating first the appropriate SMTP transport object, then composing the message with the additional copies, and finally dispatching it via the “send()” method. Voila!

Feel free to edit and enhance all of the code samples included in this tutorial, which surely will arm you with a better background in using this powerful email library.

Final thoughts

In this fifth part of the series, you expanded your background in the Swift Mailer library, since you learned how to use it for sending blind carbon copies of a basic email message to a predefined recipient. The process was as simple as using its “setBcc()” method and nothing else.

Now, moving forward, it’s time to talk about the topics that I plan to cover in the next article, so you know what to expect from it. Now that you learned how to send basic plain-text messages with Swift Mailer, in the following tutorial I’m going to discuss how to handle attachments in a very simple and intuitive manner.

Don’t miss the upcoming part!

Google+ Comments

Google+ Comments