0

This might seem like such an amateur thing, but I am an amateur when it comes to php...

Basically a website I'm creating for an organisation I'm a part of needs a contact form, because I'm always trying to learn I'd prefer to have made the code myself rather than just get some plugin. However my code doesn't quite seem to work.

Also first post, I'm pretty sure this isn't a duplicate due to it being different code, I have been using posts on this site to try get it to work but no luck, so thought best to post my code and hopefully people with more brain cells than me can help

Here's the html for the page itself:

<form method="post" action="contactengine.php">
    <label for="Name"><h2>Your Name:</h2></label>
    <input type="text" name="Name" id="Name" placeholder="Please Enter Your Name"/>

    <label for="Email"><h2>Your Email:</h2></label>
    <input type="email" name="Email" id="Email" placeholder="Please Enter Your Email Address"/>

    <label for="Message"><h2>Message:</h2></label><br />
    <textarea name="Message" rows="20" cols="20" id="Message"></textarea>

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

The PHP:

<?php

    $EmailFrom = "webmaster@temporary.co.uk";
    $EmailTo = "webmaster@temporary.co.uk";
    $Subject = "Contact Form Submission";
    $Name = Trim(stripslashes($_POST['Name'])); 
    $Email = Trim(stripslashes($_POST['Email'])); 
    $Message = Trim(stripslashes($_POST['Message'])); 

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // validation
    $validationOK=true;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
      exit;
    }

    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $Email;
    $Body .= "\n";
    $Body .= "Message: ";
    $Body .= $Message;
    $Body .= "\n";

    // send email 
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

    // redirect to success page 
    if ($success){
      print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
    }
    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    }
    ?>

I feel quite stupid because there's loads of literature on creating working contact forms but none of them seem to work

I'm hosted on 1and1 if that makes any difference?

Thanks in advance

EDIT: Okay I forgot to mention the problem: emails just aren't delivering, so I don't actually know if success is working or not, I've tried different email addresses, checked spam the whole shabang - so I'm struggling to work out what's going wrong on it

Andy
  • 1
  • 2
  • what exactly doesn't work??? where is the problem? is it an error? what is it? – Moher Sep 07 '17 at 23:05
  • Any errors? Any logs? What is the value of `$success`? – Mike S Sep 07 '17 at 23:06
  • FYI, instead of rendering HTML to perform redirects, take a look at the `header` function. E.g. `header('Location: contactthanks.php');` – fubar Sep 07 '17 at 23:08
  • Forgot to add the problem - I don't know if the form is working because no emails are getting delivered, regardless of what address is put into the php – Andy Sep 07 '17 at 23:09
  • it is not difficult to find out the problem yourself. First check the value of submitted variables ($name, $email). If they are empty then there is a problem in the form being submitted. If that is working, check the value of $success to find out if the email is sent correctly – Silencer310 Sep 07 '17 at 23:09
  • Which mail service are you using? – muasif80 Sep 07 '17 at 23:12
  • That is have you configured any mail service in your configurations? – muasif80 Sep 07 '17 at 23:13
  • I'm using the 1and1 mail service I believe it's fully set up. I've just tried submitting the form with no data input and it took me to the success rather than error page I really don't know php, I'm a graphic designer delving into the world of code and my first project is involving far more than I hoped for aha – Andy Sep 07 '17 at 23:18
  • Possible duplicate of [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – showdev Sep 07 '17 at 23:23
  • You realise that you're not actually validating the input, right? – fubar Sep 07 '17 at 23:27

1 Answers1

0

Don't use the built-in mail command. You are going to want to use something like Swift Mailer as a mail() replacement, and a mailing service like mailgun.org to send mails.

The built-in mail command may be blocked on the server. Or, since you're not sending from a server that is designed to send emails, the emails may be blocked at the SMTP level by someone else's blacklist. Or maybe they're in your spam box.

You may want to install MailHog on your server and use that as an SMTP server to test whether the mail() command even works on the server.

user176717
  • 76
  • 7
  • Thanks, I'll definitely take a look at those- might explain why the emails don't send because as far as I can tell the fields are correctly set up – Andy Sep 07 '17 at 23:28