PHPMailer integration on OsCommerce

 

Date:   June 20, 2007


Purpose:         PHPMailer lets the store owner send html email instead of plain text email that OsCommerce provided by default. PHPMailer also provides SMTP authentication so the user can use SMTP running on different server.


Requirement: A running SMTP server and a valid email address that resides on that server (Must provide Password for the SMTP server that requires authentication).


Who needs this integration:  (1) your store is hosted on Win32/php server and SMTP directive in php.ini is not setup correctly. (2) Your SMTP requires authentication.


Advantages:   Store owner can create better graphic email for sending out invoices or newsletters including store logos, links, and etc.


Note:   this integration DOES NOT provide sendmail or PHP mail functionalities and also removed OsCommerce default email functions.


Support:   Click here for support forum or copy the url [ http://forums.oscommerce.com/index.php?showtopic=267237 ] and paste it into browser's address bar.


Credits:   Using PHP Mailer in OsCommerce is originally introduced by the Community member Khairil. However, it didn't work for me; therefore, I decided to start writing my own "tep_mail" Function. It works for me very well and I would like share with other. My OSCommerce runs on (Windows/IIS) and SMTP with/without Authentication both work fine.

 

Step 1: BACK UP!   BACK UP!   BACK UP!

 

Step 2:

Upload class.phpmailer.php and class.smtp.php to \catalog\includes\classes\

 

Step 3:

Upload class.phpmailer.php and class.smtp.php to \catalog\admin\includes\classes\

 

Step 4:

 

Add the following code to \catalog\includes\configure.php before the last  ‘?>’ tag and

Also add to \catalog\admin\includes\configure.php before the last  ‘?>’ tag.

 

  define('SMTP_MAIL_SERVER', 'mail.yourmailserver.com');

  define('SMTP_PORT_NUMBER', '25');

  define('SMTP_SENDMAIL_FROM', 'valid_email@yourmailserver.com ');

  define('SMTP_FROMEMAIL_NAME', 'store owner name');

 

Step 5:

 

Find:

  require(DIR_WS_CLASSES . 'mime.php');

  require(DIR_WS_CLASSES . 'email.php');

 

in \catalog\includes\application_top.php

and \catalog\admin\includes\application_top.php

 

Replace with:

/* Nay -- PHPMailer

  require(DIR_WS_CLASSES . 'mime.php');

  require(DIR_WS_CLASSES . 'email.php');

*/

  require(DIR_WS_CLASSES . 'class.phpmailer.php');

 

 /* End Nay */

 

 

 

 

Step 6:

 

Find:

function tep_mail($to_name, $to_email_address, $email_subject, $email_text, $from_email_name, $from_email_address) {

    if (SEND_EMAILS != 'true') return false;

 

    // Instantiate a new mail object

    $message = new email(array('X-Mailer: osCommerce'));

 

    // Build the text version

    $text = strip_tags($email_text);

    if (EMAIL_USE_HTML == 'true') {

      $message->add_html($email_text, $text);

    } else {

      $message->add_text($text);

    }

 

    // Send message

    $message->build_message();

    $message->send($to_name, $to_email_address, $from_email_name, $from_email_address, $email_subject);

  }

 

in  \catalog \includes\functions\general.php

and \catalog\admin\includes\functions\general.php

 

Replace with:

/*

 

Old Function:

 

  function tep_mail($to_name, $to_email_address, $email_subject, $email_text, $from_email_name, $from_email_address) {

    if (SEND_EMAILS != 'true') return false;

 

    // Instantiate a new mail object

    $message = new email(array('X-Mailer: osCommerce'));

 

    // Build the text version

    $text = strip_tags($email_text);

    if (EMAIL_USE_HTML == 'true') {

      $message->add_html($email_text, $text);

    } else {

      $message->add_text($text);

    }

 

    // Send message

    $message->build_message();

    $message->send($to_name, $to_email_address, $from_email_name, $from_email_address, $email_subject);

  }

 

            //Email using PHP Mailer                     

                        //Edited by Nay

*/

  function tep_mail($to_name, $to_email_address, $email_subject, $email_text, $from_email_name, $from_email_address) {

    if (SEND_EMAILS != 'true') return false;

 

    // Instantiate a new mail object

    $message = new PHPMailer();

           

            if (EMAIL_TRANSPORT == 'smtp'){

                        $message->IsSMTP(); // telling the class to use SMTP

            }else{

                        //////(EMAIL_TRANSPORT == 'sendmail')

                        //////You need to implement here if you are using sendmail

            }

           

            // Config

            $message->Host = SMTP_MAIL_SERVER; // SMTP server

           

            if( !tep_not_null($from_email_address) ) {

                        $from_email_address = SMTP_SENDMAIL_FROM;

            }

           

            $message->From = $from_email_address;

           

            if( !tep_not_null($from_email_name) ) {

                        $from_email_name = SMTP_FROMEMAIL_NAME;

            }

           

            $message->FromName = $from_email_name;

           

            if( !tep_not_null($to_name) ) {

                        $to_name = '';

            }

             

            if( !tep_not_null($to_email_address) ) {

                        return false;

            }

           

            $message->AddAddress($to_email_address, $to_name);

 

            $message->Subject = $email_subject;

 

    // Build the text version

    $text = strip_tags($email_text);

    if (EMAIL_USE_HTML == 'true') {

              $message->Body = tep_convert_linefeeds(array("\r\n", "\n", "\r"), '<br>', $email_text);

              $message->AltBody = $text;

              $message->IsHTML(true);

    } else {

              $message->Body = $text;

              $message->IsHTML(false);

    }

 

    // Send message

    if(!$message->Send()){

               /*echo 'Email was not sent.';

               echo 'Mailer error: ' . $message->ErrorInfo;*/

               return false;

            }

  }

 

Step 7:

 

Find:

//Let's build a message object using the email class

    $mimemessage = new email(array('X-Mailer: osCommerce'));

    // add the message to the object

    $mimemessage->add_text($message);

    $mimemessage->build_message();

    while ($mail = tep_db_fetch_array($mail_query)) {

      $mimemessage->send($mail['customers_firstname'] . ' ' . $mail['customers_lastname'], $mail['customers_email_address'], '', $from, $subject);

    }

 

in \catalog\admin\mail.php

 

Replace with:

/*

            Nay -- Edited

                        -- Email Using PHP Mailer

             Old Code:

    //Let's build a message object using the email class

    $mimemessage = new email(array('X-Mailer: osCommerce'));

    // add the message to the object

    $mimemessage->add_text($message);

    $mimemessage->build_message();

    while ($mail = tep_db_fetch_array($mail_query)) {

      $mimemessage->send($mail['customers_firstname'] . ' ' . $mail['customers_lastname'], $mail['customers_email_address'], '', $from, $subject);

    }

 

*/

            while ($mail = tep_db_fetch_array($mail_query)) {

      tep_mail($mail['customers_firstname'] . ' ' . $mail['customers_lastname'], $mail['customers_email_address'], $subject, $message, STORE_OWNER, $from);

    }

 

/* End Nay */

 

Done!