0

I have a simple code, which should echo 1 if the mail is sent or 0 if it doesn't. But I am getting error 500. I tried changing the email, but it didn't work. Please suggest a fix.

<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <div class="container-fluid">
            <h1>Emails:</h1>
            <p>
                <?php
                $to = "myemail@gmail.com"
                $subject = "Subject";
                $message = "This is a message";
                echo mail($to, $subject, $message);
                ?>
            </p>
        </div>
    </body>
</html>

error

Synchro
  • 35,538
  • 15
  • 81
  • 104
Purva Shukla
  • 74
  • 1
  • 7
  • `$to = "myemail@gmail.com"` is missing a `;`. Please read [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – brombeer Jul 09 '20 at 20:25
  • what do you expect echo mail($to, $subject, $message); to do? – DCR Jul 09 '20 at 20:26
  • Any fatal PHP errors will appear in your web server's error log. – Synchro Jul 09 '20 at 20:44
  • The ```;``` solved my HTTP error 500, and I replaced ```echo``` with ```var_dump()```, and I get ```bool(false)```, what can I do to make it ```bool(true)```, so that the mail gets sent. Thanks for the help. – Purva Shukla Jul 10 '20 at 07:54

1 Answers1

0

My guess is you lack a ; after setting the $to variable, causing a parse error that you don't see because you the errors are not shown.

 <?php
      $to = "myemail@gmail.com";
      $subject = "Subject";
      $message = "This is a message";
      echo mail($to, $subject, $message);

You may want to enable error display for debugging.

Also, since the mail() function returns a boolean, it will show nothing on failure if you echo it. You should use var_dump() instead of echo in order to have the result shown for debugging purpose.

Divarrek
  • 325
  • 3
  • 11
  • 1
    This solved my HTTP error 500, and I replaced ```echo``` with ```var_dump()``` and that gives me ```bool(false)```, what should I do for the mail to get sent and to get ```bool(true)```. Thanks for the help. – Purva Shukla Jul 10 '20 at 07:56
  • There can be a lot of problems that make the mail() function fail. See this question to know more about it: [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Divarrek Jul 10 '20 at 08:03
  • When the answer is "add one missing semi-colon", there is no need to answer. Just leave a comment under the question and flag/vote to close as Off-topic: Typo. This page will be quickly scrubbed from the site (as will any upvote points you receive) because this page is completely useless to researchers. See: [Should one advise on off topic questions?](//meta.stackoverflow.com/q/276572/1768232). – John Conde Jul 11 '20 at 11:55