Reputation: 107
I have a problem with sending mail with attachment using php. The mail is sent with broken attachment, for example when I try to send example.doc
with size 2 Mb
, in the mail I receive noname
with size 1Kb
I use two files php.
The 1st one mailClass.php
contain :
<?php
class mail
{
function emailWithAttach($fromAdress,$toAdress,$mailSubject,$mailMessageHead,
$mailMessageMain,$mailMessageSign,$filePath,$fileName)
{
$fileatt_name = $fileName;
$fileatt = $filePath.$fileName;
$fileatt_type = "application/doc";
$email_from = $fromaddress;
$email_subject = $mailSubject;
$email_message = $mailMessageHead. "<br>";
$email_message .= $mailMessageMain. "<br>";
$email_message .= $mailMessageSign;
$email_to = $toAdress;
$headers = "From: " .$email_from;
$file = fopen ($fileatt."rb");
$data = fread ($file, filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0 \n".
"Content-Type : multipart/mixed:\n".
"boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multip-part message in MIME format. \n\n".
"--{$mime_boundary}\n".
"Content-Type : text/html; charset=\"iso-8859-1\"\n".
"Content-Transfer-Encoding: 7bit\n\n".
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n".
"Content-Type: {$fileatt_type}:\n".
"name=\"{$fileatt_name}\"\n".
"Content-Transfer-Encoding: base64 \n\n".
$data .= "\n\n".
"--{$mime_boundary}--\n";
if(@mail($email_to,$email_subject,$email_message,$headers))
{
return true;
}
}
}
?>
The second file index.php
contain :
<?php
include "mailClass.php";
$testEmail = new mail;
$from = "[email protected]";
$sendTo = "[email protected]";
$subject = "email with attachment";
$bodyHead = "welcome to the attachment email test";
$bodyMain = "hello iteb";
$bodyEnd = "Thank you";
$filePath = "";
$fileName = "example.doc";
if ($testEmail->emailWithAttach($from,$sendTo,$subject,$bodyHead,$bodyMain,$bodyEnd,$filePath,$fileName))
{
echo "Email Send successful!!";
}
else
{
echo "Email Send Failed";
}
?>
Upvotes: 0
Views: 402
Reputation: 433
Use phpmailer to send a mail: Get it here
It also has the advantage that you can send out many mails without opening and closing the connection each time as php's mail() function does.
Upvotes: 1