在特定联系表单中更改电子邮件“发件人”地址需要将php $headers放在哪里?

5
我正在尝试使用$headers = "From: webmaster@example.com\r\n";在PHP中设置联系表单的“发件人”电子邮件地址为“name@companyname.com”。
这是参考此答案PHP mail function 'from' address。我对php很陌生,如果答案很明显,还请见谅,但这让我感到很困扰。
下面是表单的代码,但我似乎无法使其工作? 有人知道如何/在哪里将其与下面的代码集成吗?
敬礼。
<?php 

    if($_POST['submit']) {

        if(!$_POST['name']) {
            $error="<br>- Please enter your name";
        }
        if(!$_POST['email']) {
            $error.="<br>- Please enter your email";
        }
        if(!$_POST['telephone']) {
            $error.="<br>- Please enter your telephone number";
        }
        if(!$_POST['message']) {
            $error.="<br>- Please enter your message";
        }
        if(!$_POST['checkbox']) {
            $error.="<br>- Please confirm you agree to the Privacy Policy";
        }

        if ($error) {
            $result='<div class="alert error">Whoops, there is an error. Please correct the following: '.$error.'</div>';
        } else {
        mail("name@company.com", "Contact Message", "Name: ".htmlspecialchars($_POST['name'])."
            Email: ".htmlspecialchars($_POST['email'])."
            Telephone: ".htmlspecialchars($_POST['telephone'])."
            Company: ".htmlspecialchars($_POST['company'])."
            Budget: ".htmlspecialchars($_POST['budget'])."
            Message: ".htmlspecialchars($_POST['message']));

            {
                $_POST= array();
                $result='<div class="alert thankyou" role="alert">THANK YOU! WE\'LL BE IN TOUCH SHORTLY...</div>';
            }

        }
    }
?>

1
我能提供的最好建议是使用PHPMailer而不是本地的mail()函数。https://github.com/PHPMailer/PHPMailer - benJ
1个回答

5

现在你已经向 mail() 函数传递了 3 个参数,第四个参数是用于邮件报头的。

因此,在消息之后将该字符串作为第四个参数传递即可。 更精确地说:

<?php 

    if($_POST['submit']) {

        if(!$_POST['name']) {
            $error="<br>- Please enter your name";
        }
        if(!$_POST['email']) {
            $error.="<br>- Please enter your email";
        }
        if(!$_POST['telephone']) {
            $error.="<br>- Please enter your telephone number";
        }
        if(!$_POST['message']) {
            $error.="<br>- Please enter your message";
        }
        if(!$_POST['checkbox']) {
            $error.="<br>- Please confirm you agree to the Privacy Policy";
        }

        if ($error) {
            $result='<div class="alert error">Whoops, there is an error. Please correct the following: '.$error.'</div>';
        } else {
        mail("name@company.com", "Contact Message", "Name: ".htmlspecialchars($_POST['name'])."
            Email: ".htmlspecialchars($_POST['email'])."
            Telephone: ".htmlspecialchars($_POST['telephone'])."
            Company: ".htmlspecialchars($_POST['company'])."
            Budget: ".htmlspecialchars($_POST['budget'])."
            Message: ".htmlspecialchars($_POST['message']),
            "From: webmaster@example.com\r\n"
        );

            {
                $_POST= array();
                $result='<div class="alert thankyou" role="alert">THANK YOU! WE\'LL BE IN TOUCH SHORTLY...</div>';
            }

        }
    }
?>

更多信息: https://www.php.net/manual/en/function.mail.php 此外,对于交易邮件,请查看像mailgun、sendgrid等API。
它们还提供PHP库,可从其服务器发送电子邮件,这通常比普通邮件服务器更可靠(邮件最终可能会出现在垃圾邮件箱中)。这些服务还具有出色的仪表板,因此您可以查看成功发送和接收的邮件数量。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接