如何通过php表单发送附件到邮件?

时间:2017-08-29 08:11:22

标签: php forms

我试图允许用户在填写表单时发送图片附件:

我可以发送电子邮件,但附件不会发送。

这是HTML

<form method="post" enctype="multipart/form-data">
            <div class="form-group">
              <label for="name">Name</label>
              <input type="text" class="form-control" id="name" placeholder="Your name" name="name">
            </div>

            <div class="form-group">
              <label for="email">Email address</label>
              <input type="email" class="form-control" id="email" placeholder="Please enter your email" name="email">
            </div>

            <div class="form-group">
              <textarea class="form-control" id="text" placeholder="What would you like to tell us" rows="3" name="text"></textarea>

              <div class="form-group">
                <label for="file"></label>
                <input type="file" class="form-control-file" id="file" name="attachment" onchange="return ValidateFileUpload()">
              </div>
            </div>

            <button type="submit" id="submit" class="btn btn-primary">Submit</button>
          </form>

这是PHP代码:

<?php

$error = ""; $successMessage = "";

if ($_POST) {

    if (!$_POST["email"]) {

        $error .= "An email address is required.<br>";

    }

    if (!$_POST["name"]) {

        $error .= "Your name is required.<br>";

    }


    if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {

        $error .= "The email address is invalid.<br>";

    }

    if ($error != "") {

        $error = '<div class="alert alert-danger" role="alert"><p>There were error(s) in your form:</p>' . $error . '</div>';

    } else {

        $emailTo = "@gmail.com";

        $subject = $_POST['name'];

        $content = $_POST['text'];

        $file = $_POST['file'];

        $headers = "From: ".$_POST['email'];

        if (mail($emailTo, $subject, $content, $headers)) {

            $successMessage = '<div class="alert alert-success" role="alert">Your message was sent, we\'ll get back to you ASAP!</div>';


        } else {

            $error = '<div class="alert alert-danger" role="alert"><p><strong>Your message couldn\'t be sent - please try again later</div>';


        }

    }

}

?>

1 个答案:

答案 0 :(得分:0)

function mail_attachment($to, $subject, $message, $from, $file) {
  // $file should include path and filename
    $filename = basename($file);
    $file_size = filesize($file);
    $content = chunk_split(base64_encode(file_get_contents($file))); 
    $uid = md5(uniqid(time()));
    $from = str_replace(array("\r", "\n"), '', $from); // to prevent email injection
    $header = "From: ".$from."\r\n"
            ."MIME-Version: 1.0\r\n"
            ."Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"
            ."This is a multi-part message in MIME format.\r\n" 
            ."--".$uid."\r\n"
            ."Content-type:text/plain; charset=iso-8859-1\r\n"
            ."Content-Transfer-Encoding: 7bit\r\n\r\n"
            .$message."\r\n\r\n"
            ."--".$uid."\r\n"
            ."Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"
            ."Content-Transfer-Encoding: base64\r\n"
            ."Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"
            .$content."\r\n\r\n"
            ."--".$uid."--"; 
   return mail($to, $subject, "", $header);
  }