Code or Server issue - PHP contact form? - php

I'm redesigning a site, and having just uploaded the files to the host, am now testing that everything works. The message form is not working consistently - sometimes it sends the user to the thank you confirmation page and sometimes it displays the error: "500 Internal Server Error, The server encountered an internal error or misconfiguration and was unable to complete your request." However, no test email has arrived at the recipient address showing that the form has worked. Strangely, it successfully went through when I substituted my personal email address in the mailer.php document. This makes me think that the code works.
I'm suspecting that this is a server or email configuration issue, because I've also been having issues with email, after changing hosts, but possibly it has to do with my code?
HTML:
<form class="form-horizontal" method="post" action="mailer.php" class="form-horizontal" role="form">
<div class="form-group">
<label for="name" class="col-sm-2"><small>Name</small></label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputName" name="inputName" placeholder="Enter your full name" required>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2"><small>Email</small></label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail" name="inputEmail" placeholder="Enter your email, example#domain.com" required>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2"><small>Telephone</small></label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPhone" name="inputPhone" placeholder="Enter your telephone number" required>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2"><small>Message</small></label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="inputMessage" id="inputMessage" placeholder="Enter your message here" required></textarea>
</div>
</div>
<div class="form-group">
<!-- The following field is for robots only, invisible to humans: -->
<p class="robotic" id="pot">
<label>If you're human leave this blank:</label>
<input name="robotest" type="text" name="robotest" id="robotest" class="robotest" />
</p>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input style="font-size:22px;" id="submit" name="submit" type="submit" value="Send" class="btn">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<! Will be used to display an alert to the user>
</div>
</div>
</form>
mailer.php file:
<?php
/* Set e-mail recipient */
$myemail = "info#domain.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your name was not entered correctly.");
$email = check_input($_POST['inputEmail'], "Your email address was not entered correctly.");
$phone = check_input($_POST['inputPhone'], "Your telephone number was not entered correctly.");
$message = check_input($_POST['inputMessage'], "Your message was not entered correctly.");
$robotest = $_POST['robotest'];
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address.");
}
/* ROBOT TEST */
if($robotest)
{
show_error("Denied, robot.");
}
/* prepare the message for the e-mail */
$subject = "Inquiry from Website";
$message = "
Someone has sent you a message using your website's contact form:
Name: $name
Email: $email
Telephone: $phone
Subject: $subject
Message:
$message
";
/* send the message using mail() function */
mail($myemail, $subject, $message);
/* redirect visitor to the thank you page */
header('Location: http://www.website.com/thankyou.html');
exit();
/* functions used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again.</p>
</body>
</html>
<?php
exit();
}
?>

It appears you have a server/configuration problem. I've used this code and put it on my own server and it works perfectly fine. (also, nice work, very clean code)
One thing to think about when checking whether you get emails or not (specifically when a personal email works, but a corporate one does not) is email heuristics. Try changing the content setup and/or the subject line. (the subject line looks suspcious to me, I'd start there).
https://en.wikipedia.org/wiki/Naive_Bayes_spam_filtering

Related

Problems with PHP form validation

My form is spread across two pages. Pages are turned using the JS slider called Slick. After filling in the form and clicking the send button on page 2, the validation says I didn't fill in my name. You can see this for yourself on this website: Rofordaward. Just complete the nomination form and push send. HTML and PHP code below.
HTML
SNIPPET OF FORM (from nominate.html):
<form action="nominate.php" method="post">
<div class="single-item slider">
<div class="page1">
<label class="row">
<h2 class="headline">Your full name</h2>
<input type="text" name="yourname" placeholder="Forename and surname"></input>
</label>
<label class="row email">
<h2 class="headline">Your email address <p>Don't worry, we won't spam you or share your email address</p></h2>
<input type="text" name="email" placeholder="example#rofordaward.co.uk"></input>
</label>
<label class="row">
<h2 class="headline">Name of company</h2>
<input type="text" name="companyname" placeholder="e.g. Roford"></input>
</label>
</div>
<div class="page2">
<label class="row reason">
<h2 class="headline">Reason for nomination</h2>
<textarea id="textarea" rows="6" cols="25" maxlength="1000" name="reason" placeholder="A brief evidence based summary"></textarea>
<div id="text-area-wrap">
<div id="textarea_feedback"></div>
</div>
</label>
<div class="row button-wrap">
<div class="column small-12">
<input class="button" type="submit" value="Send it!">
</div>
</div>
</div>
</div>
</form>
PHP
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$email = check_input($_POST['email']);
$companyname = check_input($_POST['companyname']);
$reason = check_input($_POST['reason'], "Write your reason");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/*Message for the e-mail */
$message = "New submission
Name: $yourname
E-mail: $email
Company name: $companyname
Reason:
$reason
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
/* Functions used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
When I remove the scripts for the JS slider, the form becomes fully functional.
In your PHP Code, You are using $_POST['email'] which suits good according to code on your last HTML Block when <input type="text" name="email" /> as you have set it to 'email'
On other hand, in your First HTML Code Block:
<label class="row email">
....
<input type="text" name="youremail" ....></input>
</label>
You set name="youremail", which is definitely going to derive wrong result with your PHP Code. You should be using it like $_POST['youremail'] in your PHP Code to get it working.

How do I get my php form to display a succes notice?

This is my first post here. I'm really hoping you can help me out!
I just implemented a bootstrap contact form on my website. The form is set to redirect to another website if the mail is sent successfully. I really want it to just show a notice saying "E-mail sent" or something like that.
I do realize that
header('Location: http://address-of-confirmation-page.html');
exit();
is what needs to be changed, i just don't know how to change it to the success message
Here's my code:
The PHP
/* Set e-mail recipient */
$myemail = "your-email#gmail.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your Name");
$email = check_input($_POST['inputEmail'], "Your E-mail Address");
$subject = check_input($_POST['inputSubject'], "Message Subject");
$message = check_input($_POST['inputMessage'], "Your Message");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Someone has sent you a message";
$message = "
Someone has sent you a message using your contac form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: http://address-of-confirmation-page.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
The HTML
<div class="container">
<div class="panel panel-default" style="margin:0 auto;width:500px">
<div class="panel-heading">
<h2 class="panel-title">Contact Form</h2>
</div>
<div class="panel-body">
<form name="contactform" method="post" action="php/contact.php" class="form-horizontal" role="form">
<div class="form-group">
<label for="inputName" class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputName" name="inputName" placeholder="Your Name">
</div>
</div>
<div class="form-group">
<label for="inputEmail1" class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Your Email">
</div>
</div>
<div class="form-group">
<label for="inputSubject" class="col-lg-2 control-label">Subject</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputSubject" name="inputSubject" placeholder="Subject Message">
</div>
</div>
<div class="form-group">
<label for="inputPassword1" class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea class="form-control" rows="4" id="inputMessage" name="inputMessage" placeholder="Your message..."></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-default">
Send Message
</button>
</div>
</div>
</form>
</div>
</div>
</div>
Bootstrap already has success messages like this one, which would be nice to have:
<div class="bs-example">
<div class="alert alert-success fade in">
×
<strong>Success!</strong> Your message has been sent successfully.
</div>
</div>
Thanks in advance!
I suppose you can use
header('Location: http://current-page.php?success');
And put this somewhere suitable
if(isset($_GET['success'])){
//echo the success message
}
I guess this is kinda "hacky" but it works.
I would also recommend jQuery to submit the form to an external page and have it return a success and have jQuery return a success flag and then dynamically show the success message on the page. You can do some research on that, rather than using PHP to do this.
Why not just set the "http://address-of-confirmation-page.html" page to a simple thank you page? This wouldn't involve any clever scripting. Its basic, but it fits with your script!
so change :
/* Redirect visitor to the thank you page */
header('Location: http://address-of-confirmation-page.html');
exit();
to
/* Redirect visitor to the thank you page */
header('Location: http://yourwebsite.com/thank_you.php');
exit();
and thank_you.php would be :
<div class="bs-example">
<div class="alert alert-success fade in">
×
<strong>Success!</strong> Your message has been sent successfully.
</div>
</div>
Does that answer your question, or is that too simplistic of a solution?
The best way to do it, and it's not so hard (and easy to maintain) is using Ajax like the other suggested.
Javascript/jQuery
First add an id to the form element, let's say #contact-form, then let's send the form via ajax:
$("#contact-form").submit(function(e) {
$.ajax({
type: "POST",
url: php/contact.php,
data: $("#contact-form").serialize(),
success: function(data){
$('#message')
.addClass('alert-'+ data.status)
.append(data.message);
}
);
e.preventDefault();
});
HTML
Hide the message by default, and let's show the correct message depending of the p
<div id="message" class="alert" style="display: none;">
×
</div>
<!-- Your contact form here -->
PHP
That's your homework, the script should be waiting a response in json, something like:
$result = array(
'status' => // success | error ,
'message' => // 'Success Message' | 'validation message'
);
echo json_encode($result);

Blank PHP Emails

There's a lot of blank php email posts on here but none of them have solved this for me.
I tweaked this simple php code I found to simply email a specified email address (in this case, my client's email) with a feedback message from a customer on their website. Through testing, I could only get it to send emails when I didn't include the initial if statement as validation, but even then, the emails would have no subject or body.
contact.html
<form name="feedback" class="form-horizontal" role="form" action="send_form_email.php" method="post">
<div class="form-group">
<label for="inputName" class="col-sm-3 control-label">Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="inputName" placeholder="Name"><br />
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
<input type="email" class="form-control" name="inputEmail" placeholder="Email"><br />
</div>
</div>
<div class="form-group">
<label for="inputMessage" class="col-sm-3 control-label">Message</label>
<div class="col-sm-9">
<textarea type="text" class="form-control" name="inputMessage" placeholder="Message"></textarea><br />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input class="btn btn-default" type="submit" value="Submit">
</div>
</div>
</form>
send_form_email.php
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Contact subject
$name =$_POST['inputName'];
// Details
$message=$_POST['inputMessage'];
// Mail of sender
$mail_from=$_POST['inputEmail'];
// From
$header="from: $name <$mail_from>";
// Enter your email address
$to ='test#gmail.com';
$send_contact=mail($to,$name,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
header("Location: http://wetzelscontracting.com/postcontact.html");
if($send_contact){
echo "We've recived your contact information";
}
else {
echo "ERROR";
}}
?>
Ok guys, long story, but Mailto isn't actually in the action attr, I removed it from the post.
Actually, I don't know what kind of frankenstein code I originally posted, but that was full of errors that are no longer there. Hopefully I posted the right code this time.
Why is your form action MAILTO:?
<form name="feedback" class="form-horizontal" role="form" action="MAILTO:send_form_email.php" method="post">
It should just be a clean call to the PHP page like this:
<form name="feedback" class="form-horizontal" role="form" action="send_form_email.php" method="post">
The only time you would use MAILTO: is when constructing an <a href="mailto:someguy#someplace.somedomain">. For an HTML form using PHP like this the goal is to submit the form, and the the $_POST data gets parsed by the PHP which then acts on it to send an e-mail.
Additionally, you are not setting name values in any of the input fields & the names you have for id values dont even match what the PHP is attempting to do. So try this for the HTML:
<form name="feedback" class="form-horizontal" role="form" action="send_form_email.php" method="post">
<div class="form-group">
<label for="inputName" class="col-sm-3 control-label">Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputName" placeholder="Name" name="inputName"><br />
</div></div>
<div class="form-group">
<label for="inputEmail" class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
<input name="email" type="email" class="form-control" id="inputEmail" placeholder="Email" name="inputEmail"><br />
</div></div>
<div class="form-group">
<label for="inputMessage" class="col-sm-3 control-label">Message</label>
<div class="col-sm-9">
<textarea type="text" class="form-control" id="inputMessage" placeholder="Message" name="inputMessage"></textarea><br />
</div></div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input class="btn btn-default" type="submit" value="Submit">
</div></div>
</form>
Also here is the reworked PHP code.
The first thing I did was take all of your $_POST checks into a structure that uses one main array ($post_array) and then rolls through that array to process the values & assign them to similarly named variables. You had absolutely no input validation before. This is technically not even really great “validation” since isset() just checks to see if the $_POST value even exists. But this is step up.
Also I reworked your error checking logic at the end since it all happened after headers were sent. Meaning none of that the whole "We've recived your information" would never work. This is the best I can do with the info you’re providing, but I am doing this to convey the basic concepts:
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST"){
// Set the post values array.
$post_array = array('inputName','inputEmail','inputMessage');
// Roll through the post values array.
foreach($post_array as $post_key => $post_value) {
$$post_key = isset($_POST[$post_key] ? $_POST[$post_key] : null;
}
// From
$header="from: $name <$mail_from>";
// Enter your email address
$to ='test#gmail.com';
$send_contact=mail($to,$name,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
header("Location: http://wetzelscontracting.com/postcontact.html");
}
else {
echo "ERROR";
}
}
?>
As none of the other answers have covered the issue of validation apart from the one accepted, but if your going to do that you might as well just use the extract() function, (it also won’t protect from header injection or email validation).
It’s very important to validate user input and a layer of simple CSRF protection, else bots or spammers can directly POST to your PHP and it will send you a bombardment of emails, you won’t see the forest for the trees (legit emails), or worse inject headers into your inputEmail field and send their own emails using your server which is obviously something you don't want to happen.
Also I’ve added an easy way that you can pass errors from your PHP script that sends the user back to the form for you to echo out.
So for the send_form_email.php file.
<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_SESSION['csrf'])){
//set error array to fill
$errors = array();
// Validate Contact subject
if(!empty($_POST['inputName'])){
$name = $_POST['inputName'];
}else{
$error['inputName'] = 'Required!';
}
// Validate Details
if(!empty($_POST['inputMessage'])){
$message = $_POST['inputMessage'];
}else{
$error['inputMessage'] = 'Required!';
}
// Validate Mail of sender
if(!empty($_POST['inputEmail'])){
if(filter_var($_POST['inputEmail'], FILTER_VALIDATE_EMAIL)){
$mail_from = $_POST['inputEmail'];
}else{
$error['inputEmail'] = 'Invalid Email!';
}
}else{
$error['inputEmail'] = 'Required!';
}
if(!isset($_POST['csrf']) || $_SESSION['csrf'] != $_POST['csrf']){
$_SESSION['email_status'] = 'Invalid csrf token!';
$error = true;
}
//stop multiple attempts - just remove csrf token
unset($_SESSION['csrf']);
//no errors send mail
if(empty($error)){
$headers ='MIME-Version: 1.0'."\r\n";
$headers.='Content-type: text/html; charset=utf8'."\r\n";
$headers.='From:<'.$mail_from.'>'."\r\n";
$headers.="X-Mailer: PHP"."\r\n";
if(mail('test#gmail.com', 'Website email form: '.$name, $message, $headers)){
$_SESSION['email_status'] = "We've received your contact information";
//send to success page
exit(header("Location: http://wetzelscontracting.com/postcontact.html"));
}else {
$_SESSION['email_status'] = 'There was an error sending the mail';
//backup to file
file_put_contents('mail.log.txt',print_r($_POST, true).PHP_EOL, FILE_APPEND);
}
}else{
//assuming its this url
exit(header("Location: http://wetzelscontracting.com/contact.php"));
$_SESSION['email_error'] = $error;
}
}else{
//stop multiple attempts
unset($_SESSION['csrf']);
//dont allow GET request/direct access
exit(header("Location: http://wetzelscontracting.com/contact.php"));
}
?>
Then in your page with the form, start a session to read from the $_SESSION array, and then echo out your errors if any.
<?php
session_start();
//make a session key that we will check against in send_form_email.php
$_SESSION['csrf'] = sha1(uniqid(true));
?>
<?php echo isset($_SESSION['email_status']) ? $_SESSION['email_status'] : null ?>
<form name="feedback" class="form-horizontal" role="form" action="send_form_email.php" method="post">
<input type="hidden" name="csrf" value="<?php echo $_SESSION['csrf'];?>"/>
<div class="form-group">
<label for="inputName" class="col-sm-3 control-label">Name <?php echo isset($_SESSION['email_error']['inputName']) ? $_SESSION['email_error']['inputName'] : null?></label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputName" placeholder="Name" name="inputName"><br />
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-3 control-label">Email <?php echo isset($_SESSION['email_error']['inputEmail']) ? $_SESSION['email_error']['inputEmail'] : null?></label>
<div class="col-sm-9">
<input type="email" class="form-control" id="inputEmail" placeholder="Email" name="inputEmail"><br />
</div>
</div>
<div class="form-group">
<label for="inputMessage" class="col-sm-3 control-label">Message <?php echo isset($_SESSION['email_error']['inputMessage']) ? $_SESSION['email_error']['inputMessage'] : null?></label>
<div class="col-sm-9">
<textarea type="text" class="form-control" id="inputMessage" placeholder="Message" name="inputMessage"></textarea><br />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input class="btn btn-default" type="submit" value="Submit">
</div>
</div>
</form>
<?php
//unset the errors so there only shown once
unset($_SESSION['email_status']);
unset($_SESSION['email_error']); ?>

Php mailer script not working on Apache hosted Bootstrap website

I am creating a simple Django/Bootstrap website hosted on a VM running apache.
The path to my website is var/www/experiment/
I have followed this excellent tutorial on creating a php mailer contact form. I was having an issue with php not being installed on the account, but running sudo apt-get install libapache2-mod-php5 has solved this.
I also tried a second script given to me by a friend who has used it on several websites.
My problem is that the contact form emails are not being sent. I have checked it with 2 different email accounts and I have checked the spam folders in case. As I said, I have also checked two separate scripts.
What else could be wrong or needs to be changed/configured at my end?
My mailer.php file is located in var/www/experiment/php is this the right location?
Would anyone have a link to a better tutorial on using php mailer and apache?
EDIT: added code
Bootstrap - HTML
<form name="contactform" method="post" action="http://location_of_my_web_app/php/mailer.php"class="form-horizontal" role="form">
<div class="form-group">
<label for="inputName" class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputName" name="inputName" placeholder="Your Name">
</div>
</div>
<div class="form-group">
<label for="inputEmail1" class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Your Email">
</div>
</div>
<div class="form-group">
<label for="inputSubject" class="col-lg-2 control-label">Subject</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputSubject" name="inputSubject" placeholder="Subject Message">
</div>
</div>
<div class="form-group">
<label for="inputPassword1" class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea class="form-control" rows="4" id="inputMessage" name="inputMessage" placeholder="Your message..."></textarea>
</div>
</div>
<div class="form-group">
<center>
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-primary">Send Message</button>
</div>
</center>
</div>
</form>
PHP
<?php
/* Set e-mail recipient */
$myemail = "my_address#gmail.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your Name");
$email = check_input($_POST['inputEmail'], "Your E-mail Address");
$subject = check_input($_POST['inputSubject'], "Message Subject");
$message = check_input($_POST['inputMessage'], "Your Message");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Someone has sent you a message";
$message = "
Someone has sent you a message using your contac form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: http://location_of_my_thank_you_page/Static/contact_thankyou.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
Are you using PHP's mail() function, or connecting to a server and sending mail via SMTP? Whatever the case, the official PHPMailer website has a variety of examples. Do note that if you want to test sending mail via SMTP, you can always configure a gmail account and mess around with it.
https://github.com/PHPMailer/PHPMailer/tree/master/examples

PHP Mailer. Clicking send downloads the php script but does not send mail

I am following a tutorial on how to implement a simple Bootstrap contact form with a PhP mailer script.
However when I click Send Message, my contact form downloads the PHP script rather than sending the email. What am I doing wrong?
Bootstrap - HTML
<form name="contactform" method="post" action="http://location_of_my_web_app/php/mailer.php" class="form-horizontal" role="form">
<div class="form-group">
<label for="inputName" class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputName" name="inputName" placeholder="Your Name">
</div>
</div>
<div class="form-group">
<label for="inputEmail1" class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Your Email">
</div>
</div>
<div class="form-group">
<label for="inputSubject" class="col-lg-2 control-label">Subject</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputSubject" name="inputSubject" placeholder="Subject Message">
</div>
</div>
<div class="form-group">
<label for="inputPassword1" class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea class="form-control" rows="4" id="inputMessage" name="inputMessage" placeholder="Your message..."></textarea>
</div>
</div>
<div class="form-group">
<center>
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-primary">Send Message</button>
</div>
</center>
</div>
</form>
PHP
<?php
/* Set e-mail recipient */
$myemail = "my_address#gmail.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your Name");
$email = check_input($_POST['inputEmail'], "Your E-mail Address");
$subject = check_input($_POST['inputSubject'], "Message Subject");
$message = check_input($_POST['inputMessage'], "Your Message");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Someone has sent you a message";
$message = "
Someone has sent you a message using your contac form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: http://location_of_my_thank_you_page/Static/contact_thankyou.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
"my contact form downloads the PHP script rather than sending the email"
It sounds like PHP is either not installed/running or not properly configured.
I suggest that you create a file called test.php file with <?php echo "Hello world"; ?> inside it and see if it does the same thing.
If it still wants to appear like it wants to download, then there's the problem; PHP is not installed or not properly configured and isn't parsing PHP as it normally should.
Create another file and place <?php phpinfo(); ?> and it should show you the server's information.

Categories