邮件联系表格中的UTF-8编码,

时间:2014-01-08 12:11:36

标签: php forms email character

我的网站上有一个简单的邮件脚本,但我不知道如何使用正确的编码发送邮件(使用字母“ľščťžýáíé”以包含错误字符的邮件结尾)任何人都可以帮助我吗?我在互联网上找到的解决方案似乎都没有用(邮件根本没有发送)

代码:

    <?php
                 $error    = ''; 
                 $name     = ''; 
                 $email    = ''; 
                 $subject  = ''; 
                 $message  = ''; 

            if(isset($_POST['send']))
            {
                 $name     = $_POST['name'];
                 $email    = $_POST['email'];
                 $subject  = $_POST['subject'];
                 $message  = $_POST['message'];

                if(trim($name) == '')
                {
                    $error = '<div class="errormsg"><p>Zadajte prosím svoje meno.</p></div>';
                }
                    else if(trim($email) == '')
                {
                    $error = '<div class="errormsg"><p>Zadajte prosím svoju e-mailovú adresu.</p></div>';
                }
                else if(!isEmail($email))
                {
                    $error = '<div class="errormsg"><p>Zadajte prosím platnú e-mailovú adresu.</p></div>';
                }
                    if(trim($subject) == '')
                {
                    $error = '<div class="errormsg"><p>Zadajte prosím predmet správy.</p></div>';
                }
                else if(trim($message) == '')
                {
                    $error = '<div class="errormsg"><p>Napíšte prosím text správy.</p></div>';
                }

                if($error == '')
                {
                    if(get_magic_quotes_gpc())
                    {
                        $message = stripslashes($message);
                    }

                    $to      = "xyz@xyz.com";

                    $subject = '[Kontaktny formular xyzvlmedia.sk] : ' . $subject;

                    $msg     = "Odosielatel : $name \r\nE-mail : $email \r\nPredmet : $subject \r\n\n" . "Sprava : \r\n$message";

                    mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
            ?>

                  <div class="thankyoumsg"><p>Ďakujeme <strong><?=$name;?></strong>! Vaša správa bola odoslaná.</p></div>

            <?php
                }
            }

            if(!isset($_POST['send']) || $error != '')
            {
            ?>

            <?=$error;?>

            <form method="post" name="contFrm" id="contFrm" action="">
<div class="formular">
                  Meno a priezvisko<br />
                  <input name="name" type="text" class="box" id="name" size="30" value="<?=$name;?>" /><br />
                  E-mail<br />
                  <input name="email" type="text" class="box" id="email" size="30" value="<?=$email;?>" /><br />
                  Predmet<br />
                  <input name="subject" type="text" class="box" id="subject" size="30" value="<?=$subject;?>" /><br />
                    Správa<br />
                  <textarea name="message" cols="40" rows="3"  id="message"><?=$message;?></textarea><br /><br />

                        <input name="send" type="submit" class="button" id="send" value="Odoslať" />
</div>
            </form>

            <?php
            }

            function isEmail($email)
            {
                return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i"
                        ,$email));
            }
            ?>

</div>

3 个答案:

答案 0 :(得分:1)

确保带有表单的页面与header("Content-Type: text/html; charset=utf-8");和元标记

一起发送
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

现在将适当的mime类型编码添加到邮件头:

Content-Type: text/html; charset=UTF-8\r\n

同时检查mail()的返回值:

$mail = mail(...);
if ($mail !== true) {
    // error sending mail
}

答案 1 :(得分:0)

请查看如何通过PHP发送HTML电子邮件。它非常简单,它可以解决您的问题。

http://css-tricks.com/sending-nice-html-email-with-php/

$to = 'bob@example.com';
$subject = 'Website Change Reqest';

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n"; //obviously don't use strip_tags as this is not a safe way to escape your variables. Please do proper escaping and pass in a variable
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '</body></html>';

确保包含标头变量:

mail($to, $subject, $message, $headers);

答案 2 :(得分:0)