-2

I am a novice to php but go code from the web and had edited it. The form is a contact form on my website and is supposed to send me an email. But after the edit, it is unable to email me after several tries.

HTML FORM

<h3>Use This Contact Form To Reach Us</h3>
<form action="thankyou.php" method="post">
    <input type="text" name="name" id="name" maxlength="30" placeholder="Full Name">
    <input type="email" name="email" id="email" maxlength="50" placeholder="Email">
    <input type="text" name="phone" id="phone" maxlength="10" placeholder="Phone">
    <textarea placeholder="Message" name="message" id="message"></textarea>
    <input type="submit" id="submit" value="Submit">
</form>

This is the php page.

thankyou.php

<?php
if ($_POST["submit"]) {
    $recipient   = "marketing@smartpay.com";
    $subject     = "MESSAGE FROM ONLINESMARTPAY.COM";
    $sendername  = $_POST["name"];
    $senderemail = $_POST["email"];
    $senderphone = $_POST["phone"];
    $message     = $_POST["message"];
    $mailBody    = "FullName: $sendername\nEmailAddress: $senderemail\nPhoneNumber: 

$senderphone\n\n$message";
    mail($recipient, $subject, $mailBody, "From: $sender <$senderemail>");
}
?>
Kulasangar
  • 9,046
  • 5
  • 51
  • 82
  • If the original from the web works and yours doesn't, try breaking the problem down to troubleshoot the error. – JustBaron Feb 06 '17 at 09:26
  • I suspect this :`"From: $sender <$senderemail>"` – Masivuye Cokile Feb 06 '17 at 09:30
  • 1
    Are you getting any errors? Have you checked that `$_POST["submit"]` is set to something that evaluates to `true`? It'd be nice if you could provide some more information on what you've tried to debug the issue. – madsroskar Feb 06 '17 at 09:32
  • 1
    Also, as @MasivuyeCokile points out, in your header parameter, you set `$sender`, and not `$senderemail` after "From:" Which as far as I can see never is declared. – madsroskar Feb 06 '17 at 09:34
  • 3
    Where is `$sender` assigned? – Progrock Feb 06 '17 at 09:39

1 Answers1

0

One of the reason that I suspect the $_POST['submit'] does not exist in your form you don't have any input type with name=submit You should add the name=submit attribute on your submit button currently your submit button looks like:

<input type="submit" id="submit" value="Submit"> instead of <input type="submit" id="submit" value="Submit" name="submit">

Then on your email you need to set the headers.

<h3>Use This Contact Form To Reach Us</h3>
<form action="thankyou.php" method="post">
    <input type="text" name="name" id="name" maxlength="30" placeholder="Full Name">
    <input type="email" name="email" id="email" maxlength="50" placeholder="Email">
    <input type="text" name="phone" id="phone" maxlength="10" placeholder="Phone">
    <textarea placeholder="Message" name="message" id="message"></textarea>
    <input type="submit" id="submit" value="Submit" name="submit">
</form>

thankyou.php

<?php
if ($_POST["submit"]) {
    $recipient   = "marketing@smartpay.com";
    $subject     = "MESSAGE FROM ONLINESMARTPAY.COM";
    $sendername  = $_POST["name"];
    $senderemail = $_POST["email"];
    $senderphone = $_POST["phone"];
    $message     = $_POST["message"];
    $headers     = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= 'From:' . $sendername . '<' . $senderemail . '>' . "\r\n";
    $mailBody = "FullName: $sendername\nEmailAddress: $senderemail\nPhoneNumber:$senderphone\n\n$message";
    if (mail($recipient, $subject, $mailBody, $headers)) {
        echo "email sent";
    } else {
        echo "could not send email"; // something wrrong with your server config
    }
}
?>
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34