note 43328 added to function.mail
| From: | just_somedood at yahoo dot com | Date: | Thu, 17 Jun 2004 16:44:02 +0000 |
| Subject: | note 43328 added to function.mail | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-71236@lists.php.net to get a copy of this message | ||
For those that have thier own SMTP socket functions, and need to use SMTP AUTH, but are having a
hard time with the CRAM-MD5 authentication mechanism, here's some (hopefully) helpful code:
<?php
function make_ipad($size)
{
//as defined in RFC #2104
$str = "";
for ($i=0;$i<$size;$i++)
$str .= chr(0x36);
return ($str);
}
function make_opad($size)
{
//as defined in RFC #2104
$str = "";
for ($i=0;$i<$size;$i++)
$str .= chr(0x5C);
return($str);
}
function padSecret ($secret)
{
//as defined in RFC #2104
for ($i=strlen($secret);$i<64;$i++)
{
//just null pad (character 0) the string
$secret .= chr(0x00);
}
return ($secret);
}
function keyedMD5($key,$data)
{
//this does a 'keyed' MD5 hash, as per the HMAC RFC #2104
$innerText = padSecret($key)^make_ipad(64);
$innerHash = pack("H*",md5($innerText . $data));
$done = md5 ((padSecret($key)^make_opad(64)) . $innerHash);
return ($done);
}
function cramMD5_encode ($user,$pass,$text)
{
//as defined in RFC 2195
//text should NOT be base64 encoded (decode before it comes into this function)
//result is the CRAM-MD5 string, encoded in base64
$encoded = keyedMD5($pass,$text);
return (base64_encode($user . " " . $encoded));
}
?>
The SMTP server i use won't do plaintext AUTH, and CRAM-MD5 seems to be widely used. Anyway,
it works for me :)
----
Manual Page -- http://www.php.net/manual/en/function.mail.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43328
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43328&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43328&report=yes
Search -- http://master.php.net/manage/user-notes.php