[nycphp-talk] custom mail() with attachment problemusingRH7.3/Apache
Phillip Powell
phillip.powell at adnet-sys.com
Fri Jun 11 12:09:40 EDT 2004
Thanx for your tips. I should have let you know that
"mime_content_type" is custom in my case as my instance of PHP does not
have that function (I'm using 4.3.2); simple one-liner that does an exec
on "file -bi " on the file wrapped around escapeshellarg().
Here is the updated function that now seems to work:
[PHP]
// ORIGINAL FUNCTION FOUND AT
http://www.php.net/manual/en/function.mail.php#41681 WITH cc AND bcc
MODIFICATIONS BY PHIL POWELL 6/10/2004
function mail_attach($to, $subject, $message, $from, $files = false,
$lb="\n", $cc = '', $bcc = '') {
/*----------------------------------------------------------------------------------------------
$to Recipient
$from Sender (like "email at domain.com" or "Name <email at domain.com>")
$subject Subject
$message Content
$files hash-array of files to attach
$lb is linebreak characters... some mailers need \r\n, others need \n
-------------------------------------------------------------------------------------------------*/
global $phpVersionInt;
$mime_boundary = "<<<:" . md5(uniqid(mt_rand(), 1));
$header = 'From: '. $from;
if ($cc) $header .= $lb . "Cc: $cc";
if ($bcc && (int)$phpVersionInt > 430) $header .= $lb . "Bcc: $bcc";
if (is_array($files)) {
$header.= $lb;
$header.= "MIME-Version: 1.0".$lb;
$header.= "Content-Type: multipart/mixed;".$lb;
$header.= " boundary=\"".$mime_boundary."\"".$lb;
$content = "This is a multi-part message in MIME format.".$lb.$lb;
$content.= "--".$mime_boundary.$lb;
$content.= "Content-Type: text/plain; charset=\"iso-8859-1\"".$lb;
$content.= "Content-Transfer-Encoding: 7bit".$lb.$lb;
}
$content.= $message.$lb;
if (is_array($files)) {
$content.= "--".$mime_boundary.$lb;
foreach($files as $filename => $filelocation) {
if(is_readable($filelocation) && is_file($filelocation)) {
$fileID = @fopen($filelocation, 'r');
$data = @fread($fileID, filesize($filelocation));
@fclose($fileID);
$data = chunk_split(base64_encode($data));
$content.= "Content-Disposition: attachment;".$lb;
$content.= 'Content-Type: ' . mime_content_type($filelocation) . ';';
$content.= " name=\"".$filename."\"".$lb;
$content.= "Content-Transfer-Encoding: base64".$lb.$lb;
$content.= $data.$lb;
$content.= "--".$mime_boundary.$lb;
}
}
}
if (mail($to, $subject, $content, $header)) return true;
return false;
}
[/PHP]
Thanx for your help!
Phil
Gabriel PREDA wrote:
> - Problem lies in "mime_content_type" it's not a good function even
> if you have magic.mime... not to mention if you do not have it.
> It's sort of undocumented function : "Returns the MIME content type for
>a
> file as determined by using information from the magic.mime file. Content
> types are returned in MIME format, like text/plain or
> application/octet-stream."
> But it says not what happens if it fails... or if the file has an
> unrecognizable mime. Try to use something else
>
> - You're using a custom line break although in PHP manual says:
>"You
> must use \r\n to separate headers, although some Unix mail transfer agents
> may work with just a single newline (\n)." Also note SMTP and MINE
>standards
> (RFC821/822 and ancestors) that require messages to have \r\n . :)
>
> - You do not have a charset definition in header: <start>
> Content-Type: text/plain; charset="iso-8859-1" </end>
>
> - If your mail server requires authentication you need to
> communicate directly via sockets.
>
> - A misssssconfigured SENDMAIL cand return false if it has some
> errors... even though the mail was sent...
> - Take note that RFC 821 prohibits lines longer than 998 bytes.
>
> - Speed-wize you should notice that mail() is extreeeeemly slower
> that "sockets"... if you send many emails in a loop PHP opens and closes a
> connection for every mai lyou send... compared with sockets when (if you
> write good code) you have only one connection opened and multiple mail
> sent...
>
>
> With all these problems... found in 5 minutes.. .what if I would have spent
> 30 mins or 1 day...
>
> It's amaizing that we are able to send mail... :))
>
> Gabriel PREDA
> www.amr.ro
>
>_______________________________________________
>talk mailing list
>talk at lists.nyphp.org
>http://lists.nyphp.org/mailman/listinfo/talk
>
>
>
--
---------------------------------------------------------------------------------
Phil Powell
Multimedia Programmer
BPX Technologies, Inc.
#: (703) 709-7218 x107
Fax: (703) 709-7219
More information about the talk
mailing list