File attachment with PHPMailer

Eli Nathan picture Eli Nathan · Mar 14, 2016 · Viewed 57k times · Source

I have an HTML form with the option to upload a file.
I would like then to send that file as an attachment to the email address along with the rest of the form data.
I'm using PHP Mailer and I get the form data to send: such as name, telephone number, etc.

I can't get the image to send along with it. I've provided the code I have so far

<?php
   if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size = $_FILES['image']['size'];
      $file_tmp = $_FILES['image']['tmp_name'];
      $file_type = $_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
      
      $expensions= array("jpeg","jpg","png","pdf");
      
      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a PDF, JPEG or PNG file.";
      }
      
      if($file_size > 2097152) {
         $errors[]='File size must be excately 2 MB';
      }
      
      if(empty($errors)==true) {
         move_uploaded_file($file_tmp,"uploads/".$file_name); //The folder where you would like your file to be saved
         echo "Success";
      }else{
         print_r($errors);
      }
   }

// PHPMailer script below

$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phone'] ;
$message = $_REQUEST['message'] ;
require("phpmailer/PHPMailerAutoload.php");

$mail = new PHPMailer();

$mail->IsSMTP();

$mail->Host = "smtp.gmail.com";

$mail->SMTPAuth = true; 

$mail->Username = "[email protected]"; // SMTP username
$mail->Password = "hidden"; // SMTP password
$mail->addAttachment("uploads/".$file_name);
$mail->From = $email;
$mail->SMTPSecure = 'tls'; 
$mail->Port = 587; //SMTP port
$mail->addAddress("[email protected]", "your name");
$mail->Subject = "You have an email from a website visitor!";
$mail->Body ="
Name: $name<br>
Email: $email<br>
Telephone: $phone<br><br><br>
Comments: $message";
$mail->AltBody = $message;

if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

echo "<script>alert('Message has been sent')</script>";
?>

EDIT: [SOLVED] I have updated the code in the snippets to the working code that allows me to attach a file with to an email with PHPMailer.
I used This Tutorial to help upload the file to the server before executing the PHPMailer script.

Answer

drew010 picture drew010 · Mar 14, 2016

When you call

move_uploaded_file($file_tmp,"uploads/".$file_name);

This creates a file in the uploads/ directory with the name of the file as it was named on the uploader's computer.

Then you used sample code to add the attachment to phpMailer so you're basically attempting to attach non-existent files.

These two lines:

$mail->addAttachment('uploads/file.tar.gz');   // I took this from the phpmailer example on github but I'm not sure if I have it right.      
$mail->addAttachment('uploads/image.jpg', 'new.jpg');

should be changed to:

$mail->addAttachment("uploads/".$file_name);

Also note, it isn't necessary to call move_uploaded_file if you don't want to save the attachment after its uploaded and emailed. If that's the case just call AddAttachment with $_FILES['image']['tmp_name'] as the file argument.

Also, in your HTML form, you have

<input id="file" name="file" type="file" />

but refer to the input as image in the code. You should change the name of that input from file to image.

To only attach the image and not save it take out the move_uploaded_file code and add:

$file_tmp  = $_FILES['image']['tmp_name'];
$file_name = $_FILES['image']['name'];
//...
$mail->AddAttachment($file_tmp, $file_name);