0

I'm working on a project that requires a specific button on a page to send an email out. It works as a sort of reminder function for the client, so when it's clicked they will receive an email to let them know that they need to check something.

I think I almost have it working, but for some reason it keeps failing instead of actually sending the email out. It could be the way that I have it set out, but I have tried using the "action" on the form to run the php through a seperate file, but it still does the same. I currently have it like this:

<form method='post'>
    <button type='submit' name='reminder_btn'>
        Send Reminder
    </button>
</form>

<?php

if(isset($_POST['reminder_btn'])){

    $email = "hello@email.com";
    $subject = "Company Reminder";

    $message = "

    <html>
    <head>
        <title>Company</title>
    </head>

    <body>

        <p>Hello!</p>

        <p>You have a reminder. Please check the website.</p>

        <p>Thanks!</p>

    </body>
    </html>

    ";

    $mailheaders[] = "From: Company Name";
    $mailheaders[] = "Reply-To: hello@email.com";
    $mailheaders[] = "MIME-Version: 1.0";
    $mailheaders[] = "Content-type: text/html; charset=iso-8859-1";

    if(mail($email, $subject, $message, implode("\r\n", $mailheaders))) {

        echo "Reminder sent.";

    } else {

        echo "Something has gone wrong. Try again.";

    }

}

I'm sure I've had it working like this on something else before, but all I'm getting is the "Something has gone wrong" message each time.

Obviously I've got something wrong but I don't understand what it is, so any assistance here would be much appreciated.

1 Answers1

0

You forgot the action on the form. I have used PHP SELF but you can replace it with your page. PHP SELF sends the POST data to the same page.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<button type='submit' name='reminder_btn'>
    Send Reminder
</button>
John Conde
  • 217,595
  • 99
  • 455
  • 496
Pistone Sanjama
  • 489
  • 1
  • 4
  • 14
  • There's no need, though - if the action is blank or missing it automatically sends the post data to the same page. – droopsnoot Jul 10 '20 at 11:55