Addition to the script for submitting the form to the gmail mailbox

The self-written site had a form for editing letters to the gmail mailbox, but there were two problems:

  1. All emails came to spam
  2. Letters had to come from e-mail, which the user indicated in the form, so that it would be possible to quickly immediately respond to the letter to the user's mail. Instead of this address, the system address of the timeweb hosting account was substituted.

Originally the code to send was as follows

<?php
    if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['b_submit'])) {
        $name =$_POST['b_name'];
        $email = $_POST['b_email'];
        $message = $_POST['b_mess'];
        $subject  = iconv ("UTF-8", "CP1251", "Feedback form from the site");
        $to = '[email protected]';
        $body = iconv ("UTF-8", "CP1251", "Messages: $message");
        if ($_POST['b_submit']) {
            if ($name != '' && $email != '') {
                if (!preg_match("|^([a-z0-9_\.\-]{1,20})@([a-z0-9\.\-]{1,20})\.([a-z]{2,4})|is", strtolower($email))) {
                    echo '<h5>Error, please check e-mail!</h5>';
                } else if (mail ($to, $subject, $body, $email)) {
                    echo '<h5>Thank you, your message has been sent!</h5>
                        <script language="JavaScript" type="text/javascript">
                            function changeurl(){eval(self.location="/");}
                            window.setTimeout("changeurl();",4000);
                        </script>
                    ';
                } else {
                    echo '<h5>Error, please check the fields are correct!</h5>';
                }
            } else {
                echo '<h5>Please fill in all fields!!</h5>';
            }
        }
    }
?>

The hosting technical support advised to use the -f key in the transmission of the email address, but no variations of this key were suitable. After that, as a result of a long search for materials on the topic of sending letters, I came across one of the instructions for using php mail, where I noticed an optional item Replay-To, which, according to the description, was exactly what was missing. To implement it, a separate variable was created in the example, where the From, Reply-To, X-Mailer values ​​were placed. Having pinned all this to the script, we received a working form that sends letters from the e-mail boxes we need, responds to them and does not end up in spam.

<?php
    if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['b_submit'])) {
        $name =$_POST['b_name'];
        $email = $_POST['b_email'];
        $message = $_POST['b_mess'];
        $subject  = iconv ("UTF-8", "CP1251", "Feedback form from the site");
        $to = '[email protected]';
        $headers = 'From: '.$email."\r\n" .
                    'Reply-To: '.$email."\r\n" .
                    'X-Mailer: PHP/' . phpversion();
        $body = iconv ("UTF-8", "CP1251", "Messages: $message");
        if ($_POST['b_submit']) {
            if ($name != '' && $email != '') {
                if (!preg_match("|^([a-z0-9_\.\-]{1,20})@([a-z0-9\.\-]{1,20})\.([a-z]{2,4})|is", strtolower($email))) {
                    echo '<h5>Error, please check e-mail!</h5>';
                } else if (mail ($to, $subject, $body, $headers)) {
                    echo '<h5>Thank you, your message has been sent!</h5>
                        <script language="JavaScript" type="text/javascript">
                            function changeurl(){eval(self.location="/");}
                            window.setTimeout("changeurl();",4000);
                        </script>
                    ';
                } else {
                    echo '<h5>Error, please check the fields are correct!</h5>';
                }
            } else {
                echo '<h5>Please fill in all fields!!</h5>';
            }
        }
    }
?>

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
The comment language code.