PHP 邮件表单无法识别重音/特殊字符

问题描述 投票:0回答:1

正如我所见,这是一个无休无止的话题......我已经阅读并重读了十几篇具有相同问题的帖子,但我无法找到任何解决方案来解决我的情况:所以,例如,“Envío ” 在我收到的消息中显示为“Envío”。 我的代码如下...并且我根据我发现的许多(许多!)不同建议对其进行了修改,但没有使它们中的任何一个起作用!

请帮忙! =(

<html lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <link href="assets/css/style.css" rel="stylesheet">
  <link href="assets/css/custom.css" rel="stylesheet">
</head>
<body>

<div class="col-md-6">
<div class="form contact-form">
        <?php
        error_reporting(0);
        $action=$_REQUEST['action']; 
        if ($action=="")    /* display the contact form */ 
        { 
        ?>
<form class="php-email-form" id="contact-form" action="<?php $_SERVER["PHP_SELF"];?>" method="POST" enctype="multipart/form-data" autocomplete="etelpmocotua">
<input type="hidden" name="action" value="submit">
<div class="form-group">
<input class="form-control" type="text" name="name" value="" size="30" placeholder="Nombre" class="form-area" autocomplete="etelpmocotua" /></div>
<div class="form-group mt-3"><input class="form-control" type="text" name="email" value="" size="30" placeholder="e-Mail" class="form-area" autocomplete="etelpmocotua" /></div>
<div class="form-group mt-3"><input class="form-control" type="text" name="phone" value="" size="30" placeholder="Teléfono" class="form-area" autocomplete="etelpmocotua" /></div>
<textarea class="form-control mt-3" name="message" placeholder="Tu mensaje" class="form-area"></textarea>
<div class="my-3"><div class="loading">Enviando...</div></div>
<div class="text-left"><button type="submit">Enviar Mensaje</button></div>
</form>

        <?php 
                }  
        else
        {
        $name=$_REQUEST['name']; 
        $email=$_REQUEST['email'];
        $phone=$_REQUEST ['phone']; 
        $message=$_REQUEST['message'];

        if (($name=="")||($email=="")||($phone=="")||($message=="")) 
        { 
        echo "<div class='error-message'><strong>Tu mensaje no pudo ser enviado</strong><br>Por favor, completá todos los campos.<br>
        <a href=\"javascript:history.back()\" target=\"_self\"><img src='assets/img/back.png' /></a></div>";
        } 

        else{
        $body .="Nuevo mensaje desde la web!\n
        Nombre: $name
        e-Mail: $email
        Telefono: $phone\n
        Mensaje:\n
        $message";
        $from .="From: '[email protected]'>\r\n Return-path: $email";
        $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

        $subject .="Contacto WEB";
        mail("[email protected]", $subject, $body, $from);

        echo "<div class='sent-message'>Tu mensaje ha sido enviado<br />
        y pronto será respondido.<br><br>
        <strong>Gracias por contactarnos!<strong></div>"; 
        }
        }
        ?>
</div>
</div>
</body>
php html forms special-characters
1个回答
0
投票

你有很多可以微调/需要纠正的地方

  1. 您尝试在 $headers 中指定 utf-8,但您的 $headers 并没有在 mail() 中使用
  2. 您正在使用 HTML 格式,但已尝试使用 (更好用
    <br>
  3. 如果您在消息中输入了回车符,则在 HTML 中将无法正常显示,所以最好使用
    nl2br
    来解析它

因此,修改后的代码将是: (确保您已将 your_email@your_domain.com 替换为您的真实电子邮件以接收)

<html lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <link href="assets/css/style.css" rel="stylesheet">
  <link href="assets/css/custom.css" rel="stylesheet">
</head>
<body>

<div class="col-md-6">
<div class="form contact-form">

<?php

$action=$_REQUEST['action']; 

if ($action=="")  {  ?>

<form class="php-email-form" id="contact-form" action="<?php $_SERVER["PHP_SELF"];?>" method="POST" enctype="multipart/form-data" autocomplete="etelpmocotua">
<input type="hidden" name="action" value="submit">
<div class="form-group">
<input class="form-control" type="text" name="name" value="" size="30" placeholder="Nombre" class="form-area" autocomplete="etelpmocotua" /></div>
<div class="form-group mt-3"><input class="form-control" type="text" name="email" value="" size="30" placeholder="e-Mail" class="form-area" autocomplete="etelpmocotua" /></div>
<div class="form-group mt-3"><input class="form-control" type="text" name="phone" value="" size="30" placeholder="Teléfono" class="form-area" autocomplete="etelpmocotua" /></div>
<textarea class="form-control mt-3" name="message" placeholder="Tu mensaje" class="form-area"></textarea>
<div class="my-3"><div class="loading">Enviando...</div></div>
<div class="text-left"><button type="submit">Enviar Mensaje</button></div>
</form>

<?php }  else   {
        $name=$_REQUEST['name']; 
        $email=$_REQUEST['email'];
        $phone=$_REQUEST ['phone']; 
        $message=$_REQUEST['message'];

        if (($name=="")||($email=="")||($phone=="")||($message=="")) 
        { 
        echo "<div class='error-message'><strong>Tu mensaje no pudo ser enviado</strong><br>Por favor, completá todos los campos.<br>
        <a href=\"javascript:history.back()\" target=\"_self\"><img src='assets/img/back.png' /></a></div>";
        } else{
        $body .="Nuevo mensaje desde la web!<br><br>
        Nombre: $name<br>
        e-Mail: $email<br>
        Telefono: $phone<br>Mensaje:<br><br>". nl2br($message);
        //    $headers .="From: '[email protected]'>\r\n Return-path: $email";
        //    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= 'From: <'. $email . '>' . "\r\n";
        //$headers .= 'Cc: [email protected]' . "\r\n";

        $subject .="Contacto WEB";
        mail("your_email@your_domain.com", $subject, $body, $headers);

        echo "<div class='sent-message'>Tu mensaje ha sido enviado<br />
        y pronto será respondido.<br><br>
        <strong>Gracias por contactarnos!<strong></div>"; 
        }
     }
?>

</div>
</div>
</body>
© www.soinside.com 2019 - 2024. All rights reserved.