0

I'm trying to send a message to the email a user provides in the contact form. The problem is the message never gets sent, but I always arrive at a blank page where my php code is located. Nothing warns me of any error in my code. Can anyone explain why this is happening and offer a solution to the problem?

      <form action="site.php" method="POST">
      <input
        type="text"
        class="form"
        name="email"
        placeholder="Your email address"
      />
      <button class="submit" type="submit">Join Waitlist</button>
    </form>
<?php
if (isset($_POST["submit"]))
{
    $mailTo = $_POST["email"];
    $mailFrom = "Dumele";
    $message = "https://docs.google.com/forms/d/1lpj2XnKW4HT_qHFfGwpUxcvzPmK2USZ0MGSDP0XCqfg/edit";
    $subject = "Welcome to Dumele";
    
    $txt = "Thank you for your interest in Dumele. We're glad to have 
    you join our network and mission to enhance the technological 
    innovation of our African diaspora. Below is a link to a survey 
    we would like you to answer so we can better assist you.\n\n".message;

    $headers = "From: ".mailFrom;

        (mail($mailTo, $subject, $txt, $headers));
        header("Location: index.php?mailsend");
    
}   

?>

Patrick Dankyi
  • 99
  • 1
  • 1
  • 7
  • Can you clarify what you mean by "a blank page where my php code is located"? Are you seeing PHP code in your browser? – David Aug 05 '21 at 18:16
  • 1
    You need `name="submit"` in your submit button otherwise the first `if` in the PHP will never be true. – ADyson Aug 05 '21 at 18:20
  • @David no I don't see any php code. the browser just takes me to the separate file where my php code is located once I click the submit button. – Patrick Dankyi Aug 05 '21 at 18:21
  • Also your mail from field needs to include a proper email address. Please find some examples of the usage of mail() and study them carefully – ADyson Aug 05 '21 at 18:21

3 Answers3

1

This is looking for a form value with the name "submit":

if (isset($_POST["submit"]))

But there's no form element in the HTML with that name. So this will always be false. Give your submit button that name:

<button class="submit" type="submit" name="submit">Join Waitlist</button>

It shouldn't necessarily need a value, it would just default to an empty string. But it needs a name in order for the browser to send anything at all with that key.


As an aside, your mail server may reject the message since this is not really an email address:

$mailFrom = "Dumele";

For completeness... It looks like your PHP variables are also syntactically incorrect. Variable names need to begin with a $. For example, this:

$headers = "From: ".mailFrom;

Should be this:

$headers = "From: ".$mailFrom;

The same error would need to be corrected anywhere you're mis-using variable names.

David
  • 208,112
  • 36
  • 198
  • 279
1

First of all make sure you enabled error reporting. You can check another Stackoverflow question and it's answers here about it.

As I see in your code you have syntax errors. You didn't place $ sign before variable names. For example you typed $headers = "From: ".mailFrom; instead of $headers = "From: ".$mailFrom; Let's fix it:

<?php
if (isset($_POST["submit"]))
{
    $mailTo = $_POST["email"];
    $mailFrom = "Dumele";
    $message = "https://docs.google.com/forms/d/1lpj2XnKW4HT_qHFfGwpUxcvzPmK2USZ0MGSDP0XCqfg/edit";
    $subject = "Welcome to Dumele";
    
    $txt = "Thank you for your interest in Dumele. We're glad to have 
    you join our network and mission to enhance the technological 
    innovation of our African diaspora. Below is a link to a survey 
    we would like you to answer so we can better assist you.\n\n".$message;

    $headers = "From: ".$mailFrom;

        (mail($mailTo, $subject, $txt, $headers));
        header("Location: index.php?mailsend");
    
}   

Now with the mail() function of PHP; some servers disables mail() function for security purposes. If so; you can use SMTP to securely send your emails. To use SMTP in PHP of course you need additional processes but some free software packages and libraries like PHPMailer or SwiftMailer can help you about it.

Uğur Arıcı
  • 1,180
  • 1
  • 10
  • 16
0

Use value attribute in button tag. You are testing if(isset($_post['submit']))

But what is submit? You should use value attribute and give a value submit i.e. Submit

  • if your question is "How to send an email using PHP", then, you can either use the PHP mail function mail() or you can use PHPMailer library. – Asuquo12 Aug 07 '21 at 21:57