Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-02-28 03:06:11

aslsw66
Member
From: Canberra, Australia
Registered: 2004-08-04
Posts: 342
Website

SMTP authentication for TxP emails

After moving everything across to a new hosting provider, I’ve been going through and testing everything to make sure it works.

I’ve found that emails from Textpattern aren’t getting out. Even adding the “SMTP envelope sender address” in Admin preferences doesn’t work.

I sent a query off to the hosting provider, and there response says:

“You need to actually physically have the scipt login to an email address in order to send emails.” It sounds like they have locked this down pretty tight.

In hunting around the forum, I have found a few topics relating to this. The best seems to be this Textpattern modifications to replace PHP mail() function (not on the forum, but someone linked to it). The only problem seems to be that the version of txplib_misc.php they refer to is outdated.

Does anyone else have some tips on how to get this to work? I’m not a hardcore programmer, but can read through PHP and have a try if someone can point me in the right direction.

Thanks.

PS. Or should I just find another host? The support from this one is 1000% better than my last one, but I’ve only signed up for month-to-month so far.

Offline

#2 2012-02-28 04:00:05

joebaich
Member
From: DC Metro Area and elsewhere
Registered: 2006-09-24
Posts: 507
Website

Re: SMTP authentication for TxP emails

See my post here which dealt with this same problem, a host, in this case Rackspace (was Mosso), disabling phpmail(). It involves some minor changes to txplib_misc.php and adding the file pearmail.php to the lib folder. It’s an old post but I still use the procedure. The line numbers in more recent versions of txp_lib_misc.php have changed but the search for mail( gets you to the right place. There are two instances of mail( to replace with smtp( in more recent versions of the file.

You will need to use the same methodology to patch plug-ins like zem_contact_reborn that rely on phpmail(). The search and replace for mail( makes it pretty straightforward whatever the plug-in. Note the requirement to call (include) pearmail.php at the beginning of each function containing smtp(.

At first sight, this sounds horrendous but it isn’t. Read the post and the links and it should all fall into place.

Last edited by joebaich (2012-02-28 04:00:44)

Offline

#3 2012-02-28 05:25:27

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 590
Website

Re: SMTP authentication for TxP emails

It sounds like they have locked this down pretty tight.

You can say that again. Have you looked at Site5? They have a data center in Australia now, and I can highly recommend them for Textpattern work. You wouldn’t have to do this type of modification there.

Offline

#4 2012-02-28 09:43:39

aslsw66
Member
From: Canberra, Australia
Registered: 2004-08-04
Posts: 342
Website

Re: SMTP authentication for TxP emails

Thanks for the advice.

As I have just moved everything over to the new provider, I guess I will have a go at some modifications. Normally I would be nervous, but it doesnt feel like I have too much to lose (I still have copies of all of my files and database backups). I can read code, so baby steps should do the trick (and lots of help from the forums naturally).

I’m not inclined to change provider yet – this provider matched the cheap price I was paying previously but there support has been pretty good eg. emails late at night. But if there are too many problems it might just be too much…

Offline

#5 2012-02-28 12:36:49

joebaich
Member
From: DC Metro Area and elsewhere
Registered: 2006-09-24
Posts: 507
Website

Re: SMTP authentication for TxP emails

aslsw66 wrote:

I guess I will have a go at some modifications. Normally I would be nervous, but it doesnt feel like I have too much to lose (I still have copies of all of my files and database backups). I can read code, so baby steps should do the trick.

Happy to help. The procedure I alluded to in my previous post is safe enough, I think. I make some minor modifications to the Textpattern installation files on my computer and use that to install TXP on Rackspace and the other hosts I use. I only do it once when there is a new Textpattern version.
  • Make a copy of the txplib_misc.php file and rename it txplib_misc_smtp.php and rename the original to txplib_misc_orig.php.
  • Drop a copy of pearmail.php into textpattern/lib.
  • Open txplib_misc_smtp.php in a text editor and search for “ mail(”. Note the leading space in the search term. There are two instances of mail( close to each other in the section of code around line 1179 (that may change in future versions of the file)
return mail($to_address, $subject, $body, $headers, '-f'.$prefs['smtp_from']);
			}
		}

		return mail($to_address, $subject, $body, $headers);
	}


  • Amend both instances of mail( to smtp(

return smtp($to_address, $subject, $body, $headers, '-f'.$prefs['smtp_from']);
			}
		}

		return smtp($to_address, $subject, $body, $headers);
	}


  • Scroll back up the file to the beginning of the function txpmail at or around line 1113 and add a call to pearmail.php, i.e amend

	function txpMail($to_address, $subject, $body, $reply_to = null)
	{
		global $txp_user, $prefs;

to

	function txpMail($to_address, $subject, $body, $reply_to = null)
	{
		require_once "pearmail.php";
		global $txp_user, $prefs;
  • Save the file.

Use this amended set of Textpattern files to install all instances of TXP.

  • If the host doesn’t support php mail(),
    • rename the uploaded txplib_misc_smtp.php back to txplib_misc.php.
    • Amend the opening block of code in pearmail.php to reflect the appropriate smtp email user name and password.
  • If the host supports php mail(), rename txplib_misc_orig.php back to txplib_misc.php instead.

That’s it. Obviously test it out.

You can amend plug-ins that require php mail() using the same methodology.

For ease of reference, here is a copy of the aforementioned file pearmail.php.

<?php
/***********************************************************************
This is a Pear::Mail implementation of the php mail() function to allow
scripts that use mail() to work on Web Hosts that do not support PHP’s mail() function with minimal edits. Place
the line:

require_once "pearmail.php";

at the head of any file that uses the mail() function. Because PHP does
not support function overriding, it is not possible to actually replace
mail(). Therefore, the package includes a function called smtp() which
is semantically close to mail(). All instances of the mail() function
have to be replaced with smtp (just edit mail to read smtp, leave the
parameter list alone).

Note that the $additional_parameters parameter is not supported by
smtp(). No placeholder is provided, so that the unavailability of
any behaviour depending on this parameter is flagged.

Place your SMTP server info at the top of the smtp() function in this
file. These parameters are kept here to avoid contaminating the namespace
of the including file.

Ensure that the From header is correctly formed if it is provided. The
SMTP server may refuse to send mail if it doesn't like the From header
or is no From header is provided. If smtp() is called without a From
header provided, a simple one will be added; ensure that the SMTP
server will accept the address given for $smtp_default_from.

Troubleshooting: A simple way to troubleshoot is to try uncommenting
line 80. The error message attribute of any error object returned by
the $smtp->send() method will be echoed.
************************************************************************/

require_once "Mail.php";

function smtp($to, $subject, $message, $additional_headers="") {

    # Declare SMTP Server Settings
    $smtp_server = "myserver.myhost.com";
    $smtp_username = "username";
    $smtp_password = "password";
    $smtp_default_from = "nottotallybogus@example.com";

    # Cast inputs to strings
    $to = (string) $to;
    $subject = (string) $subject;
    $message = (string) $message;
    $additional_headers = (string) $additional_headers;

    # Re-construct the headers into an array as expected by Pear
    $raw_headers = explode("\n", $additional_headers);
    $headers = Array("To"=>$to, "Subject"=>$subject);
    foreach($raw_headers as $raw_header) {
        $header = explode(":",$raw_header,2);
        if (count($header) != 2) {
            continue;     # Here the behaviour may differ somewhat with mail()... 
                        # malformed headers will be discarded silently.
        }
        if (trim(strtolower($header[0])) == "to" || trim(strtolower($header[0])) == "subject") {
            continue;     # No overriding To and Subject
        }
        $headers[ucfirst(trim($header[0]))] = trim($header[1]); # Key will start uppercase
    }

    # Set a default From header if none was provided
    if (!array_key_exists("From", $headers)) {
        $headers["From"] = $smtp_default_from;
    }

    # Create the smtp object and send mail. Must return true on success,
    # false on failure.

    $smtp = Mail::factory("smtp", Array("host"=>$smtp_server, "auth"=>true,
                                        "username"=>$smtp_username, "password"=>$smtp_password));

    $result = $smtp->send($to, $headers, $message);

    if (PEAR::IsError($result)) {
        # echo $result->getMessage();
        return false;
    } else {
        return true;
    }
}
?>

Last edited by joebaich (2012-02-28 12:48:10)

Offline

#6 2017-04-13 12:47:36

mmelon
Member
Registered: 2006-03-02
Posts: 95

Re: SMTP authentication for TxP emails

hmmn this file has changed a bit in the last 5 years

Offline

Board footer

Powered by FluxBB