0

I'm loosing my mind since yesterday, I'm pretty sure I'm close but...

Well, I have a HTML form with an input type file and I would like get the file submitted attached with the email sent.

Here is my HTML (simplified):

<form enctype="multipart/form-data" id="contact-form-cv" name="contact-form-cv" method="POST" data-name="Contact Form CV">

<div class="form-group">

    <div class="controls">

        <!-- FILE -->
        <input type="hidden" name="MAX_FILE_SIZE" value="300000">
        <input type="file" name="cv-file" id="file" class="input-file form-control special-form my-file">
          <label for="file" class="btn btn-tertiary js-labelFile">

            <span class="js-fileName"><i class="fa fa-upload"></i>&nbsp; Attach CV*</span>
          </label>

        <!-- Button -->
        <button id="cv-valid-form" type="submit" class="btn btn-lg submit">Submit</button>

    </div>

</div>

JS

I have a JS file used to display alert messages when the user is filling the form :

$("#contact-form-cv [type='submit']").click(function(e) {
e.preventDefault();

// Get input field values of the contact form
var cvuser_file       = $('input[name=cv-file]').val();

// Datadata to be sent to server
post_data = {'cvuserFile':cvuser_file};

// Ajax post data to server
$.post('../contact-me-cv.php', post_data, function(response){  

    // Load json data from server and output message    
    if(response.type == 'error') {

        ...

    } else {

        ...

    }

}, 'json');

});

PHP

<?php

// Use PHP To Detect An Ajax Request
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

    // Exit script for the JSON data
    $output = json_encode(
    array(
        'type'=> 'error',
        'text' => 'Request must come from Ajax'
    ));

    die($output);
}

if(empty($_POST["cvuserFile"])) {
    $output = json_encode(array('type'=>'error', 'text' => '<i class="icon ion-close-round"></i> Please attach your CV'));
    die($output);
}

$path = 'upload/' . $_FILES["cvuserFile"]["name"];
move_uploaded_file($_FILES["cvuserFile"]["tmp_name"], $path);

require 'php/class/class.phpmailer.php';

$mail = new PHPMailer();

//Set PHPMailer to use SMTP.
$mail->IsSMTP();       
//Set SMTP host name                          
$mail->Host = 'smtp.gmail.com';                           
//Set TCP port to connect to 
$mail->Port = '587';
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;

$mail->isHTML(true);

//Provide username and password yo your google account   
$mail->Username = "*****@gmail.com";                 
$mail->Password = "*******";   

$mail->WordWrap = 50;

$mail->From = $_POST["cvuserEmail"];
$mail->FromName = $_POST["cvuserName"];
$mail->setFrom('*****', '**** ****');
$mail->addAddress('*****', 'John Doe');
//Set the subject line

$mail->AddAttachment($path);

$mail->Subject = 'New message from my website!';

$mail->Body = 'Hello' . "\r\n" ;

if(!$mail->send()) 
{
    $output = json_encode(array('type'=>'error', 'text' => '<i class="icon ion-close-round"></i> Oops! Looks like something went wrong, please check your PHP mail configuration.'));
    die($output);
    unlink($path);
} 
else 
{
    $output = json_encode(array('type'=>'message', 'text' => '<i class="icon ion-checkmark-round"></i> Hello '.$_POST["cvuserName"] .', Your message has been sent, we will get back to you asap !'));
    die($output);
}
?>

Is someone able to save my life on that matter?

I'm well receiving the form submission but without the file, I just have an empty file in the email.

Note that I'm working under MAMP for now.

Thanks to this amazing community

user3817291
  • 107
  • 11
  • try with first upload file and then attach to php mailer – Rp9 Feb 09 '18 at 10:34
  • 1
    Possible duplicate of [Send File Attachment from Form Using phpMailer and PHP](https://stackoverflow.com/questions/11764156/send-file-attachment-from-form-using-phpmailer-and-php) – Suneel Kumar Feb 09 '18 at 10:35
  • Base your code on the [send_file_upload example](https://github.com/PHPMailer/PHPMailer/blob/master/examples/send_file_upload.phps) provided with PHPMailer. – Synchro Feb 09 '18 at 10:37
  • Thanks for the answers guys, @Synchro I tried this yesterday but impossible to get it to work, I'm trying again. – user3817291 Feb 09 '18 at 10:39
  • Check the return value from `addAttachment` - it will return `false` if it can't read the file, which is the most likely problem. – Synchro Feb 09 '18 at 10:41
  • @synchro how to check the return value ? It's driving me crazy lol, I'm making something wrong but I can't find what... – user3817291 Feb 09 '18 at 10:53
  • Base your code on https://github.com/PHPMailer/PHPMailer/blob/master/examples/send_file_upload.phps – saiibitta Feb 09 '18 at 11:28
  • @Synchro I updated the PHP code as you can see above, now I don't get anything, it's like broken, completely lost... – user3817291 Feb 09 '18 at 11:29
  • Check return value: `if (!$mail->addAttachment($uploadfile, 'UPLOAD file')) echo "attachment of $uploadfile failed";`. That will also show you the path it's trying to use, so you can check it manually. – Synchro Feb 09 '18 at 14:11
  • Hey, I'm supposed to see that in the Chrome console? That's weird, I love your plugin so much but this time, I'm like in the middle of nowhere... I wanted to use the very last version but I can't get it to work, vous aimez les Alpes? :-) – user3817291 Feb 09 '18 at 14:16
  • @synchro can you confirm the script can work even if the php part is in an other file than the index.html ? – user3817291 Feb 11 '18 at 17:54
  • Test the PHP by itself - trying to debug server-side problems on a client is far more difficult, as you're finding. – Synchro Feb 11 '18 at 18:35
  • @synchro Thank you for your help, as you can see above, I updated the topic, showing you my HTML, JS and PHP. At the moment, if I use this PHP code directly in my HTML file, it's working, I receive the file attached. But using the JS and the external PHP file, it's not working... – user3817291 Feb 11 '18 at 19:01
  • @synchro I'm sure I'm making a stupid thing but I can't find which one... – user3817291 Feb 12 '18 at 08:34
  • You're not checking whether the submission actually contains the file - inspect what is in `$_FILES` before you use it, check the return value of `move_uploaded_file`. Also you're using a very old version of PHPMailer - update it. – Synchro Feb 12 '18 at 10:25

1 Answers1

0

You need to use move_uploaded_file (http://php.net/manual/en/function.move-uploaded-file.php) to move your uploaded file in to an accessible location on your file system.

You variable $uploadfile should contain the path to the file.

tempnam (http://php.net/manual/en/function.tempnam.php) only creates a new (empty) file - it doesn't move your uploaded file!

markt
  • 903
  • 7
  • 21
  • Hey, you're right but can't find how to use it with my current code :( – user3817291 Feb 09 '18 at 12:50
  • In your updated code you just assume that the file upload is present (it may not be), and that the `move_uploaded_file` call works (it may not). – Synchro Feb 12 '18 at 10:27
  • in your html, the name of the upload is `cv-file`, but in the php you have `$_FILES["cvuserFile"]` – markt Feb 12 '18 at 12:20