skip to Main Content

Send mail with php mail() function.

Please review my code. It is not sending mail and the landing page is not being called. I have tested with another script to send mail on my host and it is working fine.

<?php

> if (isset($_POST['submit'])) {
> 
>     $name=$_POST['name'];
>     $mailFrom=$_POST['email'];
>     $message=$_POST['message'];
>     $mobile=$_POST['mobile'];
> 
> 
>     $mailTo="xxxxx";
>     $headers= "From : ".$mailFrom;
>     $txt ="You have received an email from".$name. ".nn".$message. ".nn Mobile :".$mobile;
>     $subject="Information Request";
> mail($mailTo, $subject, $txt, $headers);
> 
> header("Location: xxx.html"); //to create a landing page and test
> }
> ?>

2

Answers


  1. Chosen as BEST ANSWER

    I have changed the code to below and it is working. The issue was perhaps with the if statement

    <html>
        <head>
        <title>Contact form</title>
        </head>
        <body>
        
        <?php
        
            $name=$_POST['name'];
            $mailFrom=$_POST['email'];
            $message=$_POST['message'];
            $mobile=$_POST['mobile'];
        
        
            $mailTo='[email protected]';
            $headers= "From : ".$mailFrom;
            $txt ="You have received an email from".$name. ".nn".$message. ".nn Mobile :".$mobile;
            $subject='Information Request';
            $mailsent=mail($mailTo, $subject, $txt, $headers);
        
            if($mailsent) {
        
                header('Location: xxx.html');
                } else {
                echo 'An error occurred. Please contact us by email xxxxxx.';
                }
        ?>
                </body>
                </html>
    

  2. Based on your code, I would start checking things out from your "if" statement. See if the statement is reachable or not.

    I would suggest you go through PHP mail function doesn't complete sending of e-mail . It covers everything.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search