-3

PHP mail function is not working, I can't get email from my domain test@test.com. It worked perfectly before few days back, but not now.

Tried every solutions on Stack Overflow but still not resolved.

HTML form code : 1.html

<form method="post" name="from" action="2.php">
Company Name : <input type="text" name="cname"><input type="submit" value="submit" name="submit">
</form>

PHP mail code : 2.php

<?php
if(isset($_POST['submit']))
{
    $cname=trim($_POST['cname']);       
    //$cname='"'.$cname.'"';
    echo $cname;
     /* mail to Admin */
        $to = 'test@test.com';
        $subject = 'Details : From $cname';

        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers = 'From: $cname';

        $message = 'Company name : $cname';
    if(mail($to, $subject, $message, $headers))
        {

                echo "Mailer sent!";
            } else {
                echo "Message Error:";
            }
}
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Viraj
  • 3
  • 3
  • what type of content you are sending actually. make sure you are using corresponding headers for that. OR check for server email configuration may be SMTP works. – Deep Kakkar Mar 03 '16 at 06:16
  • i am just sending normal plain email, in $headers just sending company name that it. before few days i got email that time n today,SMTP config setting are same, no ant changes done in SMTP setting. – Viraj Mar 03 '16 at 06:27
  • If you are trying on localhost, make sure you have a mail server set up. Placing the following code at the top of your PHP files will enable error reporting. error_reporting(-1); ini_set('display_errors', 'On'); set_error_handler("var_dump"); – Priya jain Mar 03 '16 at 06:29
  • @Priyajain, done as suggested, but can't get error too, only cname displays. Its live not on local. – Viraj Mar 03 '16 at 06:39
  • Hi, you have actually echoed "Mail Error:" when its actually true that is if it sends mail and used Mail sent message for error that is false case of if. Make sure you use a real existing to address email. – MidhunKrishna Mar 03 '16 at 06:50
  • It has to be an issue with the environment that you are testing it in then. The only thing that was stopping me from receiving an email when I tested it was the extra space in the header variable. The concatenation issue that I mentioned and the reversed error output that you originally had wouldn't stop the email from being sent/received. – Son Mar 03 '16 at 06:59
  • talking about environment, my server located on godaddy, asked them also, no setting has changed on their side, but my main query is, it works before a week but not now, why??. i am very frusted why email function is not working...@Son tried every possible solution discussed on here.. – Viraj Mar 03 '16 at 07:06
  • Guys, plz apologize i am not getting solution, why you are marking negative on my question????? where i am going wrong....? – Viraj Mar 03 '16 at 07:11
  • @Viraj - Try speaking with godaddy support. It seems like an issue with them. A quick google search brings up people complaining about the use of mail() on godaddy hosting. I definitely wish you the best of luck with this. – Son Mar 03 '16 at 07:12
  • Okey, Thank much @Son. will contact and also will check complains about email functionality... – Viraj Mar 03 '16 at 07:18
  • The "urgent" may have been the cause of your downvotes. Remember that as soon as you present your problem to volunteers, it ceases to be urgent. I usually downvote these myself, but -3 is enough. – halfer Mar 03 '16 at 08:21

2 Answers2

0

EDIT2:

Here is the exact code that I have tested and is working.

  <?php
    if(isset($_POST['submit']))
    {
        $cname=trim($_POST['cname']);       
        //$cname='"'.$cname.'"';
        echo $cname;
         /* mail to Admin */
            $to = 'test@test.com';
            $subject = 'Details : From '.$cname;
            $headers = 'From: '.$cname;
            $message = 'Company name : '.$cname;
        if(mail($to,$subject,$message,$headers))
            {

                echo "Mailer sent!";
            } else {
                echo "Message Error:";
            }
    }
    ?>

The only other thing that I changed aside from the extra space in the header variable, was the concatenation of the variable with the input data ($cname).

The email would still send without the concatenation change, but it would display "$cname" instead of the entered data.


EDIT: For the header variable:

 $headers = 'From : $cname';

Should actually be:

 $headers = 'From: $cname';

I didn't notice the extra space between From and : originally, but I noticed it after I tested it. Email was not received unless the space was removed.


Try this.

Also, the if statement for the mail function will trigger as true if the message is sent. So you want to swap the echo lines as the "error" output should be under the else portion.

  <?php
    if(isset($_POST['submit']))
    {
        $cname=trim($_POST['cname']);       
        //$cname='"'.$cname.'"';
        echo $cname;
         /* mail to Admin */
            $to = 'test@test.com';
            $subject = 'Details : From $cname';
            $headers = 'From: $cname';
            $message = 'Company name : $cname';
        if(mail($to,$subject,$message,$headers))
            {

                echo "Mailer sent!";
            } else {
                echo "Message Error:";
            }
    }
    ?>
Son
  • 193
  • 5
  • swapping error message won't resolve issue...@Son – Viraj Mar 03 '16 at 06:35
  • @Viraj The part about the error messages wasn't meant to solve the issue. It was meant to fix the output. Check out the space between From and : in the header variable. – Son Mar 03 '16 at 06:37
  • removing extra space, :( tried, not worked. – Viraj Mar 03 '16 at 06:43
  • Check for my edit on my answer - I am pasting in exactly what I have that is working for me. – Son Mar 03 '16 at 06:46
  • i pasted it in code, it shows me "mailer sent" but actually i dont get email. – Viraj Mar 03 '16 at 06:59
  • I am assuming that you replace test@test.com with an actual email, and you are checking your spam mailbox, yes? – Son Mar 03 '16 at 07:00
0

Your 2.php should be like this: ( You were printing message at wrong places)

$to should be some working email address where you can check emails. Also it will work on LIVE server. Mostly not worked with localhost.

<?php
if(isset($_POST['submit']))
{
    $cname=trim($_POST['cname']);       
    //$cname='"'.$cname.'"';
    echo $cname;
     /* mail to Admin */
        $to = 'test@test.com';  
        $subject = 'Details : From $cname';
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: ' . strip_tags($cname) . "\r\n";
        $message = 'Company name : $cname';
    if(mail($to,$subject,$message,$headers))
        {
           echo "Message sent!"; 
        } else {

            echo "Mailer Error: ";
        }
}
?>
Atif Tariq
  • 2,650
  • 27
  • 34
  • Ohh yes, its corrected now, echo has changed. Atif, $to is my working id only and i tried it on live server only not on localhost. but still can't get email. – Viraj Mar 03 '16 at 06:31
  • Edited my code. I added full headers. try now plz. – Atif Tariq Mar 03 '16 at 06:49