doujing1858 2015-07-23 15:48
浏览 40

PHPMailer电子邮件没有附件发送

I am trying to use PHPMailer to email form results, and I am getting the email. For some reason though the email is coming through without any attachments. Obviously I must be doing something wrong, but I don't know what. Any help is appreciated.

Here is the 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;
      }
    }
  }

and here is the HTML form:

<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>

If it is important to know, the form is inside a modal.

**EDIT: ** I have updated my code using the example on the PHPMailer GitHub and now I'm receiving no message at all. What have I done wrong now?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 一道python难题2
    • ¥15 一道python难题
    • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试
    • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
    • ¥15 教务系统账号被盗号如何追溯设备