3

My mail function is on online server. It was working fine. But not now. I am confused why it has stopped sending mail. It shows the message that email sent, but no email is received in inbox.

Here is my php code:

<?php
if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];

    $headers = "From: " . $email . "\n"; //from address
    $to = ""; //my mail id
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1 \n";
    $subject = "Test mail";

    $body = '<html>
        <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8" />
            <title>"."<h3>"."Hello Test,."</h3>".
    </title>
        </head>
        <body><p></p>
            <p style="color: #00CC66; font-weight:600; font-style:italic; font-size:14px; float:left; margin-left:7px;">You have received an inquiry from your website.  Please review the contact information below.

    :</p>';
    if (mail($to, $subject, $body, $headers)) {
        ?>
        <h3 style="color:#d96922; font-weight:bold; height:0px; margin-top:1px;">Thank You For Contacting us!!</h3>

        <?php
    } else {

        print_r(error_get_last());
    }
}

What's wrong with it? Please help me to find out the solution. And also help me to echo error?

9
  • 3
    Can't find your mail() code anywhere .... Feb 17, 2014 at 7:28
  • i update the code please help me for this issue..it's very urgent..
    – Priya jain
    Feb 17, 2014 at 7:32
  • seems that you have a configured mail server in your server? Feb 17, 2014 at 7:34
  • 1
    Could be lots of things. I'm assuming you are getting no errors? It could be your spam filter or your server is queueing the messages.
    – SyntaxLAMP
    Feb 17, 2014 at 7:37
  • @Priyajain, Did you check your spam folders ? Feb 17, 2014 at 7:37

3 Answers 3

13

I'd give this a try, ending each line with \r\n and also adding a To header (redundant as it might seem). Also, declaring charset=utf-8 in the header should be enough. If not though, make sure it matches (right now there's a mismatch).

<?php
  $subject = "Test mail";
  $to_email = "[email protected]";
  $to_fullname = "John Doe";
  $from_email = "[email protected]";
  $from_fullname = "Jane Doe";
  $headers  = "MIME-Version: 1.0\r\n";
  $headers .= "Content-type: text/html; charset=utf-8\r\n";
  // Additional headers
  // This might look redundant but some services REALLY favor it being there.
  $headers .= "To: $to_fullname <$to_email>\r\n";
  $headers .= "From: $from_fullname <$from_email>\r\n";
  $message = "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">\r\n
  <head>\r\n
    <title>Hello Test</title>\r\n
  </head>\r\n
  <body>\r\n
    <p></p>\r\n
    <p style=\"color: #00CC66; font-weight:600; font-style: italic; font-size:14px; float:left; margin-left:7px;\">You have received an inquiry from your website.  Please review the contact information below.</p>\r\n
  </body>\r\n
  </html>";
  if (!mail($to_email, $subject, $message, $headers)) { 
    print_r(error_get_last());
  }
  else { ?>
    <h3 style="color:#d96922; font-weight:bold; height:0px; margin-top:1px;">Thank You For Contacting us!!</h3>
  <?php
  }
  ?>

I'd also replace the first line with something like this, to avoid notices / errors:

if ($_POST && isset($_POST['submit']) && isset($_POST['name']) && isset($_POST['email']) && !empty($_POST['name']) && !empty($_POST['email'])) {
0
1

Working with godadday server it works as expected, but on bigrock server it will not work till we did not create a respond mail ( registered mail from cpanel). Login to your cpanel provided by big rock, scroll down to register a mail id. create that mail id e.g [email protected].

Once you will create that mail id wait for 10-30 minutes after that mail function will start working.

for more details read this article

-1

Had a similar challenge, on my end however the error was not on the code, i had missed creating the actual sent from email on cpanel. Its an easy thing to miss but can frustrate you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.