0

I used mail() function to receive mail from customers who are visiting my web page I use this code for mail function (mail.php). I use this same code from the first it worked for me in the beginning but not working now.

<?php

if(isset($_POST['submit'])){
    $to = "******";
    $message = "
    <html>
        <head>
            <title>HTML email</title>
        </head>
        <body>
            <p>Enquiry</p>
            <table>
                <tr>
                    <td><strong>Name</strong></td><td>:</td><td>".$_POST['name']."</td>                 
                </tr>
                <tr>
                    <td><strong>Email ID</strong></td><td>:</td><td>".$_POST['email']."</td>                    
                </tr>
                <tr>
                    <td><strong>Mobile</strong></td><td>:</td><td>".$_POST['mobileno']."</td>                   
                </tr>
                <tr>
                    <td><strong>Message</strong></td><td>:</td><td>".$_POST['msg']."</td>                   
                </tr>
            </table>
        </body>
    </html>
    ";

    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    // More headers
    $headers .= 'From: <****>' . "\r\n";
    $headers .= 'Cc: *****' . "\r\n";

    if(mail($to,$subject,$message,$headers)) {
       echo '<script>
                alert("Email Sent Successfully!");
                window.location.href="../contact-us/contactus.html";
            </script>';
    } else {
        echo '<script>
                alert("Sorry your mail was not send kindly try again later.");
                window.location.href="../contact-us/contactus.html";
            </script>'; 
    }
}
?>

here is my contact form code..

<form method="post" action="../mail/mail.php">
     <p class="comment-form-author">
       <label>Name<span>(required)</span></label>
        <span class="icon-input">
        <input type="text" name="name" required />
        </span> </p>
        <p class="comment-form-email">
        <label>Email<span>(required)</span></label>
        <span class="icon-input">
        <input type="email" name="email" required />
        </span> </p>

        <p class="comment-form-mobileno">
        <label>Mobile No.<span>(required)</span></label>
        <span class="icon-input">
        <input type="text" name="mobileno" required />
        </span> </p>

        <p class="comment-form-comment">
        <label>Message<span>(required)</span></label>
        <textarea name="msg">
        </textarea>
        </p>
        <p class="form-submit">
        <input type="submit" value="submit" name="submit">
        </p>
    </form>

when I click submit it shows me "Sorry, your mail was not sent kindly try again later."

Roy Bogado
  • 4,299
  • 1
  • 15
  • 31
  • Try adding the mail error ...`error_get_last()['message'];` to check whats wrong – Roy Bogado Mar 22 '19 at 09:55
  • Check your spam folder and also the inbox for the account that sends the emails for mailer daemon return-to-sender messages. `mail()` does not send email from a SMTP account and a lot of mail servers will reject these messages as they don't pass all the spam checks. – tshimkus Mar 22 '19 at 10:47
  • Here's a very thorough answer about troubleshooting PHP's `mail()` function: https://stackoverflow.com/a/24644450/1141944 – tshimkus Mar 22 '19 at 10:49
  • @tshimkus but it worked before and not working now only!!, what is the need to check my spam folder? when it's saying "Sorry, your mail was not sent kindly try again later." – Vigneshwaran Mar 22 '19 at 11:33
  • @VigneshWaran, I misread your question. The tile says "php mail function works but...". The function is returning false so I didn't interpret that as working. Note that a FALSE return value on the `mail()` function can mean the mail was not accepted for delivery, so it's worth at least checking the inbox for the account sending the mail for a mailer daemon bounceback message – tshimkus Mar 22 '19 at 11:41
  • tried but it shows nothing @Roy – Vigneshwaran Mar 23 '19 at 08:40

1 Answers1

0

i would suggest you use PHPMailer for the same. For that you need to download the PHPMailer lib. It is available in github. Place the lib in your web-server folder. inside your if(isset($_POST['submit'])){ } write and change according to your need

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;// authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";//You can also use other smtp such as outloook<br> (oulook.office365.come)
$mail->Port = 465; // or 587 and 25 for outlook
$mail->IsHTML(true);
$mail->Username = "email@gmail.com";//Your Email Address
$mail->Password = "password";//Your Password
$mail->SetFrom("example@gmail.com");//Again Your Email email address
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddCC("CC@gmail.com");//if any cc (Set in if condifition if no cc)
$mail->AddAddress("email@gmail.com");//Email Address of the person you want to send to

if(!$mail->Send()) {
echo error here
} else {
echo "Message has been sent";
}