0

I am trying to send an email from my live server (Godaddy hosting) using the PHP mail function I have written the script but when I am trying to open the mail.php file on my browser it isn't opening and throws an error This page isn’t working prac.shayankanwal.com is currently unable to handle this request. HTTP ERROR 500 (I am not using any forms just directly opening that file) also, it is not Sending the email I have been trying to figure this out for the last 6 hours reading about that issue on StackOverflow but nothing seems to work also, another thing Does this mail function works without an SSL certificate or not?

here is the link to live server and below is the script I have written which is also on the live server

<?php

$to = "shayankanwal667@gmail.com";
$subject = "Test mail";
$message = "Hi This is  a test email you received from server";
$from = "clientsupport@shayankanwal.com";
$headers = "From: $form";
if(mail($to,$subject,$message,$headers)){
    echo "Email sent";
}else{
    echo "Email NOT sent"
}

?>
Shayan Kanwal
  • 542
  • 4
  • 15
  • 1
    It's not Javascript it's PHP :-) But looks like you have typo in $headers = "From: $form"; should'nt it be $headers = "From: $from"; – grisuu Dec 14 '22 at 13:11
  • 1
    Please use a higher error reporting level and share the error message you are facing. Also, share how this problem is related to Javascript or HTML, or remove these tags – Nico Haase Dec 14 '22 at 13:13
  • 1
    `Does this mail function works without an SSL certificate or not`...that depends on whether the mailserver it's connecting to requires SSL or not. – ADyson Dec 14 '22 at 13:18
  • 1
    500 Internal Server Error is a generic error message informing you that the server crashed while processing the request. Beyond that, it's (intentionally) meaningless, and is of very little use for debugging. You need to check the error logs on the server to try and find the underlying exception message. Once you've got that, you stand a chance of identifying the problem. See also [How can I make PHP display the error instead of giving me 500 Internal Server Error](https://stackoverflow.com/questions/2687730/how-can-i-make-php-display-the-error-instead-of-giving-me-500-internal-server-er) – ADyson Dec 14 '22 at 13:18
  • 1
    https://stackify.com/php-error-logs-guide/ also explains how to enable error logging, if your server doesn't have that switched on already. In a live environment, you should always prefer logging to a file over showing detailed errors on-screen, for security reasons. – ADyson Dec 14 '22 at 13:21
  • 1
    And in regards to the syntax error that you appear to have, please read [PHP parse/syntax errors; and how to solve them](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them). Fix that, and then if you have a further problem after that, please [edit] your question with the revised code and a clear problem description, including any error details. – ADyson Dec 14 '22 at 13:22
  • 1
    https://www.atatus.com/blog/debugging-in-php/ also has a simple guide to general debugging with PHP, which I strongly suspect you would find useful as a next step. – ADyson Dec 14 '22 at 13:30

1 Answers1

4

FATAL ERROR syntax error, unexpected token "}", expecting "," or ";" on line number 12

You have missed a ; on line 12

}else{
    echo "Email NOT sent";
}

Also you have a Typo on line 7:

$headers = "From: $form";

should be:

$headers = "From: $from";

The restored code should look like this:

<?php

$to = "shayankanwal667@gmail.com";
$subject = "Test mail";
$message = "Hi This is  a test email you received from server";
$from = "clientsupport@shayankanwal.com";
$headers = "From: $from";
if(mail($to,$subject,$message,$headers)){
    echo "Email sent";
}else{
    echo "Email NOT sent";
}

?>

Keep in mind that DEBUGGING in php is simple by adding the following lines to the top of your code (below <?php ofcourse.):

error_reporting(E_ALL);
ini_set("display_errors", 1);

Error 500's are Internal Server Errors (Fatal Errors), meaning there is a mistake in your code. You can debug them by using the code above.

Ladineko
  • 1,921
  • 1
  • 16
  • 25
  • thanks for your reply this isn't solving the problem – Shayan Kanwal Dec 14 '22 at 13:18
  • 2
    @ShayanKanwal What error are you getting when adding the reporting. – Ladineko Dec 14 '22 at 13:20
  • 1
    @ShayanKanwal I've updated my answer with all the mistakes i can find. Please review them and let me know the error that returns if you still run into issues. – Ladineko Dec 14 '22 at 13:28
  • Thanks a lot Ladineko I fixed the typo on line 7 and the mail function is working Properly!! and I received the email I was trying to fix that for the last 6 hours and the issue was so simple!!! Thanks again – Shayan Kanwal Dec 14 '22 at 13:36