Top Menu
Mail Function with Attachments in php

<?php
function mailWithAttachments($fileName, $filepath, $mailto, $fromMail, $fromName, $replyTo, $subject, $message) {
$file = $filepath.$fileName;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));//for unique number generate
$name = basename($file);
$header = "From: ".$from_name." <".$fromMail.">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.rn";
$header .= "--".$uid."rn";
$header .= "Content-type:text/plain; charset=iso-8859-1rn";
$header .= "Content-Transfer-Encoding: 7bitrnrn";
$header .= $message."rnrn";
$header .= "--".$uid."rn";
$header .= "Content-Type: application/octet-stream; name="".$fileName.""rn"; // you can also 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)) {
echo "mail send ... OK"; //
} else {
echo "mail send ... ERROR!";
}
}
?>

How can use this function.

$my_file = "demo.zip";
$my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
$my_name = "jafar khan";
$my_mail = "jafarkhanphp@gmail.com";
$my_replyto = "jafarkhanphp@gmail.com";
$my_subject = "Mail with attachment.";
$my_message = "Hallo this is testing";
mail_attachment($my_file, $my_path, "jafar_14jan@yahoo.com", $my_mail, $my_name, $my_replyto, $my_subject, $my_message); 

About The Author

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Close