I need to send one mail using php code. The mail contails HTML Part, Text , and two attachments (xls) file. How do i set headers and message body . For me Only one is working at a time either mail body or attachment. Please help
You can try this function, i found here , you can get some other code also if you google
<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$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()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">
";
$header .= "Reply-To: ".$replyto."
";
$header .= "MIME-Version: 1.0
";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"
";
$header .= "This is a multi-part message in MIME format.
";
$header .= "--".$uid."
";
$header .= "Content-type:text/plain; charset=iso-8859-1
";
$header .= "Content-Transfer-Encoding: 7bit
";
$header .= $message."
";
$header .= "--".$uid."
";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"
"; // use different content types here
$header .= "Content-Transfer-Encoding: base64
";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"
";
$header .= $content."
";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}
?>
And the Usage
$my_file = "somefile.zip";
$my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
$my_name = "Olaf Lederer";
$my_mail = "my@mail.com";
$my_replyto = "my_reply_to@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,
do you like this script? I hope it will help.
gr. Olaf";
Function Call:
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);