【问题标题】:PHP contact form is sending encoded text on email bodyPHP 联系表单正在电子邮件正文上发送编码文本
【发布时间】:2017-07-08 22:30:59
【问题描述】:

PHP 联系表单正在电子邮件正文上发送编码文本。该电子邮件包含 附件和输入字段中的一些文本。消息必须在发送之前进行解码。如何使用输入的正确文本解码"$message"

请看下面给出的代码:

if (isset($_POST['submit'])) {
if ($_POST['email'] == '' || $_FILES['file_upload'] == '' || $_POST["fname"] == '' || $_POST["lname"] == '' || $_POST["message"] == '') {
    echo '<p class="red-info">Please Fill All The Fields</p>';

} else {

    $from_email      = $_POST['email']; //from mail, it is mandatory with some hosts
    $recipient_email = 'myemail@gmail.com'; //recipient email (most cases it is your personal email)

    //Capture POST data from HTML form and Sanitize them,
    $sender_fname   = filter_var($_POST["fname"], FILTER_SANITIZE_STRING); //sender name
    $sender_lname   = filter_var($_POST["fname"], FILTER_SANITIZE_STRING); //sender name
    $sender_phone_1 = filter_var($_POST["phone_1"], FILTER_SANITIZE_STRING); //sender name
    $sender_phone_2 = filter_var($_POST["phone_2"], FILTER_SANITIZE_STRING); //sender name
    $sender_phone_3 = filter_var($_POST["phone_3"], FILTER_SANITIZE_STRING); //sender name
    $sender_phone   = $sender_phone_1 . ' ' . $sender_phone_2 . ' ' . $sender_phone_3; //sender name
    $reply_to_email = filter_var($_POST["email"], FILTER_SANITIZE_STRING); //sender email used in "reply-to" header
    $subject        = 'Contact Form'; //get subject from HTML form
    $message        = filter_var($_POST["message"], FILTER_SANITIZE_STRING); //message

    /* //don't forget to validate empty fields
    if(strlen($sender_name)<1){
    die('Name is too short or empty!');
    }
    */

    //Get uploaded file data
    $file_tmp_name = $_FILES['file_upload']['tmp_name'];
    $file_name     = $_FILES['file_upload']['name'];
    $file_size     = $_FILES['file_upload']['size'];
    $file_type     = $_FILES['file_upload']['type'];
    $file_error    = $_FILES['file_upload']['error'];

    if ($file_error > 0) {
        die('Upload error or No files uploaded');
    }
    //read from the uploaded file & base64_encode content for the mail
    $handle  = fopen($file_tmp_name, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $encoded_content = chunk_split(base64_encode($content));

    $boundary = md5("sanwebe");
    //header
    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "From:" . $from_email . "\r\n";
    $headers .= "Reply-To: " . $reply_to_email . "" . "\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

    //plain text
    $body = "--$boundary\r\n";
    $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
    $body .= "<br />First Name:" . $sender_fname;
    $body .= "<br />Last Name:" . $sender_lname;
    $body .= "<br />Phone:" . $sender_phone;
    $body .= "<br />Message:";
    $body .= chunk_split(base64_encode($message));

    //attachment
    $body .= "--$boundary\r\n";
    $body .= "Content-Type: $file_type; name=" . $file_name . "\r\n";
    $body .= "Content-Disposition: attachment; filename=" . $file_name . "\r\n";
    $body .= "Content-Transfer-Encoding: base64\r\n";
    $body .= "X-Attachment-Id: " . rand(1000, 99999) . "\r\n\r\n";
    $body .= $encoded_content;

    $sentMail = mail($recipient_email, $subject, $body, $headers);
    if (isset($sentMail)) //output success or failure messages
        {
        echo '<p class="green-info">Your Email Has Been Submitted!We will contact soon.</p>';
        echo "<script>document.contact.reset();</script>";
        header("location: contect.php");
    } else {
        die('Could not send mail! Please check your PHP mail configuration.');
    }
}

}

【问题讨论】:

  • 您的问题/问题是什么? :)

标签: php forms email


【解决方案1】:

您是否尝试过设置内容类型?

$headers .= "Content-Type: text/html;";

【讨论】:

  • 还没有..让我检查一下:)
  • 我已经在标题上设置了这个。但是是 $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";和 $headers .= "Content-Type: text/html;";一起工作?我需要发送附件以及文本
  • @AmitKollolDey 发现 this
  • 我已经检查了链接并将标题设置为 $headers .= "Content-Type: multipart/alternative; boundary = $boundary\r\n\r\n"; . 仍然得到像这样的消息的编码文本 YW1pdCBhbWl0
【解决方案2】:

改成这个了,谢谢 :)

        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "From:" . $from_email . "\r\n";
        $headers .= "Reply-To: " . $reply_to_email . "" . "\r\n";
        $headers .= "Content-Type: multipart/alternative; boundary = $boundary\r\n\r\n"; 

        //plain text
        $body = "--$boundary\r\n";
        $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
        $body .= "\n First Name:" . $sender_fname;
        $body .= "\n Last Name:" . $sender_lname;
        $body .= "\n Phone:" . $sender_phone; 
        $body .= "\n Message:" .$message;
        $body .= chunk_split(base64_encode()); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-18
    • 2019-08-15
    • 2019-09-04
    • 1970-01-01
    • 2017-11-21
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多