Using Sendmail and Mail() with Swift Mailer

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.

Among the great variety of PHP-based email libraries available nowadays, there’s one that might be particularly attractive to you. It offers a flat learning curve and impressive features, which have been implemented by using a strict object-oriented approach.

In this specific case, of course, I’m talking about Swift Mailer, a powerful package developed by Chris Corbin that you can download at http//swiftmailer.org. It allows you to send email messages in a truly painless fashion using a myriad of SMTP transports and additional options, such as easily handling attachments, formatting messages in HTML, and much more.

So, if you’re tired of using your old email libraries over and over again and want to give a face-lift to your developer’s toolbox, then start learning how to put Swift Mailer to work for you.

And now that you have a clearer idea of the things that Swift Mailer can do to help you build email applications in PHP 5, it’s time to review the topics that were discussed in the last tutorial. In that part of the series I explained how to utilize different SMTP transports within the same script for dispatching a basic email message by means of the "setHost()" and "setPort()" method provided by the library.

This particular example undoubtedly speaks for itself about the flexibility offered by Swift Mailer, but there are other options provided by the library that need to be explored. These include choosing which transport to use for sending email.

Thus, in this third installment of the series, I’m going to explain how to use the "sendmail" Unix application and the "mail()" PHP native function respectively with Swift Mailer, in order to create a couple of simple email scripts that you’ll grasp quickly. 

Now, it’s the time to continue discovering the real power of the Swift Mailer library. Let’s get going!

{mospagebreak title=Review: using different transports to send email with Swift Mailer}

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.

{mospagebreak title=Using the Unix sendmail program}

If you’ve ever put your hands on a Unix-based machine, the you may know that it includes a popular program called "sendmail," which can be used for sending email. Fortunately, the developer team behind Swift Mailer has incorporated into the library the ability to work seamlessly with this program, instead of using a different SMTP transport.

To demonstrate how to use "sendmail" with Swift Mailer, below I coded a script similar to the one that you learned previously. It shows how to perform this operation in a simple way. Here’s the script in question:

// 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 you can see, dispatching an email message through the "sendmail" program doesn’t different too much from using another SMTP transport. In this case, the transport object has been created by using a new class called "Swift_SendmailTransport" and by specifying a common path to the program. It’s actually that easy.

The rest of the script remains nearly the same, so I don’t want to waste more of your time explaining how it works. Instead, the next thing I’m going to teach you in this tutorial is how to code another script that will use the "mail()" PHP function to send a basic email message.

Want to see how this script will be created? Then click on the link below and read the following section.

{mospagebreak title=Using the mail() PHP function}

As I mentioned in the previous segment, though Swift Mailer uses PHP sockets internally to communicate directly with a SMTP host to send email messages, it also gives you the possibility to use the "mail()" PHP function to perform the same task.

If you’re a big fan of this function and are wondering how to use it with Swift Mailer, here’s an example. Look at it, please:

// example on sending a basic email message with Swift Mailer (uses mail() 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';

}

As you can see above, to use the "mail()" PHP function, the "getInstance()" method of the "Swift_SendmailTransport" class has to be called with no arguments. In doing so, the library will automatically utilize this function to send email, as you’ve done possibly hundreds of times in the past with your own scripts.

And with this last practical example, I’m finishing this third chapter of the series that explores the main features provided by the handy Swift Mailer library. Of course, you’re free to edit all of the code samples that appear in this tutorial, so you can improve your skills when it comes to working with this powerful package.

Final thoughts

Over this part of the series, I explained how to configure Swift Mailer to work seamlessly with the "sendmail" Unix program and the "mail()" PHP function as well. As you saw in previous examples, this process is extremely straightforward, so in theory you shouldn’t have major trouble understanding how it functions.

In the next part, things will be even more interesting, since I’m going to discuss in detail how to use the library to send email messages with a carbonic copy (Cc).

Here’s my final piece of advice: don’t miss the following tutorial!

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

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort