Sending jpeg attachments with mail
Posted: Sat Dec 28, 2002 8:48 pm
I have a form which sends HTML mail, optionally with jpeg images as attachments. Without the images it works fine. when images are sent, they seem to be attached fine but will not open as if they are corrupt or something.
The form (with enctype="multipart/form-data" of course) sends $fname, $lname, $testimony and optionally the uploaded files $before and $after to the following script. Note, this is only the part that handles the mail if there is an image sent through the form:
Please let me know what I am doing wrong! This is driving me up a wall. 
The form (with enctype="multipart/form-data" of course) sends $fname, $lname, $testimony and optionally the uploaded files $before and $after to the following script. Note, this is only the part that handles the mail if there is an image sent through the form:
Code: Select all
// determine the file types of the images being uploaded
$imgType = "";
if ($before_size) $bType = $before_type;
if ($after_size) $aType = $after_type;
if (!strpos($bType, "jpeg") || !strpos($aType, "jpeg")) echo "<p>One of the images you tried to upload were not jpeg! Please click the "back" button and try another file or files</p>";
else {
// prepare input for mailing
$mail_result = "<p>";
$fname = trim($fname);
$lname = trim($lname);
$testimony = nl2br(htmlentities(stripslashes(trim($testimony))));
if (isset($WINDIR)) {
$before = str_replace("\","", $before);
$after = str_replace("\","", $after);
}
// get image file information and make sure their the right format
// and, of course, don't mail anything if there's a problem
if ($before_size) {
$bFile = fopen($before, "r");
$beforeImg = fread($bFile, filesize($before));
$beforeImg = chunk_split(base64_encode($beforeImg));
}
if ($after_size) {
$aFile = fopen($after, "r");
$afterImg = fread($aFile, filesize($after));
$afterImg = chunk_split(base64_encode($afterImg));
}
// construct multi-part header
$mail_boundary = md5(uniqid(time()));
$headers = "Mime-Version: 1.0\r\nContent-Type: multipart/mixed;boundary="$mail_boundary"";
$headers .= "\r\n\r\nThis is a multi-part message in MIME format.\r\n\r\n";
// format all input for e-mailing
$headers .= "From: "Herbagirlz Testimonials page"<testimonials@someurl.com>\n";
$subject = "$name has sent a testimonial.";
$mailto = "webmaster@someurl.com";
// html portion of the e-mail
$body = "--$mail_boundary\n";
$body .= "Content-Type: text/html\n";
$body .= "Content-transfer-encoding: 8bit\r\n\r\n";
$body .= "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml">\n<head>\n<title>Catalog Request</title>\n<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />\n<meta name="Author" content="Ben Lawson, EightSeventeen Development" /><style type="text/css">\n<!--\nbody, td {font: 12px Verdana, Arial, Helvetica, sans-serif; color: #000000}\n-->\n</style>\n</head>";
$body .= "<body bgcolor="#FFFFFF" text="#000000" link="#0000CC" vlink="#0000CC" alink="#0000CC">\n";
$body .= "<p>My name is $fname $lname and I have the following testimonial to share:</p>\n";
$body .= "<p>$testimony</p>\n\n";
$body .= "\n</body>\n</html>\r\n\r\n";
// attachements portion of the e-mail
if ($before_size) {
$body .= "--$mail_boundary\n";
$body .= "Content-Type: $imgType; name=before.jpg\n";
$body .= "Content-transfer-encoding: base64\r\n\r\n";
$body .= $before . "\r\n\r\n";
}
if ($after_size) {
$body .= "--$mail_boundary\n";
$body .= "Content-Type: $imgType; name=after.jpg\n";
$body .= "Content-transfer-encoding: base64\r\n\r\n";
$body .= $after . "\r\n\r\n";
}
$body .= "--$mail_boundary--";
// send mail message
if ($sendSuccess = (mail($mailto, $subject, $body, $headers))) $mail_result = "<p>Your testimony and image(s) has been sent. Thank you!</p>";
else $mail_result = "<p><b>There was an error in submitting your request. Please click your "back" or "refresh" button and try submitting again.</b></p>";
//end of mail form
echo $mail_result;
}