Using Swift Mailer’s Cc MIME Header

In this fourth part of a series, you will learn how easy it is to send carbon copies of an email message with the Swift Mailer library. The secret is the use of its setCc() method.

If you’re a PHP developer who’s searching for a comprehensive guide that teaches you how to take advantage of the most important features provided by the popular Swift Mailer library, then you’ve come to the right place.

Welcome to the fourth part of a series that introduces you to Swift Mailer. Made up of ten tutorials, this series shows you how to put this powerful PHP 5-based package to work for you quickly, so you can start developing full-featured email applications in a truly painless way.

And now that you know what to expect from this group of articles, it’s time to review the topics that were discussed previously, in case you haven’t read the last article. In that piece, I demonstrated how flexible the Swift Mailer library can be when it comes to sending email messages using different SMTP transports. I coded a couple of scripts that first showed how to perform this process by utilizing the “sendmail” Unix program, and then by means of the “mail()” PHP native function.

Undeniably, having at your disposal a great variety of mailer transports to use speaks for itself about the versatility of Swift Mailer. However, the actual fun begins when exploring its capabilities for manipulating email headers, attachments and multiple message formats.

So, in this part of the series I’m going to discuss how to use the library for dispatching email messages with a carbon copy, or in other words, how to configure Swift Mailer to send a “Cc” MIME header.

As you’ll see in a few moments, this process will be extremely easy to grasp. It will be accomplished through an intuitive API. So let’s start learning how to send blind email copies of a message with Swift Mailer right now!

{mospagebreak title=Review: handling different SMTP transports with Swift Mailer}

Just in case you haven’t read the previous tutorial of the series, where I explained how to configure Swift Mailer for dispatching email messages by means of the “sendmail” Unix program and the “mail()” PHP function, I included the two scripts developed in that article. This way, you’ll be able to understand how to perform these tasks in a few simple steps.

Here is the first script, which uses the “sendmail” application for sending a basic email message:

// example on sending a basic email message with Swift Mailer (uses sendmail as the SMTP transport)

 

// include required files

require_once ‘lib/swift_required.php';

 

// create the mail transport using the ‘newInstance()’ method

$transport = Swift_SendmailTransport::newInstance(‘/usr/sbin/exim -bs’);

 

// 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(‘Sending email with Swift Mailer via sendmail’)

// specify the From argument

->setFrom(array(‘alejandro@domain.com’ => ‘Alejandro Gervasio’))

// specify the To argument

->setTo(array(‘john@domain.com’ =>’ John Doe’))

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

}

As shown above, sending a sample email message by using the “sendmail” program doesn’t differ too much from doing the same thing through a remote SMTP server. In this case, the transport object is created by passing to the “getInstance()” method of the “Swift_SendmailTransport” class the path to the program, along with the proper flags.

On the other hand, I also mentioned that it was possible to send emails by using the “mail()” PHP function. If you’re interested in learning how to accomplish this, I’ve included another script below that demonstrates this procedure: 

// example on sending a basic email message with Swift Mailer (uses the mail() PHP function as the SMTP transport)

 

// include required files

require_once ‘lib/swift_required.php';

 

// create the mail transport using the ‘newInstance()’ method

$transport = Swift_SendmailTransport::newInstance();

 

// 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(‘Sending email with Swift Mailer via the mail() PHP function’)

// specify the From argument

->setFrom(array(‘alejandro@domain.com’ => ‘Alejandro Gervasio’))

// specify the To argument

->setTo(array(‘john@domain.com’ =>’ John Doe’))

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

}

In this case, the code is even simpler to read, since the factory method of the “Swift_SendmailTransport” class has been called with no input arguments. This results in the creation of a SMTP transport that will use the “mail()” PHP function to send emails. That was quite easy to learn, wasn’t it?

So far, so good. At this stage, I’m sure that you grasped the logic that drives the two earlier examples, so it’s time to explore other useful features offered by the Swift Mailer library. In the following section I’m going to teach you how to handle the “Cc” MIME header to send messages with a carbon copy to other recipients.

If you wish to learn the complete details of this interesting process, simply click on the link that appears below and read the next segment.

{mospagebreak title=Creating complex email messages with the setCc() method}

As you’ll certainly know, any decent email library must have the ability to send copies of an email message to multiple recipients, and Swift Mailer isn’t an exception. Similar to the “From” argument, which was covered in previous examples, the library uses a method called “setCc()” to specify the MIME “Cc” header.

Let me show you a concrete example that uses this method to specify that a carbon copy of a sample message must be sent to a fictional recipient. The corresponding code sample looks like this:

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

As I mentioned earlier, thanks to Swift Mailer’s intuitive API, it’s extremely easy to specify that a carbon copy of an email message has to be send to a different recipient. Of course, this is achieved by means of the “setCc()” method, which takes an array comprised of the recipient’s email address and their full name as an input argument. Simple to code and read, right?

As you may have noticed, though, the previous script is still incomplete. It does build the proper MIME headers, but it doesn’t actually send out any email messages. What’s the missing part? It’s necessary to call the already-familiar “send()” method to dispatch the corresponding emails.

Therefore, in the last section of this tutorial I’m going to list for you the complete code of the script, this time including the segment that really sends the email message, along with the carbon copy.

Now, go ahead and read the next few lines. We’re almost done!

{mospagebreak title=Completing the previous email script}

In the previous section, I built a script that showed how to specify the “Cc” argument for a sample email message. So, the only thing that remains undone is coding the part that actually sends the message, along with the corresponding carbon copy.

Below I included the complete source code of the script, this time adding the block that calls the “send()” method. Look at it, please:

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

}

Definitely, the above script is starting to reveal the actual power that stands behind the Swift Mailer library, since it shows how to send a single carbon copy of an email message to a hypothetical recipient through an extremely intuitive and compact API. Again, at the risk of being repetitive, it’s valid to mention here the convenience of using chainable methods in PHP 5, which certainly permits you to build scripts that are both quite readable and shorter in length.

Feel free to edit and improve all of the examples included in this article, so you can practice using the handy “setCc()” method provided by Swift Mailer.

Final thoughts

That’s all for the moment. In this fourth part of the series you learned how easy it is to send carbon copies of an email message with the Swift Mailer library, thanks to its “setCc()” method.

Of course, it’s natural to think that if the library can work with carbon copies, it’ll also allow us to use blind carbon copies. And you’d be right; Swift Mailer comes with another method, called “setBcc(),” that performs this task painlessly.

If you want to learn how to use this new method, then you can’t miss the next tutorial!

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

chat