Home arrow PHP arrow Page 2 - Using Sendmail and Mail() with Swift Mailer

Review: using different transports to send email with Swift Mailer - PHP

In this third part of the series, I explain how to configure Swift Mailer to work seamlessly with the “sendmail” Unix program and the “mail()” PHP function. This process is extremely straightforward, so you shouldn’t have any trouble understanding how it functions.

TABLE OF CONTENTS:
  1. Using Sendmail and Mail() with Swift Mailer
  2. Review: using different transports to send email with Swift Mailer
  3. Using the Unix sendmail program
  4. Using the mail() PHP function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
February 16, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before I explain how to send out email messages by using both the "sendmail" Unix application and the "mail()" PHP function, it'd be helpful to reintroduce the  examples created in the previous chapter of the series. They demonstrated how to perform this same task by utilizing a local and a remote SMTP server.

That being clarified, here's the first example, which shows how to send email through a local host. Take a look at it:

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

 

// include required files

require_once 'lib/swift_required.php';

 

// create the mail transport using the 'newInstance()' method

$transport = Swift_SmtpTransport::newInstance('localhost', 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('Sending email with Swift Mailer via localhost')

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

}

Undeniably, understanding how the above script works is a pretty straightforward process. The most relevant part to analyze is the one that creates a SMTP transport via the "getInstance()" method of the "Swift_SmtpTransport" class. It specifies that the server to be used to send email messages will be a local host, running on port 25. Simple to code and read, right?

Now, moving forward, here's another sample script that performs the same operation, but by swapping between a local and a remote server. Here it is:

// example on sending a basic email message with Swift Mailer (uses multiple SMTP transports)

 

// include required files

require_once 'lib/swift_required.php';

 

// create the mail transport using the 'newInstance()' method

$transport = Swift_SmtpTransport::newInstance('localhost', 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('Sending email with Swift Mailer via localhost')

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

}

 

// specify another SMTP transport using the 'setHost()' and 'setPort()' methods

$transport = Swift_SmtpTransport::newInstance()

->setHost('mail.domain.com')

->setPort(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('Sending email with Swift Mailer via a remote host')

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

}

There you have it. Thanks to the clever use of the "setHost()" and "setPort()" methods shown previously, it's possible to use different transports to send email messages within the same program. Of course, the scenario shown above is rather unusual, but it's useful for illustrating how flexible Swift Mailer can be when it comes to switching between different SMTP servers.

Well, now that you hopefully grasped the logic that drive the two previous examples, it's time to explore other features provided by the Swift Mailer library. So, in the next section I'm going to discuss how to use the library for sending email messages by using the PHP "mail()" function and the Unix "sendmail" program.

As usual, to learn more about these interesting topics, click on the link below and keep reading.



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