I am trying to use a mailform to send an image from a webpage to the viewer. In most cases this works fine... 100% to web based email accounts and 50% to POP mail accounts on Mac and PC.
In the cases that fail, the mail arrives but the images is displayed simply as code in the body of the mail. I am unsure why sometimes it works and sometimes not (all testing the same image files).
Here are the 2 pages of code I am using. Does anyone see something wrong that I have missed?
Thanks for any suggesstions
BC
form.php (only sensitive server info has been altered):
<?
if(isset($_POST['Submit'])) {
$guestemail = $_POST['guestemail'];
if ($guestemail <> "") {
include("mail_attach.php");
$filename="/home/username/public_html/images/img.jpg";
$fh=fopen($filename,"r");
$data=fread($fh,filesize($filename));
mail_attach($guestemail,$mymail,$subject,"Here is the photo you requested.\n","img.jpg",$data,3,"application/octet-stream", "$headers");
?>
<p><font color="#0000CC"><i><b>Image Sent.</b></i></font></p>
<?
} else {
?>
<p><font color="#FF0000"><b><i>Could not send image.</i></b></font></p>
<?
}
}
?>
<form name="mailpic" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Enter your E-mail:
<input type="text" name="guestemail" size="20">
<input type="submit" name="Submit" value="Send">
</p>
</form>
mail_attach.php:
<?
function mail_attach($to,$from,$subject,$body,$fname,$data,
$priority=3,$type="Application/Octet-Stream")
{
$mime_boundary = "<<<:" . md5(uniqid(mt_rand(), 1));
$fdata = chunk_split(base64_encode($data));
$headers .= "From: $from\r\n";
$headers .= "To: $to\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "X-Priority: $priority\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"" . $mime_boundary . "\"\r\n";
$mime = "This is a multi-part message in MIME format.\r\n";
$mime .= "\r\n";
$mime .= "--" . $mime_boundary . "\r\n";
$mime .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$mime .= "Content-Transfer-Encoding: 7bit\r\n";
$mime .= "\r\n";
$mime .= $body . "\r\n";
$mime .= "--" . $mime_boundary . "\r\n";
$mime .= "Content-Disposition: attachment;\r\n";
$mime .= "Content-Type: $type; name=\"$fname\"\r\n";
$mime .= "Content-Transfer-Encoding: base64\r\n\r\n";
$mime .= $fdata . "\r\n";
$mime .= "--" . $mime_boundary . "\r\n";
mail($to, $subject, $mime, $headers);
}
?>