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"