0

I am trying to create a form with PHPMailer that allows me to send files. Sometimes it goes well but most of the time I get the following error:

'Failed to move file to C:\Inetpub\vhosts\mywebsite.nl\tmp\4a47EC1.tmp.jpg '.

This is the code:

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function

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

require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

$msg = '';
if (array_key_exists('userfile', $_FILES)) {
    //First handle the upload
    //Don't trust provided filename - same goes for MIME types
    //See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
    //Extract an extension from the provided filename
    $ext = PHPMailer::mb_pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);
    //Define a safe location to move the uploaded file to, preserving the extension
    $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'])) . '.' . $ext;

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        //Upload handled successfully
        //Now create a message
        $mail = new PHPMailer();
        //Server settings
        //$mail->SMTPDebug = SMTP::DEBUG_SERVER;       //Enable verbose debug output
        $mail->isSMTP();                               //Send using SMTP
        $mail->Host       = 'smtp.mail.yahoo.com';     //Set the SMTP server to send through
        $mail->SMTPAuth   = true;                      //Enable SMTP authentication
        $mail->Username   = 'myemail@yahoo.com';       //SMTP username
        $mail->Password   = 'mypassword';              //SMTP password
        $mail->SMTPSecure = 'ssl';                     //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port       = 465;                       //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
        $mail->setFrom('myemail@yahoo.com', 'First Last');
        $mail->addAddress('myemail@yahoo.com', 'John Doe');
        $mail->Subject = 'PHPMailer file sender';
        $mail->Body = 'My message body';
        //Attach the uploaded file
        if (!$mail->addAttachment($uploadfile, 'My uploaded file')) {
            $msg .= 'Failed to attach file ' . $_FILES['userfile']['name'];
        }
        if (!$mail->send()) {
            $msg .= 'Mailer Error: ' . $mail->ErrorInfo;
        } 
        else {
            $msg .= 'Message sent!';
        }
    } 
    else {
        $msg .= 'Failed to move file to ' . $uploadfile;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>PHPMailer Upload</title>
</head>
<body>
<?php include 'inc/menu-inc.php' ?>
    <div id="wrap">
<?php if (empty($msg)) { ?>
    <form method="post" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="100000"> Send this file: <input name="userfile" type="file">
        <input type="submit" value="Send File">
    </form>
<?php } else {
    echo htmlspecialchars($msg);
} ?>
    </div>
<?php include 'inc/footer-inc.php' ?>
</body>
</html>

Does anyone have an idea how this is possible?

Thanks in advance for responses

splintermark77
  • 93
  • 1
  • 1
  • 4
  • 2
    Check file and directory permissions. – Markus Zeller Apr 18 '21 at 09:44
  • Are you going to store those files for future use, or are you just using those files for this specific operation? If it's only for this, then you don't need to move the file at all, just use the temp file directly (seeing that you're only moving the temp file within the temp folder anyways) – M. Eriksson Apr 18 '21 at 10:29
  • The second argument to `addAttachment()` is an optional _filename_ and if set, it will override the original filename of the attachment, so you're code will rename the attachment as `My uploaded file` (with no extension) when added to the email. – M. Eriksson Apr 18 '21 at 10:36

1 Answers1

0

I have found the solution.

I have removed the hidden input line.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>PHPMailer Upload</title>
</head>
<body>
    <div id="wrap">
<?php if (empty($msg)) { ?>
    <form method="post" enctype="multipart/form-data">
        <!--<input type="hidden" name="MAX_FILE_SIZE" value="100000"> REMOVE THIS ROW -->
        Send this file: <input name="userfile" type="file">
        <input type="submit" value="Send File">
    </form>
<?php } else {
    echo htmlspecialchars($msg);
} ?>
    </div>
</body>
</html>
splintermark77
  • 93
  • 1
  • 1
  • 4