Send mail with attachment – PHP

<?php

class sendMail{

function send($mailto, $subject,$message , $filename, $path, $from_mail, $from_name, $replyto ) {

if(!empty($filename)) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, “r”);
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$name = basename($file);
}

$uid = md5(uniqid(time()));

$header = “From: “.$from_name.” <“.$from_mail.”>rn”;
$header .= “Reply-To: “.$replyto.”rn”;
$header .= “MIME-Version: 1.0rn”;
$header .= “Content-Type: multipart/mixed; boundary=””.$uid.””rnrn”;
$header .= “This is a multi-part message in MIME format.rn”;
$header .= “–“.$uid.”rn”;
$header .= “Content-type: text/html; charset=iso-8859-1n”;
$header .= “Content-Transfer-Encoding: 7bitrnrn”;
$header .= $message.”rnrn”;
$header .= “–“.$uid.”rn”;

if(!empty($filename)) {
$header .= “Content-Type: application/octet-stream; name=””.$filename.””rn”; // use different content types here
$header .= “Content-Transfer-Encoding: base64rn”;
$header .= “Content-Disposition: attachment; filename=””.$filename.””rnrn”;
$header .= $content.”rnrn”;
$header .= “–“.$uid.”–“;
}

if (mail($mailto, $subject, “”, $header)) {
return 1;
} else {
return 0;
}
}

}

 

$mail = new sendMail();

$mail->send(“info@phpcodez.com”,”Test”,”Test” , “image1.jpg”,”../images/”,”newsletter@phpcodez.com”,”PHPCodez”,”newsletter@phpcodez.com”);

?>

Leave a Reply

Your email address will not be published. Required fields are marked *