0

I have searched and searched for the answer and cannot find the answer to my particular problem. I am simply trying to send an email when a user fills out a contact page and clicks submit.

Below here is the code for the form.

<form method="post" action="contactRob.php">
    <label for="name">First Name:</label>
    <input type="text" name="name" id="name" />                         
    <label for="lastName">Last Name:</label>
    <input type="text" name="lastName" id="lastName" />
    <label for="email">Email:</label>
    <input type="text" name="email" id="email" />
    <label for="message">Message:</label><br />
    <textarea name="message" rows="20" cols="20" id="message"></textarea>
    <label>*What is 2+2? (Anti-spam)</label>
    <input name="human" placeholder="Type Here">

    <input type="submit" name="submit" value="Submit" class="submit-button" />

</form>

And below here is the PHP I am using.

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = $_POST['lastName']; 
    $to = 'juliansilvestri92@gmail.com'; 
    $subject = 'Hello';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit'] && $human == '4') {         
        if (mail ($to, $subject, $body, $from)) { 
           echo '<p>Your message has been sent!</p>';
        } else { 
           echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
?>

What am I missing? Currently this website is live on a hosted server domain. When I click the submit button I am getting 'something went wrong' error throw that I placed.

Any help would be greatly appreciated.

DaFois
  • 2,197
  • 8
  • 26
  • 43
Julian Silvestri
  • 1,970
  • 1
  • 15
  • 33
  • 3
    Your HTML inputs names begin with an upper case letter, i.e. ``Name``, and in your PHP code you use lower case letter, i.e. ``name``. Make these consistent and it should work for you. – alistaircol Aug 28 '18 at 12:42
  • Simply start looking into your http servers error log file. That is where php logs issues to. – arkascha Aug 28 '18 at 12:43
  • http://php.net/manual/en/function.mail.php - pay attention to examples #2 or #3 in particular where they show how to add headers for things like the From field. Also might be worth checking with your host whether the mail() functionality is actually set up and permitted on their environment. – ADyson Aug 28 '18 at 12:45
  • After fixing the uppercase and lower case differences I am still having the same problem – Julian Silvestri Aug 28 '18 at 12:46

2 Answers2

3

Your names are first capital while results arent.

$message = $_POST['email']; 
<input type="text" name="Email" id="Email" />

make both lowercase. Then echoing $body could help, see if the data is the same as you inputed, comment out the mailer part, see if the error comes from mail or from post DOM itself. If its mailer problem that's causing you the error, read about mailer a bit more, if that doesn't help.

and your from is incorrect.

$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$txt,$headers);

Your from should look like this:

$from = "From: ".$_POST['email']."\r\n CC: ".$_POST['lastName']; 

or something like that.

There is all ready similar topic about it, and there is a honest huge comment, you wont miss it. PHP mail function doesn't complete sending of e-mail Do what that comment is telling you to do, and you will most likely solve or understand the problem.

Good luck..

Thomas J.
  • 593
  • 8
  • 21
1

Most of the server not allow you to send email using just php mail function use php mailer function to send mail from any where local development server/ live web server.

https://github.com/PHPMailer/PHPMailer

also for default mail send you can use these params

$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$txt,$headers);
Fahad Bhuyian
  • 300
  • 2
  • 10