I am trying to send an image attachment by mail using the code below.
I have a problam in emailClass.php file in line 22 fread(...)
. I know it because if I echo something before that line, it is echoed successfully, and if i echo something after this line it is not echoed which indicates to me that something is wrong with the fread
statement.
The fileatt
is ok: I checked it and the full path is printed. I tried with a PNG file and with a 500KB JPG. Then I tried with some text files, but still nothing.
How can I correct this problem?
index.php:
<?php
include "emailClass.php";
$testEmail = new email();
$from = 'someone@gmail.com';
$senfTo = 'someone@gmail.com';
$subject = 'email with attachment';
$bodyHead = 'welcome';
$bodyMain = 'bodyMain writings';
$bodyEnd = 'Thank you';
$filePath = '...';
$fileName = 'check.txt';
if($testEmail->emailWithAttach($from,$sendTo,$subject,$bodyHead,$bodyMain,$bodyEnd,$filePath,$fileName))
{
echo "Email Sent Successfuly";
}
else
{
echo "Failes sending";
}
?>
emailClass.php:
<?php
class email
{
function emailWithAttach($fromaddress,$toAddress,$mailSubject,$mailMessageHead,$mailMessageMain,$mailMessageSign,$filePath,$fileName)
{
$fileatt_name = $fileName;
$fileatt = $filePath.$fileName;
$fileatt_type = "application/octet-stream";
$email_from = $fromAddress;
$email_subject = $mailSubject;
$email_message = $mailMessageHead."<br>";
$email_message .= $mailMessageMain."<br>";
$email_message .= $mailMessageSign;
$email_to = $toAddress;
$headers = "From: ".$email_from;
$file = fopen($fileatt,"rb");
echo $fileatt; //prints ok the correct pathname!!
$data = fread($file,$filesize($fileatt));
echo "check"; //not printing which means something's wrong with line 22 the fread..
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_message .= "
";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}
" .
"Content-Type: {$fileatt_type};
" .
" name=\"{$fileatt_name}\"
" .
"Content-Transfer-Encoding: base64
" .
$data .= "
" .
"--{$mime_boundary}--
";
if (@mail($email_to, $email_subject, $email_message, $headers))
{
return true;
}
}
}
?>