使用PHP mail()发送附件(.docx);

时间:2018-06-01 04:37:56

标签: php email email-attachments

使用PHP mail()发送邮件时没有附件工作正常。

但是在发送附件时sending Failed 请帮我。 我的代码有什么问题?

提前致谢。 我的HTML代码如下所示

的index.html

<html>
    <head>
        <title>Mail</title>
    </head>
    <body>
        <form method="POST" action="mail.php" enctype="multipart/form-data">
            <input type="text" name="fromName">
            <input type="email" name="fromEmail">
            <input type="file" name="fileAttach">
            <input type="submit" name="submit">

        </form>
    </body>
</html>

以下是我的邮件代码

mail.php

<?php
if (isset($_POST['submit'])) {
   /* $mailto = $_POST["mailTo"];*/
   $mailto = "to@gmail.com";
    $from_mail = $_POST["fromEmail"];
    $replyto = $_POST["fromEmail"];
    $from_name = $_POST["fromName"];
    /*$message = $_POST["message"];*/
    $message="This is message part";
    /*$subject = $_POST["subject"];*/
    $subject ="this is subject part";

    $filename = $_FILES["fileAttach"]["name"];
    $content = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));

    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: " . $from_name . " <" . $from_mail . ">\r\n";
    $header .= "Reply-To: " . $replyto . "\r\n";

    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--" . $uid . "\r\n";

// You add html "Content-type: text/html; charset=utf-8\n" or for Text "Content-type:text/plain; charset=iso-8859-1\r\n" by I.khan
    $header .= "Content-type:text/html; charset=utf-8\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";

// User Message you can add HTML if You Selected HTML content
    $header .= "<div style='color: red'>" . $message . "</div>\r\n\r\n";

    $header .= "--" . $uid . "\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n"; // For Attachment
    $header .= $content . "\r\n\r\n";
    $header .= "--" . $uid . "--";
    if (mail($mailto, $subject, $message, $header)) {
        echo "<script>alert('Success');</script>"; // or use booleans here
    } else {
        //echo mysqli_error();
        echo "<script>alert('Failed');</script>";

    }
}

代码中是否有任何要添加或更改的内容?

0 个答案:

没有答案