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

Using the Unix sendmail program - 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

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.



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