Again - I've looked and troubleshooted for about an hour and cannot get the php contact form to work. I get the "Thank you!" email, but I haven't received the email to my inbox ever. I've done it about 20 times.
<div class="container">
<div class="row">
<div class="col-lg-12">
<h5>Want to contact me quickly? Use this form!</h5>
<form id="contact" method="post" action="submit.php" class="form" role="form">
<div class="row">
<div class="form-group col-lg-4">
<label>Name</label></br>
<input type="text" name="name" placeholder="Enter your full name" required autofocus>
</div>
<div class="form-group col-lg-4">
<label>Email address</label></br>
<input type="email" name="email" placeholder="Enter your email address" required autofocus>
</div>
<div class="form-group col-lg-4">
<label>Subject</label></br>
<input type="text" name="subject" placeholder="Subject">
</div>
<div class="form-group col-lg-12">
<label>Message</label>
<textarea name="comments" name="message" data-provide="markdown-editable" rows="6" placeholder="Let's chat!" style="width:100%"></textarea>
</div>
<div class="form-group col-lg-12 form-action">
<input type="hidden" name="Submit" value="contact">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
</div>
PHP:
<?php
if ($_POST['Submit'])
{
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comments=$_POST['comments'];
$to='MY EMAIL ADDRESS';
$from='website';
if (mail($to, $subject, $name, $email, $comments))
{
echo 'Thank you!';
}
else
{
echo 'Something went wrong! Try again';
}
}?>
Any help is greatly appreciated. I have my MAMP server on and I get the "thank you!" message every time, but I haven't gotten any emails. Also, I'd really like it to show the "thank you!" and then return to my webpage, but I can't even get the php to work so I maybe should hold off on that part.
You have a wrong call of mail() function.
It should be like this:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
So, for your case:
<?php
if ($_POST['Submit'])
{
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comments=$_POST['comments'];
$to='email#email.com';
$from='website';
$message = 'Message: ' . $comments . "\r\n";
$message .= 'From: ' . $from;
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
if (mail($to, $subject, $message, $headers))
{
echo 'Thank you!';
}
else
{
echo 'Something went wrong! Try again';
}
}
?>
The mail() function simply returns true if the message has been queued without error in your system... but it doesn't tell you whether the email was actually sent or not. I see you're using Mac OS X, so you'll have to configure SMTP settings (provided by your ISP) in the PHP environment to get mail sending working.
Alternately, you can use a service like Amazon SES to send emails through PHP.
Related
I am trying to send an email from a enquiry form available on a catalogue website on which I am working and found a strange issue.
On the same domain I have a file to test the email function with following code in it:
$sender = 'someone#somedomain.tld';
$recipient = 'name#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
}
and its working fine. But the same code is not working on the index.php page where I have the enquiry form and also not showing any error at all (I have checked the error log. I have also tried by putting this code in a separate file and by including it on index.php but the same result).
<form name="frm_enquiry" id="frm_enquiry" method="post" autocomplete="off">
<div class="row">
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="fullname">Name:</label>
<input type="text" class="form-control" id="fullname" name="fullname" placeholder="Enter your name">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="mobile">Mobile No:</label>
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Enter your mobile number">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label style="width: 100%;" for="mobile"> </label>
<button type="submit" class="btn btn-block btn-primary" name="submit">Submit</button>
</div>
</div>
</div>
</form>
php:
if( isset( $_REQUEST['submit'] ) )
{
$name = $_REQUEST['fullname'];
$email = $_REQUEST['email'];
$mobile = $_REQUEST['mobile'];
echo $name . ' : ' . $email . ' : ' . $mobile . '<br>';
$sender = 'someone#somedomain.tld';
$recipient = 'name#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
}
Someone please guide me, what I am doing wrong here, I am stuck on this from last 2 days.
Thanks in advance.
I'm very new to PHP however, I have managed to get working an email system from a contact form on my front end of a webpage. The problem I have that once the email is sent, I wish to hide the form and display a message saying "thank you for the email."
I have tried to look for solutions, but the only answer I could find was to use this code: header("Location: sentmail.html"); From here then I redirect it to another HTML page, Identical to the contact page with the form removed. I feel this is bad practice as it will require many duping pages for how many contact forms are across the website.
Is there a way to display a "thank you for your message" Once the email has been sent rather then redirect to a sentmail.html page.
Thank you for taking the time to read.
`<?php
extract($_POST);
if(isset($_POST['submit']))
$name = $_POST['first_name'];
$email = $_POST['email_address'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mailTo = "jamie#sharpsdigital.co.uk";
$headers = "From: MLS systems Contact Form ";
$txt = "$name has sent you an email from the address. " .$email. "\n\n With the message of.\n\n" .$message. " \n\n This is an automated response from the contact form, Please click on the blue email address above to respond." ;
mail ($mailTo, $subject, $txt, $headers);
header("Location: sentmail.html");
?>`
This is the PHP Code, and below the form code
<section class="free-appoinment-area">
<div class="container">
<form action="sendmail.php" method="post" class="free-appoinment-form">
<div class="row">
<div class="col-md-12">
<div class="sec-title text-left">
<h1>Any queries</h1><span class="decor"></span>
</div>
<div class="row">
<div class="col-md-12">
<div class="input-field">
<input name="first_name" placeholder="Your Name*" type="text">
<div class="icon-holder">
<span class="flaticon-people"></span>
</div>
</div>
</div>
<div class="col-md-12">
<div class="input-field">
<input name="subject" placeholder="Your Subject" type="text">
<div class="icon-holder">
<span class="flaticon-people"></span>
</div>
</div>
</div>
<div class="col-md-12">
<div class="input-field">
<input name="email_address" placeholder="Email Address*" type="text">
<div class="icon-holder">
<span class="flaticon-note"></span>
</div>
</div>
</div>
<div class="col-md-12">
<div class="input-field">
<textarea name="message" placeholder="Your Message..."></textarea>
<div class="icon-holder comment">
<span class="flaticon-social-1"></span>
</div>
</div>
</div>
<div class="col-md-12">
<button type="submit" data-type="submit" name="submit" value="submit">Submit</button>
</div>
</div>
</div>
</div>
</form>
</div>
</section>
you can do it with php and css.
the php way is by using if else statement, like bellow
if(isset($_POST['submit'])){
$name = $_POST['first_name'];
$email = $_POST['email_address'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mailTo = "jamie#sharpsdigital.co.uk";
$headers = "From: MLS systems Contact Form ";
$txt = "$name has sent you an email from the address. " .$email. "\n\n With the message of.\n\n" .$message. " \n\n This is an automated response from the contact form, Please click on the blue email address above to respond." ;
if(mail ($mailTo, $subject, $txt, $headers)){
echo "Thanks for your contact.";
}
}else{ ?>
your form goes here
<?php }?>
There are hundreds of possibilities, but simply put, you can grab the return value of mail() and display contents according to this:
$success = mail('...');
form.php
if($success) {
echo 'Thank you!';
} else {
// output form here
}
Of course you can simply set a flag to true in your form handler too, but this is too broad to explain here.
I'm creating php page and create button that post to php script. In my script, I create what normal mail functions need. I passed all the variables, it successfully sent. But my gmail didn't received any mail.
sendmail.php
<?php
$to = 'mygmail#gmail.com';
$subject = 'Message from my page';
$headers = "To: email#hotmail.com\n" .
"From: From Address <from#mydomain.com>\n" .
"MIME-Version: 1.0\n" .
"Content-Type: text/html; charset=iso-8859-1";
$message = 'TheMessage Body';
mail( $to, $subject, $message, $headers );
$result = mail($to, $subject, $message, $headers);
if($result) {
echo "MAIL SENT";
}else {
echo 'MAIL NOT SENT';
}
?>
HTML
<form action="sendmail.php" method="POST" enctype="text/plain" id="contactForm">
<div class="col-sm-12">
<!-- if mail sent successfully -->
<h4 class="success">
<i class="fa fa-check"></i> Your message has been sent successfully.
</h4>
<!-- if mail sent unsuccessfully -->
<h4 class="error">
<i class="fa fa-warning"></i> E-mail must be valid and message must be longer than 1 character.
</h4>
</div>
<div class="col-sm-6">
<input id="name" type="text" name="name" placeholder="Your Name">
</div><!-- col-sm-6 end -->
<div class="col-sm-6">
<input id="email" type="email" name="email" placeholder="Your Email">
</div><!-- col-sm-6 end -->
<div class="col-md-12">
<textarea type="text" name="messages" class="textarea-box" id="message" rows="5" placeholder="Message"></textarea>
<button class="btn-new btn-bold" type="submit" id="submit" name="submit">Submit Message</button>
</div>
</form>
The email you are using on "From:" must exist in your server.
Another point, have you tried to send it a non-Gmail email account - does it arrive?
I'm having trouble getting the php script correct for my contact form. I managed to have it email me but it doesn't show a name, email or text in the email.
PHP Script
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'myemail#email.comm';
$subject = 'Hello';
mail ($to, $subject, $message, "From: " . $name);
echo "Your Message has been sent.";
?>
Contact Form
<form role="form" action="contact.php">
<div class="form-group">
<label for="InputName">Name</label>
<input name="name" type="text" class="form-control" id="InputName" placeholder="Enter Name">
</div>
<div class="form-group">
<label for="InputEmail">Email Address</label>
<input name="email" type="email" class="form-control" id="inputEmail" placeholder="Enter email">
</div>
<div class="form-group">
<label for="InputText">Message</label>
<input name="message" type="text" class="form-control" id="InputText" placeholder="Enter Text">
</div>
<button name="submit" type="submit" class="btn btn-default">Send</button>
</form>
You're using POST variables, yet your form being the following:
<form role="form" action="contact.php">
doesn't have a POST method defined. Form defaults to GET when a method isn't defined.
Therefore, you will need to change your form to
<form role="form" action="contact.php" method="post">
From a comment you left:
"I'm still having a problem where instead of giving me the email they input it gives an email created by the hosting company which is no good."
A: It's most likely because of how you're using From: in your code, which is the person's name. Mail is expecting an Email address.
Replace:
mail ($to, $subject, $message, "From: " . $name);
with:
mail ($to, $subject, $message, $header);
and adding the following under $subject = 'Hello';
$header = "From: ". $name . " <" . $email . ">\r\n";
That way, you will see the person's name in the Email, while having a valid "From" header.
Visit http://php.net/manual/en/function.mail.php for more information on mail headers.
Additional notes:
I also suggest that you test if any of the fields aren't left empty using:
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']))
{
// execute code
}
Otherwise, anyone could send an Email without any information at all.
You can also add an else{} to it like else { echo "Please fill in all the fields."; }
This is a very basic method.
Your form needs the attribute
method="POST"
Without this, the browser defaults to method="GET", which submits the form in the url, e.g., http://example.com/default.php?name=First%20Last&email=...
You should specify POST method:
<form role="form" method="POST" action="contact.php">
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I can't seem to get the form to send a message to my email. Is it maybe due to the server? Here's my code:
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form id="contact-form" action:"" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="hidden" name="action" value="submit">
<label for="name">
Name</label>
<input type="text" class="form-control" id="name" placeholder="Enter name" required />
</div>
<div class="form-group">
<label for="email">
Email Address</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>
</span>
<input type="email" class="form-control" id="email" placeholder="Enter email" required /></div>
</div>
<div class="form-group">
<label for="subject">
Subject</label>
<select id="subject" name="subject" class="form-control" required="required">
<option value="na" selected="">Choose One:</option>
<option value="General">General</option>
<option value="Hiring">Hiring</option>
<option value="My Work">My Work</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="message">
Message</label>
<textarea name="message" id="message" class="form-control" rows="9" cols="25" required
placeholder="Message"></textarea>
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-skin pull-right" id="btnContactUs">
Send Message</button>
</div>
</div>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$subject=$_REQUEST['subject'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($subject=="")||($message==""))
{
echo "All fields are required.";
}
else{
$from="From: $name, $email";
$subject="Message sent using your contact form";
mail("nilsbittmannmyers#yahoo.co.uk", $subject, $message, $from);
echo "Message sent!";
}
}
?>
THANKS :)
Im using byet as a web host by the way, I think they have PHP enabled in the free service, excuse me, I'm a bit of a noob when it comes to coding, to say the least XD XD
You're using a colon action:"" instead of an equal sign which should be action="" which is the main issue in your code.
You're also missing a name attribute for the email and name input fields.
I.e.: name="email" and name="name"
Also, the From: should initially be an email, and not a name as you have in
$from="From: $name, $email";
It's best to use something like this:
$from = "From: ". $name . " <" . $email . ">\r\n";
as your header's 4th parameter.
That way you will have a proper From Email with the person's name appearing in the Email also.
Using $from="From: $name, $email"; will most likely end up in Spam or rejected altogether.
Consult the PHP.net Website for details on mail() and headers:
http://php.net/manual/en/function.mail.php
a few headers options:
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'Return-Path: ' . $from . "\r\n";
in your case would be:
$from = "From: ". $name . " <" . $email . ">\r\n";
$from .= 'Reply-To: ' . $email . "\r\n";
$from .= 'Return-Path: ' . $email . "\r\n";
Sidenote:
I noticed you are using enctype="multipart/form-data"
Unless you're wanting to attach/upload a file, it isn't required for what you're using your form as, it's safe to remove it.
Footnotes:
Using error reporting would have signaled these errors, including Undefined index... warnings:
error_reporting(E_ALL);
ini_set('display_errors', 1);
placed just beneath your opening <?php tag.