0

I've been reading and attempting solutions for the past 4 hours, but I'm still no closer to figuring it out. I've got an HTML form, and I want it to be submitted as an email to me. PHP seems like the only way to do that, but I can't figure it out for the life of me. I've tried making < form action="form-to-email.php >, I've tried making the HTML document itself PHP and < form action="" >. I've tried using the PHP code in the HTML, and vice versa. I've tried at least ten different versions of PHP code to take the information from the form and send an email, but the only time I was able to get the email to send, it wouldn't take any info from the form. Can somebody please tell me what I'm doing wrong and how to fix it?

HTML

<div class="row contact-form">
            <form action="form-to-email.php" method="POST" enctype="text/plain" id="contact-form">
                <div class="col-md-4">
                    <label for="name" class="required">Name:</label>
                    <input name="name" type="text" id="name" maxlength="40" />
                </div> <!-- /.col-md-4 -->
                <div class="col-md-4">
                    <label for="email" class="required">Email:</label>
                    <input name="email" type="text" id="email" maxlength="40" />
                </div> <!-- /.col-md-4 -->
                <div class="col-md-4">
                    <label for="subject">Subject:</label>
                    <input name="subject" type="text" id="subject" maxlength="60" />
                </div> <!-- /.col-md-4 -->
                <div class="col-md-12">
                    <label for="message" class="required">Message:</label>
                    <textarea name="message" id="message" rows="6"></textarea>
                </div> <!-- /.col-md-12 -->
                <div class="col-md-12">
                    <div class="submit-btn">
                        <input class="largeButton contactBgColor" type="submit" value="SEND MESSAGE" />
                    </div> <!-- /.submit-btn -->
                </div> <!-- /.col-md-12 -->
            </form>
            </div>

PHP

<?php

// Debugging tools. Only turn these on in your development environment.

error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");

// Special mail settings that can make mail less likely to be considered spam
// and offers logging in case of technical difficulties.

ini_set("mail.log", "/tmp/mail.log");
ini_set("mail.add_x_header", TRUE);

// The components of our email

$to      = 'name@domain.com';
$name    = $_POST['name'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$email   = $_POST['email'];
$headers = implode("\r\n", [
    'From: email',
    'Reply-To: email',
    'X-Mailer: PHP/' . PHP_VERSION
]);

    $email_from = 'name';

    $email_subject = 'subject';

    $email_body = "You have received a new message from the user $name.\n".
                            "Here is the message:\n $message".

// Send the email

$result = mail($to, $subject, $message, $headers);

// Check the results and react accordingly

if ($result) {

    // Success! Redirect to a thank you page. Use the
    // POST/REDIRECT/GET pattern to prevent form resubmissions
    // when a user refreshes the page.

    header('contact-form-thank-you.html', true, 303);
    exit;

}
else {

    // Your mail was not sent. Check your logs to see if
    // the reason was reported there for you.

}

At this point I'm just getting a string of garbled error messages, no emails, and a withering sanity. I've read this page up and down, and none of it helped. My test site is here, and the contact form is on the bottom of the landing page. PHP is honestly making less and less sense the more I deal with it. I'm sorry that the previously posted solutions aren't working for me, but I've tried everything I know how to, and I still feel no closer to resolving this. I would really appreciate any advice you can offer to help me through this!

For those asking about the error messages, this is what I was getting:

int(8) string(21) "Undefined index: name" string(41) "/home/lanpoint/public_html/contact us.php"

Community
  • 1
  • 1
Franatic
  • 1
  • 1
  • 4
    Can you post some of the "garbled error messages"? What you have given here will not help us to help you. – Jay Blanchard Sep 01 '16 at 18:17
  • 4
    `"a string of garbled error messages"` - Dismissing the error messages as background noise generally isn't how one would correct errors. – David Sep 01 '16 at 18:17
  • Sending mail in PHP is famously difficult. Could be any of a million causes. Most PHP code that sends mail uses PHPMailer, https://github.com/PHPMailer/PHPMailer, and I highly recommend it. – Goose Sep 01 '16 at 18:19
  • If it's a garbled mess, then how do you know it's an error message? – Martin Sep 01 '16 at 18:21
  • 1
    You should treat error messages as gold. They tell you where the error is. – baao Sep 01 '16 at 18:25
  • Get rid of that `enctype="text/plain"` attribute. Check [this](http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean) or [this](http://stackoverflow.com/questions/7628249/method-post-enctype-text-plain-are-not-compatible) for more info. – Don't Panic Sep 01 '16 at 18:31

1 Answers1

0

This should work:

HTML

<div class="row contact-form">
    <form action="form-to-email.php" method="POST" id="contact-form">
         <div class="col-md-4">
               <label for="name" class="required">Name:</label>
               <input name="name" type="text" id="name" maxlength="40" />
         </div> <!-- /.col-md-4 -->
         <div class="col-md-4">
               <label for="email" class="required">Email:</label>
               <input name="email" type="text" id="email" maxlength="40" />
         </div> <!-- /.col-md-4 -->
         <div class="col-md-4">
               <label for="subject">Subject:</label>
               <input name="subject" type="text" id="subject" maxlength="60" />
         </div> <!-- /.col-md-4 -->
         <div class="col-md-12">
               <label for="message" class="required">Message:</label>
               <textarea name="message" id="message" rows="6"></textarea>
         </div> <!-- /.col-md-12 -->
         <div class="col-md-12">
             <div class="submit-btn">
                 <input class="largeButton contactBgColor" type="submit" value="SEND MESSAGE" />
             </div> <!-- /.submit-btn -->
        </div> <!-- /.col-md-12 -->
    </form>
</div>

Form-to-email.php

<?php
    $name = strip_tags($_POST['name']);
    $email = strip_tags($_POST['email']);
    $subject = strip_tags($_POST['subject']);
    $message = strip_tags($_POST['message']);

    $from = $email; 
    $to = 'youremail@address.com;

    $body = "From: $name\n Subject: $subject\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {              
        if (mail ($to, $subject, $body, $from)) {
            echo 'Success!';
        } else { 
            echo 'Failure';
        } 
    }
?>
Schwesi
  • 4,753
  • 8
  • 37
  • 62