Home arrow PHP arrow Page 4 - Using Swift Mailer's Cc MIME Header

Completing the previous email script - PHP

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.

  1. Using Swift Mailer's Cc MIME Header
  2. Review: handling different SMTP transports with Swift Mailer
  3. Creating complex email messages with the setCc() method
  4. Completing the previous email script
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
February 17, 2010

print this article



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)




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




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!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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