Send mail with attachment in php
by satheeshkumar[ Edit ] 2012-06-27 11:22:18
Following code used to attach a word document with the mail content in php,
$fileatt = "file path";
$fileatttype = "doc/docx";
$fileattname = "filename";
$email_from = "from mail id";
$email_subject = "send mail with attachment";
$email_message ="your message here";
$email_to ="to mail id";
$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 multi-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: {$fileatttype};\n" ." name=\"{$fileattname}\"\n" ."Content-Disposition: attachment;\n" ." filename=\"{$fileattname}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data . "\n\n" ."-{$mime_boundary}-\n";
$mail=@mail($email_to, $email_subject, $email_message, $headers);