在何处将php $ header放入特定的联系表中以更改电子邮件“发件人”地址

The*_*ewy 5 html php forms email-headers

我试图$headers = "From: webmaster@example.com\r\n";在PHP中使用,将联系表单上的“发件人”电子邮件地址设置为“ name@companyname.com”。

它是参考此答案的PHP邮件功能“发件人”地址,我对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>';
            }

        }
    }
?>
Run Code Online (Sandbox Code Playgroud)

sea*_*pip 5

现在,您已经向mail()函数传递了3个参数,第4个参数用于标题。

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

<?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>';
            }

        }
    }
?>
Run Code Online (Sandbox Code Playgroud)

更多信息:https//www.php.net/manual/en/function.mail.php

同样对于交易电子邮件,请检查API,例如mailgun,sendgrid等。

他们还提供了PHP库,用于从服务器发送电子邮件,这通常比普通邮件服务器(邮件最终在垃圾邮件箱中)可靠。这些服务还具有出色的仪表板,因此您可以查看成功发送和接收的邮件数量。