I have had success with IIS/SMTP using the following function that I found
on the PHP website function list (I've only slightly optimized the original
with some constants). Apparently, IIS/SMTP doesn't like the added '<' and
'>' around the "from" address, but it can be coerced into it with this
function. I've tested successfully with Outlook and Thunderbird.
JW
<?php
function send_mail($to, $body, $subject, $fromaddress, $fromname,
$attachments = FALSE)
{
define('EOL', "\r\n");
define('LT', '<');
define('GT', '>');
$headers = '';
$mime_boundary = md5(time());
# Common Headers
$headers .= "From: " . $fromname . LT . $fromaddress .
GT . EOL;
$headers .= "Reply-To: " . $fromname . LT . $fromaddress .
GT . EOL;
$headers .= "Return-Path: " . $fromname . LT . $fromaddress .
GT . EOL;
$headers .= "Message-ID: <" . time() . "-" . $fromaddress .
GT . EOL;
$headers .= "X-Mailer: PHP v" . phpversion() . EOL;
# Boundary for marking the split & Multitype Headers
$headers .= 'MIME-Version: 1.0' . EOL . EOL;
$headers .= "Content-Type: multipart/mixed; boundary=\" ".
$mime_boundary . "\"" . EOL . EOL;
# Open the first part of the mail
$msg = "--" . $mime_boundary . EOL;
$htmlalt_mime_boundary = $mime_boundary . "_htmlalt"; //we must
define a different MIME boundary for this section
# Setup for text OR html -
$msg .= "Content-Type: multipart/alternative; boundary=\"" .
$htmlalt_mime_boundary . "\"" . EOL . EOL;
# Text Version
$msg .= "--" . $htmlalt_mime_boundary . EOL;
$msg .= "Content-Type: text/plain; charset=iso-8859-1" . EOL;
$msg .= "Content-Transfer-Encoding: 8bit" . EOL . EOL;
$msg .= strip_tags(str_replace("<br>", "\n", substr($body,
(strpos($body, "<body>") + 6)))) . EOL . EOL;
# HTML Version
$msg .= "--" . $htmlalt_mime_boundary . EOL;
$msg .= "Content-Type: text/html; charset=iso-8859-1" . EOL;
$msg .= "Content-Transfer-Encoding: 8bit" . EOL . EOL;
$msg .= $body . EOL . EOL;
//close the html/plain text alternate portion
$msg .= "--" . $htmlalt_mime_boundary . "--" . EOL . EOL;
if ($attachments !== FALSE)
{
for($i = 0; $i < count($attachments); $i++)
{
if (is_file($attachments[$i]["file"]))
{
# File for Attachment
$file_name = substr($attachments[$i]["file"],
(strrpos($attachments[$i]["file"], "/") + 1));
$handle = fopen($attachments[$i]["file"], 'rb');
$f_contents = fread($handle,
filesize($attachments[$i]["file"]));
$f_contents = chunk_split(base64_encode($f_contents));
//Encode The Data For Transition using base64_encode();
$f_type = filetype($attachments[$i]["file"]);
fclose($handle);
# Attachment
$msg .= "--" . $mime_boundary . EOL;
$msg .= "Content-Type: " . $attachments[$i]["content_type"] .
"; name=\"" . $file_name . "\"" . EOL; // sometimes i have to send MS Word,
use 'msword' instead of 'pdf'
$msg .= "Content-Transfer-Encoding: base64" . EOL;
$msg .= "Content-Description: " . $file_name . EOL;
$msg .= "Content-Disposition: attachment; filename=\"" .
$file_name . "\"" . EOL . EOL; // !! This line needs TWO end of lines !!
IMPORTANT !!
$msg .= $f_contents . EOL . EOL;
}
}
}
# Finished
$msg .= "--" . $mime_boundary . "--" . EOL . EOL; // finish with
two eol's for better security. see Injection.
# SEND THE EMAIL
ini_set(sendmail_from, $fromaddress); // the INI lines are to force
the From Address to be used !
$mail_sent = mail($to, $subject, $msg, $headers);
ini_restore(sendmail_from);
return $mail_sent;
}
?>
-----Original Message-----
From: Dan Richfield [mailto:[EMAIL PROTECTED]
Sent: Tuesday, January 29, 2008 11:58
To: [email protected]
Subject: [PHP-WIN] mail() Incorrect Address Format when using IIS7/FastCGI
When using IIS7 with PHP 5.2.5 with the FastCGIModule, the mail()
function returns the following error message when trying to use anything
but a plain email address (ex. [EMAIL PROTECTED]) in the From or To
headers:
PHP Warning: mail() [function.mail]: SMTP server response: 501 Incorrect
Address Format
If you use a plain email address it works fine. An example of what
causes this problem is as follows:
Test <[EMAIL PROTECTED]>
The problem is that PHP is formatting the header incorrectly but adding
additional < and > tags around the entire address. This is illustrated
in this excerpt from my mail server log:
16:28:05.79 5 SMTPI-39353([XXX.XXX.XXX.XXX]) inp: MAIL FROM:<Test
<[EMAIL PROTECTED]>>
With a plain email address, it still adds the < and > tags, but it
works. Here is an example of that from the mail server log:
16:28:21.72 5 SMTPI-39408([XXX.XXX.XXX.XXX]) inp: MAIL
FROM:<[EMAIL PROTECTED]>
Also, this same behavior was reported in Bug #28038 but I have confirmed
that this is error does not occurs when using this same PHP version in
CGI mode with IIS7. That tells me this bug was fixed, but has reoccured
in 5.2.5 with FastCGI.
Reproduce code:
---------------
<?php
$To = "[EMAIL PROTECTED]";
$Subject = "Test";
$Body = "Test";
$Headers = "From: Test User <[EMAIL PROTECTED]>" . "\r\n";
mail($To, $Subject, $Body, $Headers);
print "mail sent!";
?>
Expected result:
----------------
True result
Actual result:
--------------
PHP Warning: mail() [function.mail]: SMTP server response: 501 Incorrect
Address Format
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php