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

Review: handling different SMTP transports with Swift Mailer - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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