send_file_upload.phps 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * PHPMailer simple file upload and send example.
  4. */
  5. //Import the PHPMailer class into the global namespace
  6. use PHPMailer\PHPMailer\PHPMailer;
  7. require '../vendor/autoload.php';
  8. $msg = '';
  9. if (array_key_exists('userfile', $_FILES)) {
  10. // First handle the upload
  11. // Don't trust provided filename - same goes for MIME types
  12. // See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
  13. $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
  14. if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  15. // Upload handled successfully
  16. // Now create a message
  17. // This should be somewhere in your include_path
  18. require '../vendor/autoload.php';
  19. $mail = new PHPMailer;
  20. $mail->setFrom('from@example.com', 'First Last');
  21. $mail->addAddress('whoto@example.com', 'John Doe');
  22. $mail->Subject = 'PHPMailer file sender';
  23. $mail->msgHTML("My message body");
  24. // Attach the uploaded file
  25. $mail->addAttachment($uploadfile, 'My uploaded file');
  26. if (!$mail->send()) {
  27. $msg .= "Mailer Error: " . $mail->ErrorInfo;
  28. } else {
  29. $msg .= "Message sent!";
  30. }
  31. } else {
  32. $msg .= 'Failed to move file to ' . $uploadfile;
  33. }
  34. }
  35. ?>
  36. <!DOCTYPE html>
  37. <html>
  38. <head>
  39. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  40. <title>PHPMailer Upload</title>
  41. </head>
  42. <body>
  43. <?php if (empty($msg)) { ?>
  44. <form method="post" enctype="multipart/form-data">
  45. <input type="hidden" name="MAX_FILE_SIZE" value="100000"> Send this file: <input name="userfile" type="file">
  46. <input type="submit" value="Send File">
  47. </form>
  48. <?php } else {
  49. echo $msg;
  50. } ?>
  51. </body>
  52. </html>