Monday 15 August 2011

php mail function with attachments -



php mail function with attachments -

i'm trying create form sends me mail.

this html file content.

<form method="post" action="email.php" enctype="multipart/form-data"> <input type="text" id="contact_name" class="form-control" placeholder="name" name="user_name"/> <input type="text" id="contact_email" class="form-control" placeholder="email address" name="user_mail" <textarea id="contact_message" class="form-control" rows="7" placeholder="write message" name="user_message"></textarea> <input name="attachment" type="file"> <input type="submit" value="send mail" class="btn btn-primary pull-right"/> </form>

and php file.

<?php if(isset($_post) && !empty($_post)) { if(!empty($_files['attachment']['name'])) { $file_name = $files['attachment']['name']; $temp_name = $files['attachment']['tmp_name']; $file_type = $files['attachment']['type']; $base = basename($file_name); $extension = substr($base, strlen($base)-4, strlen($base)); //only these file types allowed $allowed_extensions = array(".doc", "docx", ".pdf", ".zip", ".png"); //check file type allowed if(in_array($extension,$allowed_extensions)) { //mail essentials $from = $_post['user_mail']; $to = "eugenepi1025@gmail.com"; $subject = $_post['user_name']; $message = $_post['user_message']; //things u need $file = $temp_name; $content = chunk_split(base64_encode(file_get_contents($file))); $uid = md5(uniqid(time())); //unique identifier //standard mail service headers $header = "from: ".$from."\r\n"; $header .= "reply-to: ".$replyto. "\r\n"; $header .= "mime-version: 1.0\r\n"; //declare multiple kinds of email (plain text + attch) $header .="content-type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; $header .="this multi-part message in mime format.\r\n"; //plain txt part $header .= "--".$uid."\r\n"; $header .= "content-type:text/plain; charset=iso-8859-1\r\n"; $header .= "content-transfer-encoding: 7bit\r\n\r\n"; $header .= $message. "\r\n\r\n"; //attch part $header .= "--".$uid."\r\n"; $header .= "content-type: ".$file_type."; name=\"".$file_name."\"\r\n"; $header .= "content-transfer-encoding: base64\r\n"; $header .= "content-disposition: attachment; filename=\"".$file_name."\"\r\n\r\n"; $header .= $content."\r\n\r\n"; //chucked 64 encoded attch //sending mail service - message not here, in header in multi part if(mail($to, $subject, "", $header)) { echo "success"; }else { echo "fail"; } }else { echo "file type not allowed"; } //echo html file }else { echo "no file posted"; } } ?>

the thing file type not allowed message keeps coming. uploaded .png file, don't know problem.

could please help me this? i've been hanging on hours :(

actualy have

$file_name = $files['attachment']['name']; $temp_name = $files['attachment']['tmp_name']; $file_type = $files['attachment']['type'];

it should

$file_name = $_files['attachment']['name']; $temp_name = $_files['attachment']['tmp_name']; $file_type = $_files['attachment']['type'];

and works :))

php email-attachments

No comments:

Post a Comment