I have a contact form on a page that should send the details/message to an email address. You can view it here (it's in the footer): http://tedsrestaurant.com/index-working.html
HTML
<form id="contact-form" class="row style1" action="#" method="post">
<div class="col-xs-12 col-sm-4">
<div class="form-group">
<input name="name" type="text" id="name" placeholder="Your name" required />
</div>
<div class="form-group">
<input name="number" type="text" id="number" placeholder="Your number" required />
</div>
<div class="form-group">
<input name="email" type="email" id="email" placeholder="Your e-mail" required />
</div>
</div>
<div class="col-xs-12 col-sm-8">
<textarea name="message" rows="4" class="h130" id="message" placeholder="Write your message..." required></textarea>
</div>
<div class="clear"></div>
<div class="col-md-9"><p>* When booking the <strong>Food Truck</strong>, please write date, time and number of guests into your message.</p></div>
<div class="col-md-3 right">
<input class="submit btn" type="submit" value="Send a message" />
<input class="modal btn hidden" type="button" value="Send a message" name="" data-toggle="modal" data-target="#modalBox" />
</div>
<!-- Modal window -->
<div class="modal fade bs-modal-md" id="modalBox" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Contact form</h4>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn right" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</form>
And the PHP is:
<?php
//******** Email settings ********//
//your email
$email_to = "myemail#gmail.com";
//subject of email
$email_subject = "This is a message";
//message after success send mail
$email_send = "<div class='alert alert-success'>
<strong>Contact form was sent successfully.</strong>
<br/>Thank you for contacting us. We will be in touch with you very soon.</div>";
if($_POST) {
//******** Email data ********//
$cust_name = $_POST['name'];
$cust_number = $_POST['number'];
$cust_email = $_POST['email'];
$cust_message = $_POST['message'];
$header = "From:" . $cust_email . "\r\n";
$header .= 'MIME-Version: 1.0' ."\r\n";
$header .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$body = "<strong>Customer email:</strong> " . $cust_email . "<br/>";
$body .= "<strong>Customer number:</strong> " . $cust_number . "<br/>";
$body .= "<strong>Customer name:</strong> " . $cust_name . "<br/>";
$body .= "<strong>Message:</strong> " . $cust_message;
if($cust_name && $cust_number && $cust_email && mail($email_to, $email_subject, $body, $header)) {
echo $email_send;
echo '<script>$("#contact-form").each(function(){
this.reset();
});
</script>';
} else {
echo "<div class='alert alert-warning'><strong>Error sending mail. Please fill the contact form correctly or contact website administrator.</strong></div>";
}
}
Does everything look correct? FYI the PHP on the site has a real email address, not myemail#gmail.com. Once I hit the submit button I am taken to the same page, but nothing ever comes to my e-mail.
What is the output that you receive? An email_sent or error message?
If you get neither which I guess is the case, you should be using
if($_SERVER['REQUEST_METHOD'] == "POST") in place of if($_POST) to check if the request type is POST and further proceed.
I recommend to use SwiftMailer.
require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password')
;
/*
You could alternatively use a different transport such as Sendmail or Mail:
// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
// Mail
$transport = Swift_MailTransport::newInstance();
*/
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);
Related
My objective is to call a "Thank you" modal message after successfully sending a quotation to an email, but I can't find the right code to do so. This is how the page works. I will fill up the form, then after I submit it, it will load the form.php, which has the code to send the email from the user to the assigned email which the message is supposed to be sent. After that, form.php will confirm if the email was successfully sent or not. My problem is, suppose that the message was sent, how will I redirect to the main index.html with the "Thank you" modal open?
Here's my index.html code.
<form class="login-form" method="post" action="php/form.php" name="quote-form" role="form">
<fieldset>
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Your Name" required data-error="Your Name is required.">
</div>
<div class="form-group">
<input type="text" name="phone_number" class="form-control" placeholder="Phone Number " required data-error="Phone Number is required.">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email Address" required data-error="Valid email is required.">
</div>
<! --Adding Radio button For Call Back -->
<div class="form-group">
<label>Request Phone Call?: <br>
<input type="radio" value ="Yes " required data-error="required." name="call">
<em>Yes</em><br>
<input type="radio" value ="No" required data-error="required." name="call">
<em>No</em><br>
</label>
</div>
<div class="form-group">
<textarea id="textinput" class="form-control" name="message" required rows="1" placeholder="Please provide the ff:
-Location
-Dimension (Total Area less openings, All side running meters for endcap computation)
-Color
-Profile (Cladding or Decking)
-If Pick up / Shipment / Delivery
-If with installation" maxlength="500"></textarea>
Remaining characters: <span id="count"></span>
</div>
<div class="form-group">
<label>
<input type="checkbox" required data-error="checkbox is required.">
<em>I agree with the <a data-dismiss="modal" aria-label="Close" data-toggle="modal" data-target=".bs-example-modal-md-5">(privacy policy)</a></em></label>
</div>
<button class="tg-theme-btn tg-theme-btn-lg" type="submit">Submit</button>
</fieldset>
</form>
Here's my full form.php code:
<?php
//Variable Declaraton
$name = $_POST['name'];
$email = $_POST['email'];
$to_email = "someone#example.com";
$subject = "New Quote";
$phoneNumber = $_POST['phone_number'];
$call = $_POST['call'];
$message = "From: $email\n" . "Client Sender: $name \n" . "Client Number: $phoneNumber\n" . "Call Back?: $call\n" . "\n\n" . "Message: \n\n" . $_POST['message'];
$headers = 'WPC Cladding & Decking Website <no-reply#aggtrading.com>';
//EMAIL CONFIRMATION
// --- Subject of confirmation email. ---------
$conf_subject = 'Your recent enquiry';
// Who should the confirmation email be from?
$conf_sender = 'AGGTE WPC Cladding & Decking <no-reply#aggtecomposites.com>';
$msg = "Your email: $email\n" . "Your Number: $phoneNumber\n" . "\n" ."Hi" ." ". $_POST['name'] . ",\n\nThank you for your recent enquiry. A member of our team will respond to your message as soon as possible.";
mail( $_POST['email'], $conf_subject, $msg, 'From: ' . $conf_sender );
//----------Send to company email. ---------
/* $send = #mail($to_email,$subject,$message,$headers); */
if(#mail($to_email,$subject,$message,$headers)) {
echo "<script> location.href='../index.html';
</script>";
}
else{
echo "<h1>Message not sent.</h1>";
}
?>
Here's the modal dialog that should open after redirecting from form.php after sending the email:
<!--my modal-->
<div id="myModal" class="modal fade bs-example-modal-md-2" tabindex="-1" role="dialog">
<div class="modal-dialog modal-md-2" role="document">
<div class="modal-content">
<div class="top_links">Close (X)</div>
<h2 class="modal-title">GET A FREE QUOTE</h2>
<p>Thank you for sending us message! Feel free to explore more on our website!</p>
</div>
</div>
</div>
<!--my modal-->
Note: Here's the if/else that will redirect to index.html after confirming that the email was successfully sent:
if(#mail($to_email,$subject,$message,$headers)) {
echo "<script> location.href='../index.html'; </script>";
}
else{
echo "<h1>Message not sent.</h1>";
}
Thank you!
First set up a message variable instead of echo
Something like
$message = "IT WAS A SUCCESS";
Then initialize the Modal with the bootstrap jQuery initialization and place the Modal and the initialization itself ,under an if Statement.
if (isset($message)) {
Modal and initialization here
}
Set the message before the JavaScript redirect.
I am using bootstrap to open up a modal form.
I really cannot figure out what is going on. This code works in other projects (http://phpform.webitwebsites.co.nz/newtest/) and sends the mail no problem. But if I copy it into my website, it is like my submit button is unresponsive and it doesn't load the initiate quote.php file.
Here is a live version where it is not working
http://phpform.webitwebsites.co.nz/test/
My HTML:
<button href="#freeQuote" role="button" data-toggle="modal" type="button" class="btn btn-primary btn-lg outline" style="font-size:16px;">Free Quote</button>
</center>
<div class="modal fade" id="freeQuote">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">FREE Quote</h3>
</div>
<!-- close modal-header -->
<div class="modal-body">
<h4>Send us a description </h4>
<hr>
<form name="contact" method="post" action="quote.php">
<label for="name">Your Name</label>
<br>
<input class="col-md-10" type="text" name="name" class="input-xlarge">
<br>
<br>
<label for="company">Your Company</label>
<br>
<input class="col-md-10" type="text" name="company" class="input-xlarge">
<br>
<br>
<label for="email">Your E-mail</label>
<br>
<input class="col-md-10" type="email" name="email" class="input-xlarge">
<br>
<br>
<label for="message">Details</label>
<br>
<textarea class="col-md-10" name="message" class="input-xlarge"></textarea>
<br>
<input class="btn-sm btn-success" type="submit" value="Send!" id="submit">
</form>
</div>
<!-- close modal-body -->
My PHP
<?php
$myemail = 'example#gmail.com';
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$company = strip_tags ($_POST['company']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
echo "<span class=\"alert alert-success\" >Your message has been received. Thanks! Here is what you submitted:</span><br><br>";
echo "<stong>Name:</strong> ".$name."<br>";
echo "<strong>Company:</strong> " .$company . "<br>";
echo "<stong>Email:</strong> ".$email."<br>";
echo "<stong>Message:</strong> ".$message."<br>";
//generate email and send!
$to = $myemail;
$email_subject = "New Quote form submission";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
}
?>
Thank you in advance for your assistance.
I am struggling to get all the information within this form to print properly to email. At present I am receiving data from the 'email', 'checkin' and 'checkout' input fields. However 'name' , 'guests' and 'message' do not appear.
This is the HTML code for the form:
<form class="reservation-vertical clearfix" role="form" method="post" action="php/reservation.php" name="reservationform" id="reservationform">
<div id="message"></div>
<!-- Error message display -->
<div class="form-group">
<label for="name"><span class="required">*</span> Your Name</label>
<input name="name" type="text" class="form-control" value="" placeholder="Full Name"/>
</div>
<div class="form-group">
<label for="email">E-mail</label>
<input name="email" type="text" value="" class="form-control" placeholder="Please enter your E-mail"/>
</div>
<div class="form-group">
<label for="checkin">Check-in</label>
<div class="popover-icon" data-container="body" data-toggle="popover" data-trigger="hover" data-placement="right" data-content="Check-in is from 2:00pm"> <i class="fa fa-info-circle fa-lg"> </i> </div>
<i class="fa fa-calendar infield"></i>
<input name="checkin" type="text" value="" class="form-control" placeholder="Check-in"/>
</div>
<div class="form-group">
<label for="checkout">Check-out</label>
<div class="popover-icon" data-container="body" data-toggle="popover" data-trigger="hover" data-placement="right" data-content="Check-out is by 11:00am"> <i class="fa fa-info-circle fa-lg"> </i> </div>
<i class="fa fa-calendar infield"></i>
<input name="checkout" type="text" value="" class="form-control" placeholder="Check-out"/>
</div>
<div class="form-group">
<label for="guests">Guests</label>
<i class="fa fa-user infield"></i>
<input name="guests" type="text" value="" class="form-control" placeholder="Number of guests"/>
</div>
<div class="form-group">
<label for="extra"> Your message</label>
<textarea name="extra" rows="9" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary btn-block">Enquire Now</button>
</div>
</div>
</div>
</form>
Here is the PHP code:
<?php
if(!$_POST) exit;
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$checkin = $_POST['checkin'];
$checkout = $_POST['checkout'];
$guests = $_POST['guests'];
$extra = $_POST['extra'];
if(get_magic_quotes_gpc()) {
$extra = stripslashes($extra);
$email = stripslashes($email);
$name = stripslashes($name);
$guests = stripslashes($guests);
}
$address = "myemail#gmail.com";
$e_subject = "Hotel booking enquiry submitted by $fullname" . PHP_EOL;
$e_body = "
A hotel booking enquiry has been made by: $name
Their email is: $email
The customer wants to check-in at: $checkin
and check-out at: $checkout
The customer requested accommodation for: $guests guest(s).
They also included this message: $extra" . PHP_EOL;
$e_reply = "You can contact the customer via email, $email";
$msg = wordwrap( $e_body . $e_reply, 70 );
$headers = "From: myemail#gmail.com\r\n" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id=success_page style=text-align:center>";
echo "<h4>Booking enquiry sent successfully!</h4>";
echo "<p><br>Thank you, your enquiry has been received. We will contact you shortly to complete your booking.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo "ERROR!";
}
This line
$e_subject = "Hotel booking enquiry submitted by $fullname" . PHP_EOL;
Should be
$e_subject = "Hotel booking enquiry submitted by $name" . PHP_EOL;
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 am using a simple html form as a contact, and when fields and submitted the form does not clear the fields.
this is my php
I read online in few places and I've learned that I have to use the .reset , but I am not familiar with php a lot. I am not sure where would I add the .reset and how.
<?
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$msg = $_REQUEST["msg"];
$to = "example#example.com";
if (isset($email) && isset($name) && isset($msg)) {
$subject = "Message / Closure Film";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$name." <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
$msg = "Name: $name:<br/> Email: $email <br/> Message: $msg";
$mail = mail($to, $subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
my html
<div id="contact">
<div class="container">
<div class="row-fluid PageHead">
<div class="span12">
<h3>CONTACT US<span> <img src="images/underline.png" alt="______"></span></h3>
</div>
</div>
<div class="row-fluid ContactUs">
<div class="span6 offset3">
<form class="form-horizontal" id="phpcontactform">
<div class="control-group">
<input class="input-block-level" type="text" placeholder="Full Name" name="name" id="name">
</div>
<div class="control-group">
<input class="input-block-level" type="email" placeholder="Email" name="email" id="email">
</div>
<div class="control-group">
<textarea class="input-block-level" rows="10" name="message" placeholder="Your Message" id="message"></textarea>
</div>
<div class="control-group">
<p>
<input class="btn btn-danger btn-large" type="submit" value="Send Message">
</p>
<span class="loading"></span> </div>
</form>
</div>
added this to the head of my html, but didnt get any result
<script type="javascript">
$('#phpcontactform').trigger("reset");
</script>
Try:
<script type="javascript">
$(document).ready(function() {
$('#phpcontactform')[0].reset();
});
</script>
I'm not sure if you're trying to do an ajax submit and keep the user on the page, or just submit the form. But the line you're looking for is $("#phpcontactform")[0].reset();, you could wrap that in a $(document).ready() if you needed to!