-1

I have been trying to build my own website and a central part of it is a form. I have set up an email address to recieve form results. I have been able to run the website on localhost and everything but the forms are working. When I click submit, it sucsessfully sends me to the PHP file and I am not recieving any emails from the file. I have checked my spam and it is empty. I am brand new to PHP and I tried to adapt some code I found online. I am not sure if there is anything wrong with my code or not. If anyone could help that would be great. I am also trying to be able to send an image file through the form. This is a screenshot of what the server log says when I try to submit the form. Server Log Screenshot This is the code I have wrote:

<?php
$title = htmlspecialchars($_POST['title']); 
$coordinates = htmlspecialchars($_POST['coordinates']);
$description = htmlspecialchars($_POST['description']); 
$shelterorcampsite = htmlspecialchars($_POST['shelter-campsite']);
$distanceToWater = htmlspecialchars($_POST['howfarawayiswater']);
$image = $_POST['file'];

$email_from = 'contactbackpackingproject@gmail.com';

$email_subject = "New Form submission";

$email_body = "There has been a new campsite submission. 
The title is: $title;
The type is: $shelterorcampsite;
The coordinates are: $coordinates;
The description is: $description;
The distance to water is: $distanceToWater;
An image is: $image";

$to = 'contactbackpackingproject@gmail.com';
  
        
        mail($to,$email_subject,$email_body,$headers);
  ?>

This is a screenshot of the code is VS code: VS code screenshot

EvanB101
  • 19
  • 6
  • It isn't trivial to send email from localhost (https://stackoverflow.com/questions/15267423/php-send-mail-in-localhost). You'll have better luck connecting to a 3rd party email service (like mailgun.com) and triggering an email to be sent from their servers. Searching the web for `php send email localhost` might help give you some more direction though. – WOUNDEDStevenJones Jan 02 '22 at 02:05
  • I am trying to use localhost to just test the website then deploy to a cloud server. So even if my code works I might not get an email becuase I am running on localhost? – EvanB101 Jan 02 '22 at 02:08
  • 1
    Yes, it won't work because it's localhost. The mail function tries to dump mail to a mail transfer agent running on your workstation. Usually you won't have one. Even if you set one up, it is likely to not be able to send mail to legitimate mail servers, because the things you need set up in DNS to legitimize it, won't exist. It's best to test to something like [mailhog](https://hub.docker.com/r/mailhog/mailhog/) using the mailhog docker image – gview Jan 02 '22 at 03:10
  • Ok, Thank you. That solves my problem – EvanB101 Jan 03 '22 at 00:40

1 Answers1

0

Often it's helpful to find the exact error message that is triggered by the mail() function. While the function doesn't provide an error directly, you can use error_get_last() when mail() returns false.

<?php
$success = mail('example@example.com', 'My Subject', $message);
if (!$success) {
    $errorMessage = error_get_last()['message'];
}
?>

Please identify the errors as without them nobody can help you and the log that you provided is just a POST request which doesn't necessarily indicate that mail function executed successfully.

Vasu Grover
  • 114
  • 6