I don't know whether this is the right place to put this comment, so if there is a better place, feel free to repost this where you think it belongs.

Amazingly, Drupal 4.7 still doesn't have an SMTP Auth capability built in. It is trivial to add it, though, so maybe that is why.

Here is what I did to implement SMTP Auth for my windows installation. I'm not going to pretend that this is the most efficient way of making SMTP Auth work, but it did work for me (after too much trial and error, even after reading all the other documentation and posts I could find here at drupal.org - maybe that says more about me than anything else).

1. Download two include files and put them into the /includes folder. These two files are:

a. smtp.inc
b. class.smtp.inc

I found my /includes folder at c:\program files\apache\www\Drupal\includes

Of course, your installation might find this folder in a different location.

At the time of this writing, these files could be found at:

http://cvs.drupal.org/viewcvs/*checkout*/drupal/contributions/tricks/smt...

and:

http://drupal.org/files/issues/class.smtp.inc

I will also copy these files inline, just to make sure they don't get lost (see below).

2. Modify the settings.php file so that the code in the above include files gets included. I found settings.php in my c:\program files\apache\www\Drupal\sites\default\ folder. Of course, your installation might find this file in a different location.

The specfic modification that I did to my settings.php file was to add the following line:

$conf["smtp_library"]="./includes/smtp.inc";

You can add the line virtually anywhere in the file and it will work.

3. Make sure you stop and restart the apache server and php so that the above settings file takes affect.

4. Modify the smtp.inc file so that it has the information needed.

These are the lines that I confirmed/set:

$params['port'] = 25;
$params['helo'] = $_SERVER['__mysmtpserver__'];
$params['auth'] = TRUE;
$params['user'] = '__emailusername__';
$params['pass'] = '__password__';

where __mysmtpserver__ is replaced with the internet address of your smtp server. It can be a dotted quad (e.g., 10.0.1.2) or a FQDN (e.g., smtp.mydomain.com).

where __emailusername__ is replaced with the name of the user you intend the system to pretend to be so that it can use the smtp server

where __password__ is the plain text password

Be sure to replace both the words and the underscores both before and after.

5. Modify the class.smtp.inc file so that it has the information needed.

These are the lines that I set:

$this->host = '__mysmtpserver__';
$this->port = 25;
$this->helo = '__mysmtpserver__';
$this->auth = TRUE;
$this->user = '__emailusername__';
$this->pass = '__password__';

Yes, I know that it is highly likely that some part of the above changes were redundant.

I can live with that because it just works.

I hope this helps somebody.

Mike

Here is the smtp.inc file:

// $Id: smtp.inc,v 1.6 2003/12/11 13:59:58 mathias Exp $
include_once 'includes/class.smtp.inc';

function user_mail_wrapper($mail, $subject, $message, $header) {
  // The smtp server host/ip
  $params['host'] = ini_get('SMTP');
  // The smtp server port
  $params['port'] = 25;
  // What to use when sending the helo command. Typically, your domain/hostname
  $params['helo'] = $_SERVER['HTTP_HOST'];
  // Whether to use basic authentication or not
  $params['auth'] = FALSE;
  // Username for authentication
  //$params['user'] = 'testuser';
  // Password for authentication
  //$params['pass'] = 'testuser';

  // The recipients (can be multiple)
  $send_params['recipients'] = $mail;
  // The headers of the mail
  $send_params['headers'] = explode("\n", $header ."\nSubject: $subject\nTo: $mail");
  // The body of the email
  $send_params['body'] = str_replace("\n", "\r\n", $message);

  $smtp = smtp::connect($params);
  if (count($smtp->errors)) {
    watchdog('error', 'mail connect error: '. implode('<br />', $smtp->errors));
    return false;
  }
  $smtp->send($send_params);
  if (count($smtp->errors)) {
    watchdog('error', 'mail send error: '. implode('<br />', $smtp->errors));
    return false;
  }

  return true;
}

Here is the class.smtp.inc file:

/***************************************
** Filename.......: class.smtp.inc
** Project........: SMTP Class
** Version........: 1.0.5
** Last Modified..: 21 December 2001
***************************************/

	define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE);
	define('SMTP_STATUS_CONNECTED', 2, TRUE);

	class smtp{

		var $authenticated;
		var $connection;
		var $recipients;
		var $headers;
		var $timeout;
		var $errors;
		var $status;
		var $body;
		var $from;
		var $host;
		var $port;
		var $helo;
		var $auth;
		var $user;
		var $pass;

		/***************************************
        ** Constructor function. Arguments:
		** $params - An assoc array of parameters:
		**
		**   host    - The hostname of the smtp server		Default: localhost
		**   port    - The port the smtp server runs on		Default: 25
		**   helo    - What to send as the HELO command		Default: localhost
		**             (typically the hostname of the
		**             machine this script runs on)
		**   auth    - Whether to use basic authentication	Default: FALSE
		**   user    - Username for authentication			Default: <blank>
		**   pass    - Password for authentication			Default: <blank>
		**   timeout - The timeout in seconds for the call	Default: 5
		**             to fsockopen()
        ***************************************/

		function smtp($params = array()){

			if(!defined('CRLF'))
				define('CRLF', "\r\n", TRUE);

			$this->authenticated	= FALSE;			
			$this->timeout			= 5;
			$this->status			= SMTP_STATUS_NOT_CONNECTED;
			$this->host				= 'localhost';
			$this->port				= 25;
			$this->helo				= 'localhost';
			$this->auth				= FALSE;
			$this->user				= '';
			$this->pass				= '';
			$this->errors   		= array();

			foreach($params as $key => $value){
				$this->$key = $value;
			}
		}

		/***************************************
        ** Connect function. This will, when called
		** statically, create a new smtp object, 
		** call the connect function (ie this function)
		** and return it. When not called statically,
		** it will connect to the server and send
		** the HELO command.
        ***************************************/

		function &connect($params = array()){

			if(!isset($this->status)){
				$obj = new smtp($params);
				if($obj->connect()){
					$obj->status = SMTP_STATUS_CONNECTED;
				}

				return $obj;

			}else{
				$this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
				if(function_exists('socket_set_timeout')){
					@socket_set_timeout($this->connection, 5, 0);
				}

				$greeting = $this->get_data();
				if(is_resource($this->connection)){
					return $this->auth ? $this->ehlo() : $this->helo();
				}else{
					$this->errors[] = 'Failed to connect to server: '.$errstr;
					return FALSE;
				}
			}
		}

		/***************************************
        ** Function which handles sending the mail.
		** Arguments:
		** $params	- Optional assoc array of parameters.
		**            Can contain:
		**              recipients - Indexed array of recipients
		**              from       - The from address. (used in MAIL FROM:),
		**                           this will be the return path
		**              headers    - Indexed array of headers, one header per array entry
		**              body       - The body of the email
		**            It can also contain any of the parameters from the connect()
		**            function
        ***************************************/

		function send($params = array()){

			foreach($params as $key => $value){
				$this->set($key, $value);
			}

			if($this->is_connected()){

				// Do we auth or not? Note the distinction between the auth variable and auth() function
				if($this->auth AND !$this->authenticated){
					if(!$this->auth())
						return FALSE;
				}

				$this->mail($this->from);
				if(is_array($this->recipients))
					foreach($this->recipients as $value)
						$this->rcpt($value);
				else
					$this->rcpt($this->recipients);

				if(!$this->data())
					return FALSE;

				// Transparency
				$headers = str_replace(CRLF.'.', CRLF.'..', trim(implode(CRLF, $this->headers)));
				$body    = str_replace(CRLF.'.', CRLF.'..', $this->body);
				$body    = $body[0] == '.' ? '.'.$body : $body;

				$this->send_data($headers);
				$this->send_data('');
				$this->send_data($body);
				$this->send_data('.');

				$result = (substr(trim($this->get_data()), 0, 3) === '250');
				//$this->rset();
				return $result;
			}else{
				$this->errors[] = 'Not connected!';
				return FALSE;
			}
		}
		
		/***************************************
        ** Function to implement HELO cmd
        ***************************************/

		function helo(){
			if(is_resource($this->connection)
					AND $this->send_data('HELO '.$this->helo)
					AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){

				return TRUE;

			}else{
				$this->errors[] = 'HELO command failed, output: ' . trim(substr(trim($error),3));
				return FALSE;
			}
		}
		
		/***************************************
        ** Function to implement EHLO cmd
        ***************************************/

		function ehlo(){
			if(is_resource($this->connection)
					AND $this->send_data('EHLO '.$this->helo)
					AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){

				return TRUE;

			}else{
				$this->errors[] = 'EHLO command failed, output: ' . trim(substr(trim($error),3));
				return FALSE;
			}
		}
		
		/***************************************
        ** Function to implement RSET cmd
        ***************************************/

		function rset(){
			if(is_resource($this->connection)
					AND $this->send_data('RSET')
					AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){

				return TRUE;

			}else{
				$this->errors[] = 'RSET command failed, output: ' . trim(substr(trim($error),3));
				return FALSE;
			}
		}
		
		/***************************************
        ** Function to implement QUIT cmd
        ***************************************/

		function quit(){
			if(is_resource($this->connection)
					AND $this->send_data('QUIT')
					AND substr(trim($error = $this->get_data()), 0, 3) === '221' ){

				fclose($this->connection);
				$this->status = SMTP_STATUS_NOT_CONNECTED;
				return TRUE;

			}else{
				$this->errors[] = 'QUIT command failed, output: ' . trim(substr(trim($error),3));
				return FALSE;
			}
		}
		
		/***************************************
        ** Function to implement AUTH cmd
        ***************************************/

		function auth(){
			if(is_resource($this->connection)
					AND $this->send_data('AUTH LOGIN')
					AND substr(trim($error = $this->get_data()), 0, 3) === '334'
					AND $this->send_data(base64_encode($this->user))			// Send username
					AND substr(trim($error = $this->get_data()),0,3) === '334'
					AND $this->send_data(base64_encode($this->pass))			// Send password
					AND substr(trim($error = $this->get_data()),0,3) === '235' ){

				$this->authenticated = TRUE;
				return TRUE;

			}else{
				$this->errors[] = 'AUTH command failed: ' . trim(substr(trim($error),3));
				return FALSE;
			}
		}

		/***************************************
        ** Function that handles the MAIL FROM: cmd
        ***************************************/
		
		function mail($from){

			if($this->is_connected()
				AND $this->send_data('MAIL FROM:<'.$from.'>')
				AND substr(trim($this->get_data()), 0, 2) === '250' ){

				return TRUE;

			}else
				return FALSE;
		}

		/***************************************
        ** Function that handles the RCPT TO: cmd
        ***************************************/
		
		function rcpt($to){

			if($this->is_connected()
				AND $this->send_data('RCPT TO:<'.$to.'>')
				AND substr(trim($error = $this->get_data()), 0, 2) === '25' ){

				return TRUE;

			}else{
				$this->errors[] = trim(substr(trim($error), 3));
				return FALSE;
			}
		}

		/***************************************
        ** Function that sends the DATA cmd
        ***************************************/

		function data(){

			if($this->is_connected()
				AND $this->send_data('DATA')
				AND substr(trim($error = $this->get_data()), 0, 3) === '354' ){
 
				return TRUE;

			}else{
				$this->errors[] = trim(substr(trim($error), 3));
				return FALSE;
			}
		}

		/***************************************
        ** Function to determine if this object
		** is connected to the server or not.
        ***************************************/

		function is_connected(){

			return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED));
		}

		/***************************************
        ** Function to send a bit of data
        ***************************************/

		function send_data($data){

			if(is_resource($this->connection)){
				return fwrite($this->connection, $data.CRLF, strlen($data)+2);
				
			}else
				return FALSE;
		}

		/***************************************
        ** Function to get data.
        ***************************************/

		function &get_data(){

			$return = '';
			$line   = '';
			$loops  = 0;

			if(is_resource($this->connection)){
				while((strpos($return, CRLF) === FALSE OR substr($line,3,1) !== ' ') AND $loops < 100){
					$line    = fgets($this->connection, 512);
					$return .= $line;
					$loops++;
				}
				return $return;

			}else
				return FALSE;
		}

		/***************************************
        ** Sets a variable
        ***************************************/
		
		function set($var, $value){

			$this->$var = $value;
			return TRUE;
		}

	} // End of class

Comments

drupal777’s picture

Also change the following line in smtp.inc (Item # 4, above):

$params['host'] = '__mysmtpserver__';

Here is item # 4, repeated in its entirety for convenience:

4. Modify the smtp.inc file so that it has the information needed.

These are the lines that I confirmed/set:

$params['host'] = '__mysmtpserver__';
$params['port'] = 25;
$params['helo'] = $_SERVER['__mysmtpserver__'];
$params['auth'] = TRUE;
$params['user'] = '__emailusername__';
$params['pass'] = '__password__';

where __mysmtpserver__ is replaced with the internet address of your smtp server. It can be a dotted quad (e.g., 10.0.1.2) or a FQDN (e.g., smtp.mydomain.com).

where __emailusername__ is replaced with the name of the user you intend the system to pretend to be so that it can use the smtp server

where __password__ is the plain text password

Be sure to replace both the words and the underscores both before and after.

cmsproducer’s picture

The SMTP module supports authorization for sending mail and I am using that in my Windows installations that obviously do not have sendmail (). Here is commentary: Sending email from Drupal on Windows
-----
iDonny - Web Content Management System Design, Development. & CRM

drupal777’s picture

Well, I thank you for pointing out the module. Of course it works a treat. I wish I had found it before I went through the effort to patch the settings/includes. I guess people have a backup in case your module ends up not working for them.

sepeck’s picture

instead of off site?

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide -|- Black Mountain

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide

cmsproducer’s picture

I did not write the SMTP module, I just found it after I struggled with a non SMTP authenticating setup for a client (Although I am tempted to take credit, please give credit to the creator). Here is a link to the SMTP module within Drupal.org http://drupal.org/node/35189

-----
iDonny - Web Content Management System Design, Development. & CRM