0

I have an HTML form and I am using phpMailer. When completing the form and clicking submit all data entered in the text input fields etc work as expected. However, when I attach a file to the input type "file" when receiving the email no attachment is there.

Does anyone have any suggestions as to why the attachment is not attached?

FYI - at current, the site has no SSL certificate and the emails are going to the spam folder - could this be the issue?

I' am new to phpMailer and have been able to achieve the below by research.

phpMailer()

<?php

$file = $_POST['file'];

// more variables... 

$CustomerSignature = $_POST['q21'];
$Date = $_POST['q22'];
$filename = "./upload/" . $_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"],"./upload/" . $_FILES["file"]["name"]);

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    use PHPMailer\PHPMailer\SMTP;

    require 'phpMailer/src/Exception.php';
    require 'phpMailer/src/PHPMailer.php';
    require 'phpMailer/src/SMTP.php';
    require 'phpMailer/src/OAuth.php'; 

    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 3;
    $mail->Host = 'smtp.hostinger.com';
    $mail->Port = 465;
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'ssl';
    $mail->Username = '######';
    $mail->Password = '######';
    $mail->isHTML(true);
    $mail->setFrom('######', '######');
    $mail->addReplyTo('######', '######');
    $mail->addAddress('######', '######');
    $mail->addAddress('######');
    $mail->AddAttachment($filename);
    $mail->Subject = 'New Credit Application';
    $mail->Body = '<b>New Credit Application (Online)!</b></br>'
.'Trading Name: '.$TradingName.'</br>'
.'blah blah blah etc.'
.'Customer Signature: '.$CustomerSignature.'</br>'
.'Date: '.$Date.'</br>';
    if (!$mail->send()) {
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'The email message was sent.';
    }
?>

HTML file attachment part:

<form method="POST" action="testmail2.php" id="myform" class="fs-form fs-form-full" autocomplete="off">
                    <ol class="fs-fields">
                        
                        <li>
                            <label class="fs-field-label fs-anim-upper" for="file">Attachment</label>
                            <input class="fs-anim-lower" id="file" name="file" type="file" required/>
                        </li>
Martin
  • 22,212
  • 11
  • 70
  • 132
richag
  • 133
  • 13
  • Enable all error level and display it. `$path` is undefined variable, I don't see it anywhere. Before go to send an email, make sure that the file is uploaded properly. – vee Dec 13 '21 at 12:12
  • @vee , error log shows on first line, Could not access file: – richag Dec 13 '21 at 12:14
  • @vee, i made some code changes, now the error is gone, the email is attaching a blank file named uplaod, and the logs show the following: 2021-12-13 12:28:19 CLIENT -> SERVER: Content-Transfer-Encoding: base64 2021-12-13 12:28:19 CLIENT -> SERVER: Content-Disposition: attachment; filename=upload – richag Dec 13 '21 at 12:30
  • @Martin , 6.5.3 - the most up-to-date, I have only downloaded it today from GitHub – richag Dec 13 '21 at 12:36
  • 2
    Does this answer your question? [Send File Attachment from Form Using phpMailer and PHP](https://stackoverflow.com/questions/11764156/send-file-attachment-from-form-using-phpmailer-and-php) – Martin Dec 13 '21 at 12:46
  • PS Do not update your question with answers from the question. Keep the original issue as it is. Thank you. – Martin Dec 13 '21 at 13:00

4 Answers4

4

You're missing one fundamental thing: you have not set the encoding type of the form. The default encoding will not work with files, so $_FILES will be empty, and consequently functions like move_uploaded_file will fail because there is no file to move, not because you have got the path or permissions wrong (though you can do that too!). Your form tag should be:

<form method="POST" action="testmail2.php" id="myform" class="fs-form fs-form-full" autocomplete="off" enctype="multipart/form-data">

Aside from that, base your code on the file upload example provided with PHPMailer.

Synchro
  • 35,538
  • 15
  • 81
  • 104
2

$filename should be an absolute filepath on your server, not a relative path as you have a leading dot which can screw up a lot of things ./ ...

You have:

 $filename = "./upload/" . $_FILES["file"]["name"];

What you should have is:

 move_uploaded_file($_FILES["file"]["tmp_name"],$_SERVER['Document_root']."/path/to/upload/" . $_FILES["file"]["name"]);
 ...
 $filename = $_SERVER['Document_root']."/path/to/upload/" . $_FILES["file"]["name"];

you should be using the PHPMailer error reporter ($mail->SMTPDebug = SMTP::DEBUG_SERVER;) to check this sort of things, as well as reading your own PHP error messages. Also you should follow the examples set in the github.

Martin
  • 22,212
  • 11
  • 70
  • 132
  • i will make code change as suggested, Do i need to make any change to line 26? `move_uploaded_file($_FILES["file"]["tmp_name"],"./upload/" . $_FILES["file"]["name"]);` – richag Dec 13 '21 at 12:41
  • Yes. `./` is a relative file path. you should be using an absolute file path – Martin Dec 13 '21 at 12:42
  • No file is being uploaded to the folder in the directory , even after making your suggested changes – richag Dec 13 '21 at 12:48
  • So what does your PHPMailer error reporting tell you? – Martin Dec 13 '21 at 12:49
  • Could not access file: /upload/ – richag Dec 13 '21 at 12:49
  • @richag so that's your issue. As I said in my answer, I would suggest giving PHPMailer the absolute filepath to the file you want to attach. – Martin Dec 13 '21 at 12:51
  • sorry maybe you are miss understanding, this is a user contact form, where they upload their OWN file, and then I want the file attached to the email ... – richag Dec 13 '21 at 12:52
  • @richag `Could not access file: /upload/` this means PHPMailer can't access the file that was uploaded. This is a server access problem not the issue with the person who has uploaded their file . – Martin Dec 13 '21 at 12:56
  • When I go to the directory , folder upload , it is empty, suggesting that the file being attached is not actually uploading to the directory folder 'upload' – richag Dec 13 '21 at 12:57
  • @richag right, so then what does your PHP error log say about file uploading? ie your `move_uploaded_file` command? – Martin Dec 13 '21 at 12:58
  • @richag there are lots of Stack Overflow Q&A about file upload errors: https://stackoverflow.com/questions/9550972/php-cant-upload-files-to-server – Martin Dec 13 '21 at 12:59
  • @Matin, I enabled $mail->SMTPDebug = SMTP::DEBUG_SERVER; I get no error besides Could not access file ... form the logs, it looks like it is not even trying to upload the attached file. – richag Dec 13 '21 at 13:02
1

You might try the addStringAttachment() method instead

Despite of its name, it does not work with text files only, but with any kind of file

Please refer to the following code snippet

if (is_uploaded_file($_FILES["file"]["tmp_name"])) {
    $filename = $_FILES["file"]["tmp_name"];
    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    fclose($handle);
    $mail->addStringAttachment($contents, $filename);
}
Fabio Scagliola
  • 338
  • 2
  • 11
1

I have solved the issue

move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
$filename = "upload/" . $_FILES["file"]["name"];

Thank you to everyone who assisted

richag
  • 133
  • 13