-2

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. ".\n\n".$message. ".\n\n Mobile :".$mobile;
>     $subject="Information Request";
> mail($mailTo, $subject, $txt, $headers);
> 
> header("Location: xxx.html"); //to create a landing page and test
> }
> ?>
Ash
  • 5
  • 3

2 Answers2

0

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.

ashish
  • 3,555
  • 1
  • 20
  • 26
0

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='info@xxx.com';
        $headers= "From : ".$mailFrom;
        $txt ="You have received an email from".$name. ".\n\n".$message. ".\n\n 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>
Ash
  • 5
  • 3