Help With Attaching Image To Php Mailform Please

liunx

Guest
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. <br /><br />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). <br /><br />Here are the 2 pages of code I am using. Does anyone see something wrong that I have missed? <br /><br />Thanks for any suggesstions <br />BC <br /><br /><br /><br /><b>form.php </b>(only sensitive server info has been altered): <br /><br /><? <br />if(isset($_POST['Submit'])) { <br />$guestemail = $_POST['guestemail']; <br />if ($guestemail <> "") { <br />include("mail_attach.php"); <br />$filename="/home/username/public_html/images/img.jpg"; <br />$fh=fopen($filename,"r"); <br />$data=fread($fh,filesize($filename)); <br />mail_attach($guestemail,$mymail,$subject,"Here is the photo you requested.\n","img.jpg",$data,3,"application/octet-stream", "$headers"); <br />?> <br /><br /><p><font color="#0000CC"><i><b>Image Sent.</b></i></font></p> <br /><? <br />} else { <br />?> <br /><br /><p><font color="#FF0000"><b><i>Could not send image.</i></b></font></p> <br /><? <br />} <br />} <br />?> <br /><form name="mailpic" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <br /><p>Enter your E-mail: <br /><input type="text" name="guestemail" size="20"> <br /><input type="submit" name="Submit" value="Send"> <br /></p> <br /></form> <br /><br /><br /><b>mail_attach.php: </b><br /><br /><? <br />function mail_attach($to,$from,$subject,$body,$fname,$data,<br />$priority=3,$type="Application/Octet-Stream") <br />{ <br /><br />$mime_boundary = "<<<:" . md5(uniqid(mt_rand(), 1)); <br />$fdata = chunk_split(base64_encode($data)); <br /><br />$headers .= "From: $from\r\n"; <br />$headers .= "To: $to\r\n"; <br />$headers .= "MIME-Version: 1.0\r\n"; <br />$headers .= "X-Priority: $priority\r\n"; <br />$headers .= "Content-Type: multipart/mixed;\r\n"; <br />$headers .= " boundary=\"" . $mime_boundary . "\"\r\n"; <br />$mime = "This is a multi-part message in MIME format.\r\n"; <br />$mime .= "\r\n"; <br />$mime .= "--" . $mime_boundary . "\r\n"; <br />$mime .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; <br />$mime .= "Content-Transfer-Encoding: 7bit\r\n"; <br />$mime .= "\r\n"; <br />$mime .= $body . "\r\n"; <br />$mime .= "--" . $mime_boundary . "\r\n"; <br />$mime .= "Content-Disposition: attachment;\r\n"; <br />$mime .= "Content-Type: $type; name=\"$fname\"\r\n"; <br />$mime .= "Content-Transfer-Encoding: base64\r\n\r\n"; <br />$mime .= $fdata . "\r\n"; <br />$mime .= "--" . $mime_boundary . "\r\n"; <br />mail($to, $subject, $mime, $headers); <br />} <br /><br /><br />?><!--content-->
I would strongly suggest using a pre-made script available at HotScripts.com or using the one I posted in the Code Central part of the forum. It's pinned as 'The Ultimate FormMail Script'<!--content-->
 
Back
Top