Sending Mail In PHP Through SMTP

15 Nov 2008 | Mail, PEAR, PHP, SMTP

Sending mail is a common task for many applications today. Whether sending mail through a webform or as an automated tasks, sending mail can be simple using PHP and PEAR. Using the sendMail() function below, you can send HTML formatted mail through any smtp server.

Update March 11, 2009: Removed the demo since GoDaddy does not support external SMTP servers. Update July 06, 2009: Updated variables for the usage example to be less confusing. Thanks to David for pointing this out.

This function requires the following PEAR classes along any of their dependencies.

This function assumes that you have your pear include path set correctly. You can download the function here (right click -> Save Link As) or copy the code from below. Scroll down for some examples/documentation.

 * Sends mail by connecting to an smtp server
 * @param string $subject
 * @param string $smtp_server
 * @param string $smtp_username
 * @param string $smtp_password
 * @param string $html html portion of the message body
 * @param string $text text portion of the message body
 * @param string|array $to
 * @param string $from
 * @param array $cc
 * @param array $bcc
 * @return boolean|string
function sendMail($subject, $smtp_server, $smtp_username, $smtp_password,
    $html, $text, $to, $from, $cc = array(), $bcc = array()) {
    // require pear classes
    require_once 'Mail.php';
    require_once 'Mail/mime.php';
    // create the heaers
    $to = is_array($to) ? $to : explode(',', $to);
    $headers = array(
        'Subject' => $subject,
        'From' => $from,
        'To' => implode(',', $to),
    ); // end $headers
    // create the message
    $mime = new Mail_mime("\n");
    $mime->addCc(implode(',', $cc));
    $mime->addBcc(implode(',', $bcc));
    // always call these methods in this order
    $body = $mime->get();
    $headers = $mime->headers($headers);
    // create the smtp mail object
    $smtp_params = array(
        'host' => $smtp_server,
        'auth' => true,
        'username' => $smtp_username,
        'password' => $smtp_password,
        'timeout' => 20,
        'localhost' => $_SERVER['SERVER_NAME'],
    ); // end $smtp_params
    $smtp = Mail::factory('smtp', $smtp_params);
    // send the message
    $recipients = array_merge($to, $cc, $bcc);
    $mail = $smtp->send($recipients, $headers, $body);
    // check to make sure there was no error
    if (PEAR::isError($mail)) {
        return $mail->getMessage();
    } // end if there was an error
    return true;

Using this function is simple. Simply pass in the parameters as described below:

For example:

// set all of the parameters
$subject = 'This is an HTML email.';
$smtp_server = '';
$from = $smtp_username = '';
$smtp_password = 'password';
$html = 'This is an <strong>HTML</strong> <i>formatted</i> email!';
$text = strip_tags($html);
$to = '';
$cc = array('');
$bcc  array('', '');

// send the message
$result = sendMail($subject, $smtp_server, $smtp_username, $smtp_password, $html, $text, $to, $cc, $bcc);

// check to see if the message was sent properly
if ($result !== true) {
    echo 'There was an error sending the message. ('.$result.')';
} // end if the message was not sent properly
else {
    echo 'Message sent successfully.';
} // end else the message was sent properly
comments powered by Disqus

Older · View Archive (34)

Accessing HarrisData (IBM iSeries) In PHP

HarrisData I have recently been working on a project to integrate with a HarrisData ERP Application. The Application runs on an IBM iSeries (AS/400). I used PHP’s built in PDO object with the ODBC extension. Using PDO, I was able to execute SQL Queries on the server. IBM systems use IBM’s DB2 syntax. This entry will outline some of the basic steps, in PHP, to communicate with the system


vcards In Mozilla Thunderbird

Lately, I have been constantly switching between both operating systems and mail clients. I became quite fond of Kontact, KDE’s PIM suite, long ago. The only thing that stopped me from switching over to it full time was its lack of support for HTML signatures. As of the KDE 4 release of Kontact, support has been added for HTML signatures. I decided to make the switch back to Kontact earlier this week.