0
0
mirror of https://github.com/PHPMailer/PHPMailer.git synced 2024-09-20 01:52:15 +02:00
PHPMailer/examples/send_file_upload.phps

52 lines
1.7 KiB
Plaintext
Raw Normal View History

2014-12-24 10:40:13 +01:00
<?php
/**
* PHPMailer simple file upload and send example.
2014-12-24 10:40:13 +01:00
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
2014-12-24 10:40:13 +01:00
$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']));
2014-12-24 10:40:13 +01:00
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
// Upload handled successfully
// Now create a message
2015-11-09 19:09:13 +01:00
require '../vendor/autoload.php';
2014-12-24 10:40:13 +01:00
$mail = new PHPMailer;
$mail->setFrom('from@example.com', 'First Last');
$mail->addAddress('whoto@example.com', 'John Doe');
$mail->Subject = 'PHPMailer file sender';
2017-01-05 13:06:20 +01:00
$mail->Body = 'My message body';
2014-12-24 10:40:13 +01:00
// Attach the uploaded file
$mail->addAttachment($uploadfile, 'My uploaded file');
if (!$mail->send()) {
2016-03-29 10:05:36 +02:00
$msg .= "Mailer Error: " . $mail->ErrorInfo;
2014-12-24 10:40:13 +01:00
} else {
2016-03-29 10:05:36 +02:00
$msg .= "Message sent!";
2014-12-24 10:40:13 +01:00
}
} else {
2016-03-29 10:05:36 +02:00
$msg .= 'Failed to move file to ' . $uploadfile;
2014-12-24 10:40:13 +01:00
}
}
?>
<!DOCTYPE html>
<html>
<head>
2016-03-29 10:05:36 +02:00
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
2014-12-24 10:40:13 +01:00
<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="100000"> Send this file: <input name="userfile" type="file">
<input type="submit" value="Send File">
</form>
<?php } else {
echo $msg;
} ?>
</body>
</html>