Email with attachment function
| From: | Mark Cubitt | Date: | Thu, 17 Jun 2004 07:38:39 +0000 |
| Subject: | Email with attachment function | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-188459@lists.php.net to get a copy of this message | ||
Hello,
I'm trying to write a function that sends an email with an attachment using
sendmail,
But I can't seem to get it working, the code I currently have is below,
Any ideas would be much appreciated.
I'm probably doing something stupid as the output is what its meant to be
except it isn't divided up (i.e. attachment is text)
Regards
Mark Cubitt
<CODE>
function send_mail($to, $from, $subject, $body, $attachment = '',
$attachmentDir = '')
{
$path_to_sendmail = "/usr/sbin/sendmail";
$fp = popen("$path_to_sendmail -t", "w");
if ($attachment != '')
{
$filename = $attachmentDir.$attachment;
$filePointer = fopen($filename, "rb");
$fileContent = fread($filePointer, filesize($filename));
$fileContent = chunk_split(base64_encode($fileContent));
fclose($filePointer);
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$num = fputs($fp, "To: $to\n");
$num += fputs($fp, "From: $from\n");
$num += fputs($fp, "Subject: $subject\n");
$num += fputs($fp, "MIME-Version: 1.0\n");
$num += fputs($fp, "Content-type: multipart/mixed;\n");
$num += fputs($fp, "
boundry=\"{".$mime_boundary."}\"\n\n");
$num += fputs($fp, 'This is a multi-part message in MIME
format'."\n");
$num += fputs($fp, "\n");
$num += fputs($fp, "--{".$mime_boundary."}\n");
$num += fputs($fp, "Content-type: text/html;
charset=iso-8859-1\n");
// $num += fputs($fp, "Content-Transfer-Encoding: 7bit\n");
$num += fputs($fp, "\n");
// $num += fputs($fp, "Subject: $subject\n");
$num += fputs($fp, "$body");
$num += fputs($fp, "--{".$mime_boundary."}\n");
$num += fputs($fp, "--{".$mime_boundary."}\n");
$num += fputs($fp, "Content-type: application/zip;\n");
$num += fputs($fp, " name=\"{".$attachment."}\"\n");
$num += fputs($fp, "Content-Transfer-Encoding: base64\n");
// $num += fputs($fp, "Content-Disposition: attachment;\n");
// $num += fputs($fp, "
filename=\"{".$attachment."\"}\n");
$num += fputs($fp, "\n");
$num += fputs($fp, $fileContent);
$num += fputs($fp, "\n");
$num += fputs($fp, "--{".$mime_boundary."}--\n");
}
else
{
$num = fputs($fp, "To: $to\n");
$num += fputs($fp, "From: $from\n");
$num += fputs($fp, "MIME-Version: 1.0\n");
$num += fputs($fp, "Content-type: text/html;
charset=iso-8859-1\n");
$num += fputs($fp, "Subject: $subject\n\n");
$num += fputs($fp, "$body");
}
pclose($fp); if ($num>0) { return 1; } else { return 0; }
} // end function send_mail()
</CODE>