I am having difficulties attaching an image to an email. The attachment is there but its empty. Anyone know why?
<?php
error_reporting(0);
$w = (int)$_POST['width'];
$h = (int)$_POST['height'];
$img = imagecreatetruecolor($w, $h);
imagefill($img, 0, 0, 0xFFFFFF);
$rows = 0;
$cols = 0;
for($rows = 0; $rows < $h; $rows++){
$c_row = explode(",", $_POST['px' . $rows]);
for($cols = 0; $cols < $w; $cols++){
$value = $c_row[$cols];
if($value != ""){
$hex = $value;
while(strlen($hex) < 6){
$hex = "0" . $hex;
}
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
$test = imagecolorallocate($img, $r, $g, $b);
imagesetpixel($img, $cols, $rows, $test);
}
}
}
$day = date("md_hm") ;
$imageName = "test".$day.".jpg";
$dest = "photos/".$imageName;
imagejpeg($img, $dest, 40);
$siteaddress ="http://www.com/";
$sitename = "Sitename";
$fileatt = "$siteaddress/photos/"; // Path to the file
$fileatt_type = "image/jpeg"; // File Type
$email_from = ""; // Who the email is from
$email_subject = "Info Request"; // The Subject of the email
$email_txt = "aasdfasdf"; // Message that the email has in it
$email_to = ""; // Who the email is too
$headers = "From: ".$email_from;
$completeimage = "$fileatt$imageName";
$file = fopen($completeimage,'rb');
$data = fread($file,filesize($completeimage));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "
MIME-Version: 1.0
" .
"Content-Type: multipart/mixed;
" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.
" .
"--{$mime_boundary}
" .
"Content-Type:text/html; charset=\"iso-8859-1\"
" .
"Content-Transfer-Encoding: 7bit
" .
$email_txt . "
";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}
" .
"Content-Type: {$fileatt_type};
" .
" name=\"{$imageName}\"
" .
"Content-Disposition: attachment;
" .
" filename=\"{$imageName}\"
" .
"Content-Transfer-Encoding: base64
" .
$data . "
" .
"--{$mime_boundary}--
";
$ok = mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo "<font face=verdana size=2>The file was successfully sent!</font><br><br>";
echo "<img src='$completeimage'/>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>