PHPMailer电子邮件没有附件发送

时间:2015-07-23 15:49:00

标签: php html forms email phpmailer

我正在尝试使用PHPMailer通过电子邮件发送表单结果,我收到了电子邮件。出于某种原因虽然电子邮件没有任何附件。显然我一定是做错了什么,但我不知道是什么。任何帮助表示赞赏。

这是PHP:

<?php
require_once('class.phpmailer.php');

if (isset($_POST["submit"])) {
  $name = $_POST['name'];
  $location = $_POST['location'];
  $desc = $_POST['desc'];

  if(isset($_POST['submit']))
  {
    $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(), sha1($_FILES['userfile']['name']));
      if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
          // Upload handled successfully
          // Now create a message
          // This should be somewhere in your include_path
          require 'PHPMailerAutoload.php';
          $mail = new PHPMailer();

          $mail->From = 'gallery@####.co.uk';
          $mail->FromName = 'Gallery Robot';
          $mail->addAddress('gallery@####.co.uk');
          $mail->Subject = 'PHPMailer file sender';
          $mail->Body = "My message body";
          // Attach the uploaded file
          $mail->addAttachment($uploadfile, 'My uploaded file');

          if ($mail->send()) {
              $msg = "Message sent!";
          } else {
              $msg = "Mailer Error: " . $mail->ErrorInfo;
          }
      } else {
          $msg = 'Failed to move file to ' . $uploadfile;
      }
    }
  }

这是HTML表单:

<form role="form" method="post" action="index.php" enctype="multipart/form-data">
                  <div class="form-group">
                    <label for="name">Your Name</label>
                    <input class="form-control" placeholder="John Smith" type="text" name="name" required value="<?php echo htmlspecialchars($_POST['name']); ?>">
                  </div>
                  <div class="form-group">
                    <label for="location">Location</label>
                    <input class="form-control" placeholder="South-west bank of Trout Pool." type="text" name="location" value="<?php echo htmlspecialchars($_POST['location']); ?>">
                  </div>
                  <div class="form-group">
                    <label for="desc">Picture Description</label>
                    <textarea class="form-control" name="desc" rows="3" placeholder="A picture of a 6lbs trout, my biggest all season."><?php echo htmlspecialchars($_POST['desc']); ?></textarea>
                  </div>
                  <div class="form-group">
                    <label for="btn-upload">Upload Image File</label>
                    <input type="file" id="uploaded_file" name="uploaded_file" value="<?php echo htmlspecialchars($_POST['file']); ?>">
                  </div>
                  <div class="progress">
                    <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100" style="width: 76%" data-toggle="tooltip" title="This is currently useless and broken. Sorry about that.">
                      <span class="sr-only">45% Complete</span>
                    </div>
                  </div>
                  <input id="submit" name="submit" type="submit" value="Upload" class="btn btn-primary">
                </form>

如果知道这一点很重要,那么表格就是一个模态。

**编辑:**我已经使用PHPMailer GitHub上的示例更新了我的代码,现在我根本没有收到任何消息。我现在做错了什么?

0 个答案:

没有答案