I want to create a contact form using PHP mailing function. I am learning the process by developing on a free web hosting provider. Here are my files:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 Contact Form</title>
<link rel="stylesheet" media="screen" href="styles.css">
</head>
<body>
<div id="contact">
<form class="contact_form" action="contact.php" method="post" name="contact_form">
<ul>
<li>
<h2>Contact Us</h2>
<span class="required_notification">* Denotes Required Field</span>
</li>
<li>
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="John Doe" required />
</li>
<li>
<label for="email">Email:</label>
<input type="email" name="email" id="email" placeholder="john_doe#example.com" required /> <span class="form_hint">Proper format "name#something.com"</span>
</li>
<li>
<label for="message">Message:</label>
<textarea name="message" id="message" cols="40" rows="6" required></textarea>
</li>
<li>
<button class="submit" id="submit_btn" type="submit">Submit Form</button>
</li>
</ul>
</form>
</div>
</body>
</html>
contact.php
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'babloopuneeth#gmail.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) {
?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'sample.html';
</script>
<?php
} else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to gordon#template-help.com');
window.location = 'sample.html';
</script>
<?php
}
?>
I also have a styles.css file. I'm getting the alert box saying Thank you for the message. But I'm not getting a mail.
Can anyone help me? Where am I going wrong?
The free hosting service you're using may not allow this functionality (I would of thought it wouldn't) have you checked?
Also check out this post:
Please check your hosting service, I am sure your code is all good as I actually tested it and it worked perfectly. :) You may want to test it yourself on http://goo.gl/sDIL6. (I will keep it there for a few days..)
Does your hosting service block port 25? I have tested it on my server and it works perfectly.
If you are using 000webhost then it does. That is the most common free service and they removed support for mail a while back.
Related
I don't know any PHP at all so I researched a couple hours to try and get my contact form to actually save and send the inputs to my email. As of right now, I'm getting an error Failed to load resource: the server responded with a status of 405 (). Here is the contact form code and also PHP file code. They are located in the root directory, named index.html and contact.php. I'm using the template html5up.net/strata.
<form method="post" action="contact.php" id="form" class="contact-form">
<div class="row uniform 50%">
<div class="6u 12u$(xsmall)"><input type="text" name="cf_name" id="name" placeholder="Name" /></div>
<div class="6u$ 12u$(xsmall)"><input type="email" name="cf_email" id="email" placeholder="Email" /></div>
<div class="12u$"><textarea name="cf_message" id="message" placeholder="Message" rows="4"></textarea></div>
</div>
<ul class="actions">
<li><input type="submit" value="Send Message" class="special"/></li>
</ul>
</form>
contact.php
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'simonchangwong#gmail.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact_page.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to gordon#template-help.com');
window.location = 'contact_page.html';
</script>
<?php
}
?>
I am having trouble with my PHP code for my website. The email is being sent but has no messages.
This is the email I get when it sends. It's empty.
Data I input in my contact form
I tested my php code without the css files and bootstrap, and received the email perfectly with the messages. But when I included everything, from css and bootstrap codes, I receive the email without any messages.
HTML CODE:
<div class="contact-form bottom">
<h2> We want to hear from you . Send us a message!</h2>
<form id="main-contact-form" name="contact-form" method="post" action="send_email_test.php">
<div class="form-group">
<input type="text" name="userName" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="userEmail" class="form-control" required="required" placeholder="Email Id">
</div>
<div class="form-group">
<textarea name="userMessage" id="message" required class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit">
</div>
</form>
</div>
This is my PHP code:
<?php
$field_name = $_POST['userName'];
$field_email = $_POST['userEmail'];
$field_subject = $_POST['userSubject'];
$field_message = $_POST['userMessage'];
$mail_to = 'info#mariamulan.com'; /* Add your email address here */
$subject = 'Message from website'.$field_name; /* Create your own subject */
$body_message .= 'From: '.$field_name."\n";
$body_message .= 'Email: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Hey! Thanks for the message! We will try to reply to you as soon as possible!');
window.location = 'index.html'; /* Where you want to get directed */
</script>
<?php
} else { ?>
<script language="javascript" type="text/javascript">
alert('Sorry, your message was not sent! Please send an email to
hello.mariamulan#gmail.com instead.');
window.location = 'index.html'; /* Where you want to get directed
*/
</script>
<?php
}
?>
Verify that your form has the correct "name" attributes.
Your code is pretty dirty tho.
I'm having trouble to set this email form working properly
I have this contact.html:
<form mehtod="post" action="contact.php">
<hr />
<div class="row">
<div class="large-12 columns">
<label>Name
<input type="text" name="cf_name" placeholder="Name" />
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Email
<input type="text" name="cf_email" placeholder="Email" />
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Your message
<textarea name="cf_message" placeholder="Your message"></textarea>
</label>
</div>
</div>
<input class="button" type="reset" value="Clear">
<input class="button success expand" type="submit" value="Submit">
</form>
And this contact.php:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'my#email.com';
$subject = 'Personal Website Message from '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. I will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. There is a problem with your message, if keep aving trouble click on the Email me button above.');
window.location = 'contact.html';
</script>
<?php
}
?>
But when I upload this, and test it, nothing is sent to my email, why ? For me everything seems to be right ...
Your mail client may not be allowing sending emails without authorization. Unfortunately PHP's mail function does not support username + password authorization, so you have to look for third party extensions.
Please check the spelling of your 'method' attribute in the form element.
The PHP code you supplied specifies $_POST as the variable that contains data relevant to the form.
Your misspelling causes the server use the default 'get' method to post the data (for reference, see here)
Correcting the opening tag to the following should fix the issue:
<form method="post" action="contact.php">
I want to create a email contact form without refreshing the page. so i used jquery for that. Now after submitting the form i want to send successful or error message to the user. Here is my code:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 Contact Form</title>
<link rel="stylesheet" media="screen" href="styles.css" >
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$('#mycontactform').submit(function(e){
e.preventDefault();
$.post("contact.php", $(this).serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
});
});
</script>
</head>
<body>
<form id="mycontactform" class="contact_form" action="" method="post" name="contact_form">
<ul>
<li>
<h2>Contact Us</h2>
<span class="required_notification">* Denotes Required Field</span>
</li>
<li>
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="John Doe" required />
</li>
<li>
<label for="email">Email:</label>
<input type="email" name="email" id="email" placeholder="john_doe#example.com" required />
<span class="form_hint">Proper format "name#something.com"</span>
</li>
<li>
<label for="message">Message:</label>
<textarea name="message" id="message" cols="40" rows="6" required ></textarea>
</li>
<li>
<button class="submit" id="submit" style="cursor:pointer" type="submit">Submit Form</button>
<div id="success" style="color:red;"></div>
</li>
</ul>
</form>
</body>
</html>
contact.php
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'babloopuneeth#gmail.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) {
echo "Your email was sent!";
} else {
echo "Your email was not sent!";
}
?>
Now after submitting the form, if the mail was sent successfully i want if statement of php to be displayed if mail was not sent i wand else statment to be executed. But Im not getting successful or error messages after submitting the form nor im recieving any mail? Where am i going wrong? And is my jquery code correct to submit the form without refreshing the page? Plz someone help me
You are passing the data to a different page. You need to pass the message you wish to display back to the originating page before it will display. You can do this using jQuery.
You can have a div on your page as a place holder
<div id="message"></div>
Then load the output of contact.php into it
My contact form downloads the php file on submit everytime, not sure why this happens but my code is correct and have used this code on other websites and it works fine.
Here's my HTML:
<form id="ContactForm" action="contact.php" method="post">
<div>
<div class="wrapper">
<div class="bg">
<input class="input" type="text" name="name" id="name" value="" tabindex="1">
</div>
Name: </div>
<div class="wrapper">
<div class="bg">
<input class="input" type="text" name="email" id="email" value="" tabindex="1">
</div>
Email: </div>
<div class="wrapper">
<div class="bg2">
<textarea cols="1" rows="1" name="message" id="message"></textarea>
</div>
Message: </div>
<input type="submit" value="Submit" class="button" /> </div>
</form>
Here's the PHP:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'email address';
$subject = 'Message from a website visitor '.$field_name;
$body_message = 'From: '.$field_name."\n\n";
$body_message .= 'E-mail: '.$field_email."\n\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed.');
window.location = 'contact.html';
</script>
<?php
}
?>
Please note: I deliberately took out the email address after $mail_to
Anyone have this problem before and manage to find out what it is?
That usually indicates that PHP is not installed or not running on that web server.
You'll need to install php onto your server (if you have access to the terminal):
sudo apt-get install php5
sudo apache2ctl restart
Best way to check if you have php installed is by creating a new php file and calling it test.php or something:
<?php
phpinfo();
?>
If you have php installed, it'll show you your current php settings. If not, it means you dont have it installed.