Hey guys the code below worked perfectly fine for me. Just replace the setFrom and addAddress with your preference and that's it.
<?php
/**
* PHPMailer simple file upload and send example.
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$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
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name']));
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
// Upload handled successfully
// Now create a message
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->setFrom('[email protected]', 'CV from Web site');
$mail->addAddress('[email protected]', 'CV');
$mail->Subject = 'PHPMailer file sender';
$mail->Body = 'My message body';
$filename = $_FILES["userfile"]["name"]; // add this line of code to auto pick the file name
//$mail->addAttachment($uploadfile, 'My uploaded file'); use the one below instead
$mail->addAttachment($uploadfile, $filename);
if (!$mail->send())
{
$msg .= "Mailer Error: " . $mail->ErrorInfo;
}
else
{
$msg .= "Message sent!";
}
}
else
{
$msg .= 'Failed to move file to ' . $uploadfile;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHPMailer Upload</title>
</head>
<body>
<?php if (empty($msg)) { ?>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="4194304" />
<input name="userfile" type="file">
<input type="submit" value="Send File">
</form>
<?php } else {
echo $msg;
} ?>
</body>
</html>