...

View Full Version : function mail() not working



maghiel
03-22-2007, 05:02 PM
ello,

rather nasty problem,
a website moved to another host, and there the mail() function just won't work. sysop said it should work, and even when forcing the smtp host and port it fails

the sysop pointed out to me to use HELO. So I tried this function i found in the comments on mail() in the php manual



Hope the links might be useful to some people :)
josephcmiller2 at gmail dot com
26-May-2006 04:26
I have been using the function mymail() provided by Antony Male (below) to send mail on my server, but with a couple of modifications. First, I needed to enable AUTH LOGIN in order to use my smtp server from my ISP. Second, his mymail() function allows for a $from to be used, but the function does not accept any such parameter. I have addressed the latter issue by parsing the headers to obtain the From: address. If this is not provided, failed mail will not be returned properly to the sender.

// modified to provide authenticated logins
function mymail($to,$subject,$message,$headers)
{

// set as global variable
global $GLOBAL;

// get From address
if ( preg_match("/From:.*?[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+.*/", $headers, $froms) ) {
preg_match("/[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+/", $froms[0], $fromarr);
$from = $fromarr[0];
}

// Open an SMTP connection
$cp = fsockopen ($GLOBAL["SMTP_SERVER"], $GLOBAL["SMTP_PORT"], &$errno, &$errstr, 1);
if (!$cp)
return "Failed to even make a connection";
$res=fgets($cp,256);
if(substr($res,0,3) != "220") return "Failed to connect";

// Say hello...
fputs($cp, "HELO ".$GLOBAL["SMTP_SERVER"]."\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "Failed to Introduce";

// perform authentication
fputs($cp, "auth login\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "334") return "Failed to Initiate Authentication";

fputs($cp, base64_encode($GLOBAL["SMTP_USERNAME"])."\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "334") return "Failed to Provide Username for Authentication";

fputs($cp, base64_encode($GLOBAL["SMTP_PASSWORD"])."\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "235") return "Failed to Authenticate";

// Mail from...
fputs($cp, "MAIL FROM: <$from>\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "MAIL FROM failed";

// Rcpt to...
fputs($cp, "RCPT TO: <$to>\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "RCPT TO failed";

// Data...
fputs($cp, "DATA\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "354") return "DATA failed";

// Send To:, From:, Subject:, other headers, blank line, message, and finish
// with a period on its own line (for end of message)
fputs($cp, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "Message Body Failed";

// ...And time to quit...
fputs($cp,"QUIT\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "221") return "QUIT failed";

return true;
}


but that wont work either.
does anyone has an idea how to fix?

Nightfire
03-22-2007, 05:06 PM
What do you mean "doesn't work"? Comes up with errors, doesn't send an email, mail function not enabled, kermit the frog doesn't dance?

firepages
03-22-2007, 05:13 PM
Kermit never could dance... now Miss Piggy ... thats another matter ;)

don't mess around with talking directly to mailservers, net time you move you may well have to do this all over again for a different type of mailserver (been there burnt the t-shirt)

try phpmailer or similar class , it lets you use your local sendmail or any SMTP server that will relay for you , it does what you are trying to do but without the headaches.

http://phpmailer.sourceforge.net/

maghiel
03-23-2007, 10:03 AM
What do you mean "doesn't work"? Comes up with errors, doesn't send an email, mail function not enabled, kermit the frog doesn't dance?

mail() only gives back a boolean. so it returns FALSE. doh :p


Kermit never could dance... now Miss Piggy ... thats another matter ;)

don't mess around with talking directly to mailservers, net time you move you may well have to do this all over again for a different type of mailserver (been there burnt the t-shirt)

try phpmailer or similar class , it lets you use your local sendmail or any SMTP server that will relay for you , it does what you are trying to do but without the headaches.

http://phpmailer.sourceforge.net/

thnx for the tip m8. Never had any problem like this before. I'll look into the phpmailer class. And well, wouldn't have much headaches from it by the way. I included the function above als function mail2(), so easily replaced with the 'normal' function when moving :)

kaisellgren
03-24-2007, 11:26 AM
Maybe you put this on the very top of your document:

ini_set("error_reporting","E_ALL");

And then tell us what errors you get.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum