send_file_upload.phps 1.6 KB

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