php - Add attachment through PHPMailer -


i have following piece of code of phpmailer. problem is, file uploads server attachment not sent in mail. attachment code seems right best of knowledge. please review code , let me know have gone wrong.

form

<form name="contactform" method="post" action="send1.php" enctype="multipart/form-data"> <table width="100%" border="0"> <tr>  <td id="ta">  <label for="title">title *</label>  </td>  <td id="ta">  <select name="title">  <option value="0">title</option>  <option value="1">mr.</option>  <option value="2">ms.</option>  <option value="3">mrs.</option>  </select></td></tr><tr><td id="ta">   <label for="first_name">first name *</label>  </td>  <td id="ta">   <input  type="text" name="first_name" maxlength="50" size="30" required="required">  </td> </tr> <tr>  <td id="ta">   <label for="last_name">last name *</label>  </td>  <td  id="ta">   <input  type="text" name="last_name" maxlength="50" size="30" required="required">  </td> </tr> <tr>  <td id="ta">   <label for="email">email address *</label>  </td>  <td  id="ta">   <input  type="text" name="email" maxlength="80" size="30" required="required">  </td> </tr> <tr>  <td id="ta">   <label for="telephone">telephone number *</label>  </td>  <td  id="ta">   <input  type="text" name="telephone" maxlength="30" size="30" required="required">  </td> </tr> <tr>  <td id="ta">   <label for="comments">details</label>  </td>  <td  id="ta">   <textarea  name="comments" maxlength="100000" cols="25" rows="6"></textarea>  </td> </tr> <tr> <td id="ta">     <label for="file">or upload file (only word, excel or pdf)</label> </td> <td  id="ta"> <input type="file" name="file"> </td> </tr> <tr>  <td colspan="2" style="text-align:center" id="ta">   <input type="submit" value="submit">  </td> </tr> </table> </form> 

send1.php

<?php  require('phpmailer/class.phpmailer.php');  if(isset($_post['email'])) {      // edit 2 lines below required     //$email_to = "hidden";     //$email_subject = "request portfolio check ".$first_name." ".$last_name;      $title = array('title', 'mr.', 'ms.', 'mrs.');     $selected_key = $_post['title'];     $selected_val = $title[$_post['title']];       $first_name = $_post['first_name']; // required     $last_name = $_post['last_name']; // required     $email_from = $_post['email']; // required     $telephone = $_post['telephone']; // not required     $comments = $_post['comments']; // required    if(($selected_key==0))     echo "<script> alert('please enter title')</script>";     function clean_string($string) {       $bad = array("content-type","bcc:","to:","cc:","href");       return str_replace($bad,"",$string);     }      $email_message = "";     $email_message .="title: ".$selected_val."\n";     $email_message .= "first name: ".clean_string($first_name)."\n";     $email_message .= "last name: ".clean_string($last_name)."\n";     $email_message .= "email: ".clean_string($email_from)."\n";     $email_message .= "telephone: ".clean_string($telephone)."\n";     $email_message .= "comments: ".clean_string($comments)."\n";      $allowedexts = array("doc", "docx", "xls", "xlsx", "pdf"); $temp = explode(".", $_files["file"]["name"]); $extension = end($temp); if ((($_files["file"]["type"] == "application/pdf") || ($_files["file"]["type"] == "application/msword") || ($_files["file"]["type"] == "application/excel") || ($_files["file"]["type"] == "application/vnd.ms-excel") || ($_files["file"]["type"] == "application/x-excel") || ($_files["file"]["type"] == "application/x-msexcel") || ($_files["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") || ($_files["file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))  && in_array($extension, $allowedexts))   {   if ($_files["file"]["error"] > 0)     {     echo "<script>alert('error: " . $_files["file"]["error"] ."')</script>";     }   else     {         $d='upload/';         $de=$d . basename($_files['file']['name']);     move_uploaded_file($_files["file"]["tmp_name"], $de); $filename = $_files['file']['name'];     $filepath = $_files['file']['tmp_name'];      //add if file upload      }   } else   {   echo "<script>alert('invalid file')</script>";   }  // create email headers $headers = 'from: '.$email_from."\r\n". 'reply-to: '.$email_from."\r\n" . 'x-mailer: php/' . phpversion(); //create new phpmailer instance $mail = new phpmailer(); //tell phpmailer use smtp $mail->issmtp(); //enable smtp debugging // 0 = off (for production use) // 1 = client messages // 2 = client , server messages $mail->smtpdebug  = 0; //ask html-friendly debug output $mail->debugoutput = 'html'; //set hostname of mail server $mail->host       = "hidden"; //set smtp port number - 25, 465 or 587 $mail->port       = 25; //whether use smtp authentication $mail->smtpauth   = true; //username use smtp authentication $mail->username   = "hidden"; //password use smtp authentication $mail->password   = "hidden"; //set message sent $mail->setfrom($email_from, $first_name.' '.$last_name); //set alternative reply-to address //$mail->addreplyto('replyto@example.com','first last'); //set message sent $mail->addaddress('hidden', 'hidden'); //set subject line $mail->subject = 'request profile check up'; //read html message body external file, convert referenced images embedded, convert html basic plain-text alternative body $mail->msghtml($email_message); //replace plain text body 1 created manually $mail->altbody = 'this plain-text message body'; //attach image file //$mail->addattachment($file); $mail->addattachment($_files['file']['tmp_name'], $_files['file']['name']); //send message, check errors if(!$mail->send()) {   echo "<script>alert('mailer error: " . $mail->errorinfo."')</script>"; } else {   echo "<script>alert('your request has been submitted. contact soon.')</script>";   header('location: main.php'); } } ?> 

edit mail sends details too. attachment won't send.

edit 2: solved changed $mail->msghtml $mail->body , worked!

change

$mail->msghtml(); 

to

$mail->body; 

source: here


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -