Receiving 500 Server Error with Contact Form Submission.php - php

I've looked through a bunch of similar issues but haven't been able to find an answer that works. I recently switched my website over to a different host and am trying to set up the contact form. I am getting a 500 error when I click submit. I've made sure the SMTP is set up correctly. I just don't know what else to try at this point. Here's my code:
HTML:
<form role="form" method="POST" action="contact-form-submission.php">
<div class="row">
<div class="form-group col-lg-4">
<label for="input1">Name</label>
<input type="text" name="contact_name" class="form-control" id="input1">
</div>
<div class="form-group col-lg-4">
<label for="input2">Email Address</label>
<input type="email" name="contact_email" class="form-control" id="input2">
</div>
<div class="form-group col-lg-4">
<label for="input3">Phone Number</label>
<input type="phone" name="contact_phone" class="form-control" id="input3">
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<label for="input4">Message</label>
<textarea name="contact_message" class="form-control" rows="6" id="input4"></textarea>
</div>
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
PHP:
<?php
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: contact.php'); exit;
}
// get the posted data
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];
// check that a name was entered
if (empty($name))
$error = 'You must enter your name.';
// check that an email address was entered
elseif (empty($email_address))
$error = 'You must enter your email address.';
// check for a valid email address
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address))
$error = 'You must enter a valid email address.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header('Location: contact.php?e='.urlencode($error)); exit;
}
$headers = "From: $email_address\r\n";
$headers .= "Reply-To: $email_address\r\n";
// write the email content
$email_content = "Name: $name\n";
$email_content .= "Email Address: $email_address\n";
$email_content .= "Phone Number: $phone\n";
$email_content .= "Message:\n\n$message";
// send the email
//ENTER YOUR INFORMATION BELOW FOR THE FORM TO WORK!
mail ('EMAIL', 'Young & Company - Contact Form Submission', $email_content, $headers);
// send the user back to the form
header('Location: index.html'/*.urlencode('Thank you for your message.'*/)); exit;
?>

You need to look into PHP error log. Try this to show errors.
ini_set('display_errors', '1');
Post your logs here if you can't discern which logs are relevant. Most likely SMTP settings are be blamed. mail function opens a socket connection. using SMTP settings.
SMTP settings can be managed through ini or by:
ini_set('SMTP', 'smtphost');
ini_set('smtp_port', 25);
The above settings are just, for example, you need to have your own SMTP settings. For example, if you have a Gmail account, you may use it to send mail. It depends on your specific situation on which SMTP server you may want to use.
Here are the Gmail SMTP settings. Also, look at the documentation.

There is an error on the last line of contact-form-submission.php file.
You forget to remove the extra bracket from the below line.
Old
// send the user back to the form
header('Location: index.html'/*.urlencode('Thank you for your message.'*/)); exit;
New
// send the user back to the form
header('Location: index.html'/*.urlencode('Thank you for your message.'*/); exit;
Or include that bracket in comment tag.
// send the user back to the form
header('Location: index.html'/*.urlencode('Thank you for your message.')*/); exit;

Related

React JS and PHP contact form fails

I'm having an issue with my website. To give some context, I've worked on it using ReactJS for the front-end and Express for the back-end. BUT, my hosting provider doesn't support Node, so I had to change the back-end part to PHP. I only developed the contact form functionality.
I'm not sure if I'm missing something, but I would like to ask your opinion to continue due to I'm stuck.
Here's the front-end part for Contact.js:
import React, {Component} from 'react';
class Contact extends Component{
render(){
return(
<div>
<form id="contact-form" name="c-form" method="post" action="/mailer.php">
<div className="input-field">
<input
id="first_name"
type="text"
className="validate"
name="first_name"
required/>
<label for="first_name">Name</label>
</div>
<div className="input-field">
<input id="sub" type="text" className="validate" name="sub"/>
<label for="sub">Subject</label>
</div>
<div className="input-field">
<input id="email" type="email" className="validate" name="email" required/>
<label for="email">Email</label>
</div>
<div className="input-field">
<textarea
id="textarea1"
className="materialize-textarea"
name="message"
required></textarea>
<label for="textarea1">Message</label>
</div>
<div className="contact-send">
<button
id="submit"
name="contactSubmit"
type="submit"
value="Submit"
className="btn waves-effect">Send
</button>
</div>
</form>
</div>
)
}
}
export default Contact;
and also, the back-end part in the PHP file, mailer.php located under /src folder:
<?php
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "*HERE I PLACED MY EMAIL*";
// Set the email subject.
$subject = "New contact from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>
The result is:
POST http://www.*mywebsite.com*/mailer.php 400 (Bad Request)
Any suggestion or recommendation? Thanks in advance!
change mailer.php
$name = strip_tags(trim($_POST["name"]));
to
$name = strip_tags(trim($_POST["first_name"]));

HTML 5 form which send infos in a PHP e-mail

I would use some help because I don't know what to do at all...
I created an HTML form (which isn't a problem) :
<form method="post" action="form-page.php" class="cta">
<div class="row gtr-uniform gtr-50">
<div class="col-8 col-12-xsmall"><input type="text" name="user_name" id="name" required placeholder="Votre nom"></div>
<div class="col-8 col-12-xsmall"><input type="email" name="email" id="email" placeholder="Votre adresse e-mail" /></div>
<div class="col-8 col-12-xsmall"><textarea name="user_message" id="msg" required placeholder="Votre message"></textarea></div>
<div class="col-4 col-12-xsmall"><input type="submit" value="Envoyer." class="fit primary" /></div>
</div>
</form>
I created the file "form-page.php" which treat the form with this code :
<?php
// We get the infos of the form
$user_name = $_POST['user_name']; // Nom / Pseudo.
$email = $_POST['email']; // e-mail.
$user_message = $_POST['user_message']; // Message.
//=========
// We put the adress where the mail is going to be send.
//P.S : "myemail#gmail.com" isn't MY email, I just put it here to hide my email :)
$mail = 'myemail#gmail.com';
//=========
// We filtrate servers to avoid bugs.
if (!preg_match("#^[a-z0-9._-]+#(hotmail|live|msn).[a-z]{2,4}$#", $mail))
{
$passage_ligne = "\r\n";
}
else
{
$passage_ligne = "\n";
}
//=========
// We define the subject of the mail.
$sujet = "Suggestion ou bug de ton site, Pokemon Reality";
//=========
// We create the header of the email.
$header = "From: \"$user_name\" $email".$passage_ligne;
$header.= "MIME-Version: 1.0".$passage_ligne;
//==========
// We create the message.
$message = $user_message;
//==========
// Finally, we send the email.
mail($mail,$sujet,$message,$header);
//==========
?>
Buuuut... This doesn't work! After I press the button "Envoyer." of the form, I wait while the page is loading and I see an error :
ERROR
ERROR2
I didn't touch to anything, and before that, I test it 3 times, and it send the mail, but there was a blank page.
So I want to fix the error, and then, I want to redirect the user to the form and say "Thank You !".
Thanks for your help!

PHP form is not successfully submitting getting custom error

My PHP form isn't submitting successfully. I keep getting the custom error that I wrote ('Oops there was a problem. Please try again").
any help would be greatly appreciated. I'm totally new to PHP so I'm thinking maybe some of my php variables are linked wrong and arent connecting with my mailer-new.php file?
Thanks in advance,
<section class="form-body">
<form method="post" action="mailer-new.php" class="contact-form" >
<div class="row">
<?php
if ($_GET['success']== 1){
echo " <div class=\"form-messages success\"> Thank you!
your message has been sent. </div>";
}
if ($_GET['success']== -1){
echo " <div class=\"form-messages error\"> Opps there was a
problem. Please try again </div>";
};
?>
<div class="field name-box">
<input type="text" name="name" id="name" placeholder="Who
Are You?" required/>
<label for="name">Name</label>
<span class="ss-icon">check</span>
</div>
<div class="field email-box">
<input type="text" name="email" id="email"
placeholder="name#email.com" required/>
<label for="email">Email</label>
<span class="ss-icon">check</span>
</div>
<div class="field msg-box">
<textarea name="message" id="msg" rows="4"
placeholder="Your message goes here..."/></textarea>
<label for="message">Msg</label>
<span class="ss-icon">check</span>
</div>
<input class="button" type="submit" value="Send"/>
</div>
</form>
</section>
MAILER.PHP
<?php
// Get the form fields, removes html tags and whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"), array(" ", " " ), $name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
// Check the data.
if (empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: http://www.conallen.ie/index.php?
success=-1#form");
exit;
}
// Set the recipient email address. Update this to YOUR desired email address.
$recipient = "allenconallen46#gmail.com";
// Set the email subject.
$subject = "New contact from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
mail($recipient, $subject, $email_content, $email_headers);
// Redirect to the index.html page with success code
header("Location: http://www.conallen.ie/index.php?success=1#form");
?>
This code works correctly in my local machine, even though email is not sent response messages are coming correctly. The changes I have done is changed the host name and made the two lined header redirect into one line in the failed response. Also you have mentioned in the HTML the file name as action="mailer-new.php" and the file name mentioned in the question as MAILER.PHP. Are they same in the code?
To check whether the mail is sent or not remove the header redirect and update like this. If you are getting a failed response means mail is not configured in your server.
if(mail($recipient, $subject, $email_content, $email_headers)) {
echo "mail sent";
} else {
echo "mail sent failed";
}
Alternatively you can use the SMTP for sending email. You can use a library called PHP Mailer and can use any of the valid email address like a gmail account. Please have look at this question if that's the case
Sending email with PHP from an SMTP server

PHP sending form doesn't work correctly

I'm testing a PHP form to send email in local.
I put all input but when I press the submit button it always returns "false" and then the error message. Is that because I'm working in local and don't have any mail server, or is there something wrong in my code?
here the code:
<?php
if(isset($_POST['submit']))
{
if(empty($_POST['nome']) ||
empty($_POST['email']) ||
empty($_POST['motivo']) ||
empty($_POST['messaggio']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
// return false;
}
else
{
$nome = strip_tags(htmlspecialchars($_POST['nome']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$motivo = strip_tags(htmlspecialchars($_POST['motivo']));
$messaggio = strip_tags(htmlspecialchars($_POST['messaggio']));
// Create the email and send the message
$to = 'mirkocoppola80#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $nome";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $nome\n\nEmail: $email_address\n\nOggetto: $motivo\n\nMessaggio:\n$messaggio";
$headers = "From: mirkocoppola80#gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
if (!#mail($to,$email_subject,$email_body,$headers))
{
// return true;
echo "<p>Email error</p>";
}
else
{
echo "<p>Email sent successfully!</p>";
}
}
}
?>
<form class="form-horizontal col-sm-6 col-sm-offset-3" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
<div class="form-group">
<div class="row">
<label for="email" class="col-sm-12">Email</label>
</div>
<div class="row">
<div class="col-sm-12">
<input type="email" name="email" class="form-control" id="email" placeholder="Email">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label for="nome" class="col-sm-12">Nome</label>
</div>
<div class="row">
<div class="col-sm-12">
<input type="text" name="nome" class="form-control" id="nome" placeholder="Nome">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label for="motivo" class="col-sm-12">Motivo</label>
</div>
<div class="row">
<div class="col-sm-12">
<input type="text" name="motivo" class="form-control" id="motivo" placeholder="Motivo">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label for="messaggio" class="col-sm-12">Messaggio</label>
</div>
<div class="row">
<div class="col-sm-12">
<textarea class="form-control" name="messaggio" rows="5" id="messaggio" placeholder="Motivo">Inserisci il tuo messaggio...</textarea>
</div>
</div>
</div>
<div class="form-group">
<div class="">
<button type="submit" name="submit" class="btn btn-default">Invia</button>
</div>
</div>
</form>
Any help?
Is that because I'm working in local and don't have any mail server
Yes.
PHP's mail() function will always fail and return false if you don't have a local mail server running.
This answer has some useful tips
You need to setup a mail server on your machine for the mail function to work. If you are on Windows (which I am guessing you are from your use of WAMP) you can setup a Pegasus mail server.
Other options include using a wrapper class such as SwiftMailer or PHPMailer and using them to connect to another SMTP server such as your GMail account. Even if you go the Pegasus mail server on your own localhost route then I would still recommend using one of the two classes I have mentioned above. They give you far more flexibility and are safer.
Connecting to either your ISPs SMTP server or GMail or whatever is the easiest route out of this one.
To expand on the above, you can also look into Mailhog or Mailcatcher to capture your mail locally and examine its contents.
remove the "#" in
if (#mail($to,$email_subject,$email_body,$headers))
<?php
if(isset($_POST['submit']))
{
if(empty($_POST['nome']) ||
empty($_POST['email']) ||
empty($_POST['motivo']) ||
empty($_POST['messaggio']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
// return false;
}
else
{
$nome = strip_tags(htmlspecialchars($_POST['nome']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$motivo = strip_tags(htmlspecialchars($_POST['motivo']));
$messaggio = strip_tags(htmlspecialchars($_POST['messaggio']));
// Create the email and send the message
$to = 'mirkocoppola80#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $nome";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $nome\n\nEmail: $email_address\n\nOggetto: $motivo\n\nMessaggio:\n$messaggio";
$headers = "From: mirkocoppola80#gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
if (mail($to,$email_subject,$email_body,$headers))
{
// return true;
echo "<p>Email error</p>";
}
else
{
echo "<p>Email sent successfully!</p>";
}
}
}
?>
it should work then.

AJAX Contact Form - Adding Fields + Multiple Forms One Page

So I need to implement an AJAX information submit form. I'm using this example built by the guys at Treehouse… http://blog.teamtreehouse.com/create-ajax-contact-form
First part of the question… How do I add new fields? I've tried editing the .php .js and .html, getting them all in line to what I believe is right but upon testing my solution fails.
The fields I require… 
Name:
Location:
Post Code:
Date of Birth:
URL Link:
Message (200 words):
The second part… Can I run two of these on the same page?
Cheers in advance for any help! - you can email me garethyo#gmail.com if you want me to send code etc.
CODE BELOW —
<form id="ajax-contact" method="post" action="mailer.php">
<div class="field">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
</div>
<div class="field">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="field">
<label for="link">Link:</label>
<input type="text" id="link" name="link" required>
</div>
<div class="field">
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea>
</div>
<div class="field">
<button type="submit">Send</button>
</div>
</form>
JS
$(formMessages).text(response);
// Clear the form.
$('#name').val('');
$('#email').val('');
$('#link').val('');
$('#message').val('');
})
.fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('success');
$(formMessages).addClass('error');
PHP
<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "garethyo#gmail.com";
// Set the email subject.
$subject = "A Future Bubbler… $name ";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Link: $link\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>
You never define $link. You need to add $link = trim($_POST['link']); before $message = trim($_POST["message"]);. – Sean Apr 4 at 20:50
— Sean, CEO Worldstar

Categories