Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
52 lines (50 sloc) 1.7 KB
* PHPMailer simple file upload and send example.
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
// First handle the upload
// Don't trust provided filename - same goes for MIME types
// See 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('', 'First Last');
$mail->addAddress('', 'John Doe');
$mail->Subject = 'PHPMailer file sender';
$mail->Body = 'My message body';
// Attach the uploaded file
$mail->addAttachment($uploadfile, 'My uploaded file');
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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHPMailer Upload</title>
<?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">
<?php } else {
echo $msg;
} ?>
You can’t perform that action at this time.