0

I'm trying to make the file input in the contact form to simply attach the file in the email as an attachment when sent using phpmailer. The code below doesn't show any errors but it just stays on the blank php page and doesn't send the email. Thanks in advance.

<form action="send-mail-tutor.php"  method="post" class="form-inline contact-form" entype="multipart/form-data">
  <div class="form-group col-sm-10">
    <label for="inputName1">Name</label>
    <input type="text" class="form-control" name="name" id="name" placeholder="Enter first and last name">
  </div>

  <div class="form-group col-sm-6">
    <label for="inputEmail1">Email address</label>
    <input type="email" class="form-control" name="email" id="email" placeholder="Enter email">
  </div>

  <div class="form-group col-sm-6">
    <label for="inputPhone1">Phone Number</label>
    <input type="tel" class="form-control" name="phone" id="phone-number"  placeholder="Enter phone number" maxlength="10">
  </div>

  <div class="form-group">
    <label for="resume">Resume</label>
    <input type="file" id="resume" name="resume">
  </div> <br/>

  <button type="submit" class="btn btn-default">Submit</button>
</form>



  <?php

if(isset($_POST['submit'])) {

$emailAddress = "ronniehung95@gmail.com";

// require_once "class.phpmailer.php";

$msg = 'Name: '.$_POST['name'].'<br />  Email: '.$_POST['email'].'<br /> Phone: '.$_POST['phone'];

if (array_key_exists('resume', $_FILES)) {
// first handle the upload
// don't trust provided filename - same goes for MIME types
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['resume']['name']));
if (move_uploaded_file($_FILES['resume']['tmp_name'], $uploadfile)) {
// upload handled successfully
// now create a message
// This should be somewhere in your include_path
require_once 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsMail();

$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->addAddress($emailAddress);
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->Subject = "New Tutor Applicant";
$mail->msgHTML($msg);
// attach the uploaded file
$mail->addAttachment($uploadfile, 'resume');
$mail->send();

} echo "<script> window.location=../message-sent.php; </script>";
} 
}
onodera
  • 9
  • 2
  • You should add error handling to your file-upload and move section to see if it all completed succesfully. – jeroen Feb 17 '15 at 07:05
  • What kind of error handling? @jeroen Also an update: I added a name attribute to the send button in the html and it sends the form but the file still isn't attached. – onodera Feb 17 '15 at 07:46
  • Check here for possible errors and check the return value when you move the uploaded file: http://php.net/manual/en/features.file-upload.post-method.php – jeroen Feb 17 '15 at 07:48
  • I suggest you try using [the example code provided with PHPMailer that does exactly this](https://github.com/PHPMailer/PHPMailer/blob/master/examples/send_file_upload.phps), and isn't based on a faulty, obsolete example. Also update your PHPMailer while you're there. – Synchro Feb 17 '15 at 08:35
  • So I've tried using the example from PHPMailer and it's back to the old problem if you go back to the top, which basically nothing helps and it stays on the blank page. Am I doing something wrong? I've updated the code I currently have above. – onodera Feb 18 '15 at 05:01
  • Please use this $mail->addAttachment($uploadfile,'resume.pdf'); – Sharma Vikram Feb 18 '15 at 05:10
  • Please define what type of file you have to sent. – Sharma Vikram Feb 18 '15 at 05:12
  • @SharmaVikram It's a file input field, so the user inserts the file. But it should be a word doc or pdf. – onodera Feb 18 '15 at 06:12
  • See link hope your help http://stackoverflow.com/questions/24455076/send-attachment-file-using-phpmailer/36370941#36370941 – Pankaj Upadhyay Apr 02 '16 at 08:20

0 Answers0