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

Using the mail() PHP function - 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.

  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



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




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!

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