php - 将 php $headers 放在特定联系表单中的何处以更改电子邮件 'From' 地址

标签 php html forms email-headers

我正在尝试在 PHP 中使用 $headers = "From: webmaster@example.com\r\n"; 将联系表单上的“发件人”电子邮件地址设置为“姓名” @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>';
            }

        }
    }
?>

最佳答案

现在您已经将 3 个参数传递给 mail() 函数,第 4 个参数用于 header 。

因此只需将该字符串作为消息后的第四个参数传递即可。更准确地说:

<?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

对于交易电子邮件,检查 API,如 mailgun、sendgrid 等。

他们还提供 PHP 库,用于从他们的服务器发送电子邮件,这通常比普通邮件服务器(邮件最终进入垃圾邮件箱等)更可靠。这些服务还有很棒的仪表板,因此您可以查看成功发送和接收了多少邮件。

关于php - 将 php $headers 放在特定联系表单中的何处以更改电子邮件 'From' 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56668783/

相关文章:

javascript - 数据库驱动的页面内容

php - 是什么原因导致 memcached 错误 10 ('server error' )?

html - 字体在 Ipad2 和 iphone4 上显示两次 - 在其他桌面浏览器上工作正常

javascript - 使用 angularjs 隐藏错误消息

javascript - 指示浏览器仅从一个目录中选择

php - 从我的数组中删除项目及其 ID

html - Quicktime - Wmode透明问题

javascript - Box-shadow 响应鼠标位置

forms - Spring 表单错误地解码撇号

php - 将表单提交给 php 文件中的特定函数