Adding SMTP server support in CRE Loaded 6.2

Adobe Design Premium CS5

Till version 6.2 cre loaded was only capable of sending emails using sendmail program using php mail() function. Some time it happens that our site is hosted on shared hosting where same server IP is shared between many sites. These sites may include site which send spam emails and in result the server IP get penalized by email servers and messages being sent from this IP are not delivered to recipients, or sent to junk/spam folders directly.

For a site which has to send email notifications and status update emails, this can be a nightmare. Due to this customer don’t feel confident and think the site as fake one. To avoid this situation we can use separate mail hosting services or servers which are not black list to ensure delivery to customer inbox.

Starting from cre loaded 6.3 we can tell which smtp server to be used to send emails. It is also possible to add this feature in old versions.

In this post I will guide you how to add smtp feature, but before start keep these in mind

  • None of the code is written by me so the code belongs to its respective owners
  • Backup your files and database for in case any thing goes wrong
  • The version I am using is 6.2 Pro, I think procedure will be same for simple and B2B version

What you will need

We need SMTP class library (includes/classes/class.smtp.php)available in version 6.3 and 6.4, I got it from 6.4.0 Pro, but i think it will be same for Community addition.
Copy the file includes/classes/class.smtp.php from version 6.3 or 6.4 to includes/classes/ folder. This will be used for sending emails from front end for admin area copy same file in admin/includes/classes/.
So two new files will be includes/classes/class.smtp.php and admin/includes/classes/class.smtp.php

Adding Required Configurations:
Add following configurations in your database, these will allow admins to do settings for SMTP server.

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, last_modified, date_added, use_function, set_function) VALUES ( 'SMTP Server Host Address', 'EMAIL_SMTP_HOST_SERVER', '', 'The fully qualified host name of the SMTP server.', '12', '10', '0000-00-00 00:00:00', '2009-06-16 20:09:42', '', '');


INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, last_modified, date_added, use_function, set_function) VALUES ('SMTP Server EHLO / HELO Name', 'EMAIL_SMTP_HELO_SERVER', '', 'A name to send as part of the SMTP EHLO / HELO commands. The name is typically the hostname of the machine this web site runs on.', '12', '11', '0000-00-00 00:00:00', '2009-06-16 20:09:42', '', '');


INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, last_modified, date_added, use_function, set_function) VALUES ('SMTP Server Port Number', 'EMAIL_SMTP_PORT_SERVER', '25', 'The SMTP server port number. Port number 25 is typically used by default.', '12', '12', '0000-00-00 00:00:00', '2009-06-16 20:09:42', '', '');


INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, last_modified, date_added, use_function, set_function) VALUES ('SMTP Authentication Required', 'EMAIL_SMTP_ACTIVE_PASSWORD', 'true', 'Set to true when the SMTP Server requires password authentication.', '12', '13', '0000-00-00 00:00:00', '2009-06-16 20:09:42', '', 'tep_cfg_select_option(array(\'true\', \'false\'), ');


INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, last_modified, date_added, use_function, set_function) VALUES ('SMTP Authentication Username', 'EMAIL_SMTP_USERNAME', '', 'The e-mail username sent to the server when SMTP password authentication is required.', '12', '14', '0000-00-00 00:00:00', '2009-06-16 20:09:42', '', '');


INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, last_modified, date_added, use_function, set_function) VALUES ('SMTP Authentication Password', 'EMAIL_SMTP_PASSWORD', '', 'The e-mail password sent to the server when SMTP password authentication is required.', '12', '15', '0000-00-00 00:00:00', '2009-06-16 20:09:42', '', '');

Modifying files:
You will need to modify two files one for front and second for admin. These files will be includes/classes/email.php and admin/includes/classes/email.php

Now open includes/classes/email.php
Find

if (EMAIL_TRANSPORT == 'smtp') {
return mail($to_addr, $subject, $this->output, 'From: ' . $from . $this->lf . 'To: ' . $to . $this->lf . implode($this->lf, $this->headers) . $this->lf . implode($this->lf, $xtra_headers));

Replace line

return mail($to_addr, $subject, $this->output, 'From: ' . $from . $this->lf . 'To: ' . $to . $this->lf . implode($this->lf, $this->headers) . $this->lf . implode($this->lf, $xtra_headers));

With this code

include_once(DIR_WS_CLASSES . ‘class.smtp.php’);

// Build up the SMTP connection parameter list
$params['host'] = EMAIL_SMTP_HOST_SERVER; // The smtp server host/ip
$params['port'] = EMAIL_SMTP_PORT_SERVER; // The smtp server port
$params['helo'] = EMAIL_SMTP_HELO_SERVER; // helo/ehlo command string; typically your domain/hostname
$params['auth'] = EMAIL_SMTP_ACTIVE_PASSWORD; // Whether to use basic authentication or not
$params['user'] = EMAIL_SMTP_USERNAME; // Username for authentication
$params['pass'] = EMAIL_SMTP_PASSWORD; // Password for authentication

// Prepare the recipient names; there can be multiple recipients in the to_addr seperated by a comma.
// Create an array of the recipients and then strip off everything and just leave the internet style
// email address behind. For example: “MyCuteName <me@mydomain.com>” => “me@mydomain.com”
$recipients = explode(‘,’, $to_addr);
for ($i = 0; $i < count($recipients); $i++) {
$recipients[$i] = trim(preg_replace( ‘/(.*)<(.*)>(.*)/’, ‘$2′, $recipients[$i]));
}
$send_params['recipients'] = $recipients;

// Timestamp the message
$date = date(‘r’);

$send_params['headers'] = array_merge($this->headers, array(“From: $from”, “To: $to”, “Subject: $subject”, “Date: $date”));

// This is used as in the MAIL FROM: cmd
// It should end up as the Return-Path: header
$send_params['from'] = $from_addr;

// The body of the email message
$send_params['body'] = $this->output;

//Send the email via SMTP
return (is_object($smtp = smtp::connect($params)) AND $smtp->send($send_params));

For file admin/includes/classes/email.php do the same as above and after that
Find:

case (($text == true) && ($attachments == false) && ($html == false)):

Replace with

case (($text == true) && ($attachments == false)):

and thats it now go to admin area and update smtp settings in Configuration > Email Options. I am using this code in one of my sites so it should work if every thing is done as described.

3 Responses to “Adding SMTP server support in CRE Loaded 6.2”

  1. Hi,

    I receive an error on this line :
    $recipients[$i] = trim(preg_replace( ‘/(.*)(.*)/’, ‘$2′, $recipients[$i]));

    Does someone got the same issue ?
    Kind regards.
    Jeanflo

  2. I think you need to do a bit cleaning in code because when you copy the code all single and double quote (‘ and “) become messy and not recognized by php. Please put these manually in your php editor.

  3. Alexandr Vergelis on June 13th, 2010 at 7:42 am

    For me, as a poet, it was very interesting!

Leave a Reply