html表单电子邮件php“From”形成

时间:2013-12-28 18:55:07

标签: php html email

我正在为网站写一个html / php电子邮件表单。 php工作正常,但在电子邮件标题的“发件人:”字段的电子邮件端看起来像“Tomemxample@email.com”,当我希望它看起来像“Tom,example @email.com”。我是新手使用PHP所以我可能会遗漏一些语法明智的东西。

<?php 
    function spamcheck($field) {
        $field=filter_var($field, FILTER_SANITIZE_EMAIL);
        if(filter_var($field, FILTER_SANITIZE_EMAIL)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    $name = test_input($_POST['name']);
    $email = test_input($_POST['email']);
    $subject = test_input($_POST['subject']);
    $message = test_input($_POST['message']);
    $to = "email goes here";
    $send = mail($to, $subject, $message, "From: " .$name .$email);
    if ($send) {
        header ("Location: http:// refreshes the page");
    } else {
        echo "The form could not be sent, please contact me at: email goes here";
    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
?>

3 个答案:

答案 0 :(得分:0)

将电子邮件地址放在“&lt;”之间和“&gt;”如下:

   $send = mail($to, $subject, $message, "From:". $name ."<".$email.">". "\r\n");

答案 1 :(得分:0)

根据RFC 5321和5322,您要求的内容将形成无效的电子邮件地址 - 不允许使用逗号或空格。

RFC5321

RFC5322

答案 2 :(得分:0)

只需在名称和电子邮件之间添加逗号:)

$send = mail($to, $subject, $message, "From: " . $name . ", " . $email);

应该是这样的:

$send = mail($to, $subject, $message, "From: " . $name . "<" . $email . ">");

结帐

http://php.net/manual/en/function.mail.php