0

Hi I´m new to PHP so hopefully someone can help me. I have read various topics about this but don´t seem to find a solution. I have a PHP code that I found that I have connected to my HTML contact form. This code worked on my other site that had another web host and had not SSL certificate. On my old site I received the email but I don´t on the new site. I think I need to add my web hosts IMAP and port to this PHP but I cant figure out how.

<?php   if (isset($_POST['email'])) {

        $email_to = "myemail@something.com";

        $email_subject = "Contact form";
        function died($error) {
            echo "Message not valid.<br /><br />";
            echo $error."<br /><br />";
            echo "Message not valid.<br /><br />";
            die();
        }
        if(!isset($_POST['name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['comments'])) {
            died('Message not valid');     
        }

        $name = $_POST['name'];
        $email_from = $_POST['email'];
        $comments = $_POST['comments'];

        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) {
        $error_message .= 'Message not valid.<br />';}if(strlen($comments) < 2) {
        $error_message .= 'Message not valid.<br />';}if(strlen($error_message) > 0) {
        died($error_message);}$email_message = "Contact form.\n\n<br /><br />";

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $email_message .= "Name: ".clean_string($name)."\n<br />";
        $email_message .= "Email: ".clean_string($email_from)."\n<br />";
        $email_message .= "Comments: ".clean_string($comments)."\n<br />";$headers = 'Content-type: text/html; charset=UTF-8' . "\r\n".'From: '.$email_from."\r\n".'Reply-To: '.$email_from."\r\n" .'X-Mailer: PHP/' . phpversion();@mail($email_to, $email_subject, $email_message, $headers);  ?> We will be in touch with you very soon.
    <?php
    }
    die();
    ?>
Eva
  • 1
  • Have you checked spam folder? If the email you are trying to send from MX records doesn't match the server this script is on it will likely go to spam every time. – Nico Plyley Mar 06 '19 at 15:18
  • Thank you for your answer. Yes I checked it is not there. – Eva Mar 07 '19 at 09:17

1 Answers1

1

I recommend switching to https://github.com/PHPMailer/PHPMailer it has many more features then the standard php mail function; including error handling.

Feel free to ask new questions when expriencing problems implementing.

AgeDeO
  • 3,137
  • 2
  • 25
  • 57