Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP mail()发送附件(.docx);_Php_Email_Email Attachments - Fatal编程技术网

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

使用PHP mail()发送附件(.docx);,php,email,email-attachments,Php,Email,Email Attachments,使用PHPmail()发送邮件时,无附件工作正常 但在使用附件发送时发送失败 请帮帮我。 我的代码有什么问题 提前谢谢。 我的HTML代码如下所示 index.html <html> <head> <title>Mail</title> </head> <body> <form method="POST" action="mail.php" enctype="mu

使用PHP
mail()
发送邮件时,无附件工作正常

但在使用附件发送时
发送失败
请帮帮我。 我的代码有什么问题

提前谢谢。 我的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>";

    }
}

改用
PHPMailer
。我完全同意hungrykoala的说法。使用一个经过尝试和测试的邮件库,而不是低级别的
mail()
-功能。它为您提供了一种更详细的API、更容易通过SMTP发送的方式,这不仅更安全,而且还可以删除服务器依赖项,使您的脚本更具可移植性。是的,PHPMailer是正确的方式,请参阅关于附件的说明: