Relay SMTP mail to external mail server using smtp.class

Note that there is now also a SMTP module that will do this for you: http://drupal.org/project/smtp

The following are steps that I used to get Drupal 4.6.x running on Apache with PHP 4.3.10-15 to successfully relay mail to an external mail gateway without using the php_mail() function. I have tested this on Postfix and Exchange server mail gateways successfully.

1. Download class.smtp.inc from http://www.phpguru.org/downloads/smtp/smtp.class-1.0.5.tar.gz.
2. Untar using tar –xzvf and copy class.smtp.inc to the /includes directory.
3. Edit the file and change the following lines to coincide with your external mail gateway:

  • $this->host = 'localhost'; Change localhost to the IP address of your mail gateway. You may also use the FQDN instead of the IP address if you have DNS set up correctly to something like yourmailgateway.yourdomain.com.
  • $this->helo = 'localhost'; Change localhost to reflect the hostname (FQDN) of your external mail gateway. For example: yourmailgateway.yourdomain.com.
  • If your gateway requires SMTP authentication to relay mail, then you will need to modify the following lines to appropriately reflect the authentication credentials. (If you manage your own mail gateway or it resides within local subnets behind your firewall, then you should be able to leave authentication set to FALSE and allow relaying based upon IP address/network ID. This will probably only affect you if you use a hosting company for email management.
  • $this->auth = FALSE; (change to TRUE)
    $this->user = ''; (put username here(you may need to preceed with domain; e.g. domain\username))
    $this->pass = ''; (put password here)

4. Download smtp.inc from http://cvs.drupal.org/viewcvs/drupal/contributions/tricks/smtp/ and place it in your /includes directory. No modification to this file is needed.
5. Add the following line to the sites/default/settings.php file before the closing ?> tag:

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

6. Finally be sure to add the IP address of the mail gateway to the [mail function] section in your php.ini file and restart your web server.

Good Luck! I hope this saves you the hours of researching it took me to get it running.

Any idea ...

Phoenix77 - January 9, 2006 - 07:00

on how to get this to work under PHP 5?

the class.smtp.inc does not work, I get an error concerning the variable $this on line 78: if(!isset($this->status)){.

Could have something todo with the way PHP 5.x handles classes?

The Phoenix77

In my case, the resolution

nuxlli - June 5, 2006 - 13:50

In my case, the resolution of problem:

Replace line in class.smtp.inc:

if(!isset($this->status)){

for :

if(!isset($this) || !isset($this->status)){

Corrections that make it work for me

acros - April 30, 2006 - 14:19

Firstly, very many thanks to the original poster for this. It would have taken me ages to find the user_mail_wrapper function.

  1. Follow steps 1, 2 and 4 as stated.
  2. Don't edit class.smtp.inc (this class is instantiated in smtp.inc and the instantiation passes params that will overwrite edits you make in class.smtp.inc). If you need SMTP authentication then edit smtp.inc as follows:
    • set $params['auth'] = TRUE;
    • uncomment //$params['user'] = 'testuser'; and put your username in
    • uncomment //$params['pass'] = 'testuser'; and put your password in
    • There is no need to edit $params['host'] (it reads what you set in the php.ini file in step 6)
    • It is highly unlikely you will need to edit $params['port']. You will know if you do need to and if you are in doubt then you don't
  3. Follow step 5 but use the corrected entry already posted. i.e. $conf["smtp_library"] = "includes/smtp.inc";
  4. Follow step 6 as stated. It can be FQDN instead of IP.

I'm using WinXP Apache 2 and

couloir007 - August 10, 2006 - 17:57

I'm using WinXP Apache 2 and php 5 and have just spent about 6 hours trying to get mail to work. Drupal better be worth it. It turns out the that "if (variable_get('smtp_library', '') && file_exists(variable_get('smtp_library', ''))) {" in use.module was failing, perhaps a linux vs win issue. Either way, I can only get it to work with phpmailer.inc at this point. Also, my anti-virus program caused the biggest problem, since I couldn't isolate the other problem until I figured out anti-virus programs don't like you sending mail without permission, which I still haven't figured out, I just turn it off.

Getting Mail work for Drupal 4.7.*

bellars - December 1, 2006 - 14:02

I am confident this post its gonno help someone.

I have labored so hard to make smtp.ini work only to discover that a little tweak is all that I need. I have used the original post above following the corrections on "Corrections that make it work for me" .

After doing this I discovered that I was now having blank screen and when I checked the error log using the admin login all I saw was mail send error: AUTH command failed: Invalid Username or Password . Since I am very sure that my Username and Password is correct, I then concluded that smtp.ini was accessing a wrong SMTP server.

Changing just two line in smtp.ini solved this problem.

I changed
$params['host'] = ni_get('SMTP');

to
$params['host'] = 'smtp.1and1.com' ;

and

$params['helo'] = $_SERVER['HTTP_HOST'];

to
$params['helo'] = 'smtp.1and1.com' ;

I restarted Apache and I was smiling.

-----------------
Get IT Done NOW

 
 

Drupal is a registered trademark of Dries Buytaert.