0

I've tried out a few PHP contact form tutorials but none seem to work for me. I'm not sure what I'm doing wrong. I tested it in localhost and nothing, so I went ahead and hosted it to see if that would work but still nothing.

HTML

<form class="form" action="form_process.php" method="post" name="contact_form">
  <p class="name">
    <label for="name">Name</label><br>
    <input type="text" name="name_first" id="name" placeholder="First" />
    <input type="text" name="name_second" id="name" placeholder="Last"  />

  </p>

  <p class="email">
    <label for="email">Email</label><br>
    <input type="text" name="email" id="email" placeholder="mail@example.com" />
  </p>

  <p class="text">
    <label for="email">Comments</label><br>
    <textarea name="text" placeholder="Write something to us" /></textarea>
  </p>

  <p class="submit">
    <input type="submit" value="Send" />
  </p>
</form>

form_process.php

<?php
    $name_first = $_POST['name_first'];
    $name_second = $_POST['name_second'];
    $email = $_POST['email'];
    $text = $_POST['text'];
    $from = 'From: '; 
    $to = 'EMAIL HERE'; 
    $subject = 'Hello';

    $body = "From: $name_first\n $name_second\n E-Mail: $email\n Message:\n $text";

    if ($_POST['submit']) {
        if (mail ($to, $subject, $body, $from)) { 
            header("Location: index.html");
            echo '<p>Your message has been sent!</p>';
            exit;
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        }
    }

?>
b4n4n4
  • 65
  • 2
  • 12
  • 1
    For one, you have two fields named `name` (they should be an array, or have different names), but that's not the source of your problem. Do you get any errors? And you can't use `header` like you're using it, take a look at [this SO](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php). – Qirel Sep 09 '15 at 01:09
  • I'm suspecting that your `mail()` functions has some wrong parameters, specifically the 4th one. That's a "header", but you don't really specify the full header. Take a look at [PHP `mail()`](http://php.net/manual/en/function.mail.php) under "additional_headers (optional)". – Qirel Sep 09 '15 at 01:25

2 Answers2

2

Your error is from the line

if ($_POST['submit']) {

This is because you did not give your submit button a name of submit. If you fix this line in your HTML it should fix the issue:

<input type="submit" name="submit" value="Send" />

I recommend that you set an error log in your php.ini file. That way you can see the error for yourself which would have said something similar to:

PHP Notice: Undefined index: submit in /var/www/pwd/blah/form_process.php on line 12

kojow7
  • 10,308
  • 17
  • 80
  • 135
  • 1
    Even though that's true, his original post had that name in it :) (You can look at the edit-history) – Qirel Sep 09 '15 at 01:29
  • This fixed the issue and it now redirects back to my index.html. But it doesn't give me a message saying it worked. Most importantly it still doesn't send the email. I'm using localhost:8080 by running `php -S localhost:8080` the email should send, right? – b4n4n4 Sep 09 '15 at 01:33
  • You do not see a message telling you it has been sent because you have a header that redirects you back to the index page. Nothing after that will be seen. For security reasons you also should always have a die() or exit() after doing a header redirect. – kojow7 Sep 09 '15 at 01:36
  • True indeed Qirel. :) – kojow7 Sep 09 '15 at 01:37
  • @b4n4n4 if you using localhost to send email you can't use this way. And you need to echo it if the sending work. – Fiido93 Sep 09 '15 at 01:38
  • Do you have a mail server installed such as sendmail or postfix? – kojow7 Sep 09 '15 at 01:46
  • @kojow7 thanks this makes sense. I will add that in as well! No I don't. I'm guessing it wont send over localhost, but this code will work once hosted? – b4n4n4 Sep 09 '15 at 01:47
  • @FiidoFirdauz will this way work once it is hosted though, without me having to add anything else? – b4n4n4 Sep 09 '15 at 01:48
  • @FiidoFirdauz do I need to alter anything in my HTML for your code to work or did you build it around my existing HTML? – b4n4n4 Sep 09 '15 at 01:50
  • @b4n4n4 Yes! you need to alter it haha :'). sorry if this feel burden u haha – Fiido93 Sep 09 '15 at 01:51
  • It should work, though there are some things that you should fix up. You should have a From email address, use a Reply-To: as well, and replace all your \n with \r\n as per the specs. Also, if you are sending from a local machine without DKIM/SPF set in your domain records, expect your mail to get delivered to a junkmail folder. – kojow7 Sep 09 '15 at 01:54
0

If you are working in localhost mode you will need phpmailer.

First you need download phpmailer from here https://github.com/PHPMailer/PHPMailer/archive/master.zip

Then paste in your folder. If my coding doesn't clear you, you can check from

https://github.com/PHPMailer/PHPMailer

<?php
require 'PHPMailerAutoload.php'; // Your Path

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // Your mail 
$mail->Password = 'secret';                           // Your mail password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;     

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML



$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

//Check Condition
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

Second way.

If you working testing in online mode(Have own domain and hosting), you can just randomly copy and paste.

Doesnt required phpmailer.

if(isset($_POST['email'])) $email = $_POST['email'];
else $email = "";



function send_mail($myname, $myemail, $contactname, $contactemail, $subject, $message) {


    $headers = "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    $headers .= "X-Priority: 1\n";
    $headers .= "X-MSMail-Priority: High\n";
    $headers .= "X-Mailer: php\n";
    $headers .= "From: \"".$myname."\" <".$myemail.">\r\n";
    return(mail("\"".$contactname."\" <".$contactemail.">", $subject, $message, $headers));
}

if(isset($Submit) && $Submit=="Go") {

     $emailContent ='';


    $sent=send_mail($name, "yourmailname.gmail.com", "Fido", $receipientEmail, "Testing", $emailContent);
    if($sent) {
      echo $emailContent;

        header('Location: contact.php');
    }else{
        echo "Failed";
        exit;
    }

}


?>

Regards

Fiido93
  • 1,918
  • 1
  • 15
  • 22