I'm trying to send an email using PHP mail(). I created a form that allows the user to input their information and press a submit button to send the email.
What I'm trying to do is to grab $email and set it to the $to header to send to the receiver. Everything else works perfectly, $name and $contact outputs just missing $email.
<form action="#" method="post">
<input type="text" name="name" placeholder="Your Name"><br/>
<input type="text" name="email" placeholder="Your Email"><br/>
<input type="text" name="contact" placeholder="Your Mobile"><br/>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "Welcome: ". $_POST['name']. "<br />";
echo "Your Email is: ". $_POST["email"]. "<br />";
echo "Your Mobile No. is: ". $_POST["contact"];
?>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$to = $email;
$subject = "Test Email";
$message = 'Hello '.$name.', contact: '.$contact.', this is a test email';
$headers = "From: Me <". strip_tags('test#mail.com') . ">\r\n";
mail($to,$subject,$message,$headers);
?>
Any tips would be appreciated
Solution:
<?php
if (!array_key_exists('Submitted',$_POST))
{
?>
<form method="post" action="#">
<input type="hidden" name="Submitted" value="true"><form action="#"
method="post">
<input type="text" name="name" placeholder="Your Name"><br/>
<input type="text" name="email" placeholder="Your Email"><br/>
<input type="text" name="contact" placeholder="Your Mobile"><br/>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "Welcome: ". $_POST['name']. "<br />";
echo "Your Email is: ". $_POST["email"]. "<br />";
echo "Your Mobile No. is: ". $_POST["contact"];
?>
<?php
}
else
{
$name = $_POST['name'];
$contact = $_POST['contact'];
$to = $_POST['email'];
$subject = "Test Email";
$message = 'Hello '.$name.',<br /> contact #: '.$contact.', this is a test
email';
$headers = "From: Me <". strip_tags('test#mail.com') . ">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if(mail($to,$subject,$message,$headers))
{
echo "Message Sent";
}
else
{
echo "Message Not Sent";
}
}
?>
Related
I have a contact form which is working fine. I have Integrate Google ReCaptcha v2 with it and it is showing below my contact form but form will be submitted either user checked reCaptcha or not. I want to verify it that form only goes submit if user has checked google ReCaptcha otherwise it shows a message to the user to check it.
Here is my HTML Form (contact.html):
<html>
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer>
</script>
</head>
<body>
<form action="contact_us.php" method="post">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Your
Name *" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email *" required>
</div>
<div class="form-group">
<input type="text" name="phone" class="form-control" placeholder="Contact Number">
</div>
<div class="form-group">
<textarea class="form-control textarea" name="message" placeholder="Message"></textarea>
</div>
<div class="form-group">
<div class="g-recaptcha" data sitekey="have_enter_my_site_key_here"></div>
</div>
<div class="form-group">
<input type="submit" name="submit" class="form-control btn-
submit" Value="Send">
</div>
</form>
</body>
</html>
Here is PHP code as (contact_us.php):
<?php
ob_start();
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from ='';
$email_subject = "Contact Form";
$email_body = "User Name: $name \n".
"User Email: $email \n".
"Phone Number: $phone \n".
"User Message: \n $message \n";
$to = "my_Email_here";
$headers = "From: $email_from \r\n";
$headers = "Reply-To: $email \r\n";
$result = mail($to,$email_subject,$email_body,$headers);
if($result) {
echo "Sent Successfully";
}
else{
echo "Something Went wrong. Please try again";
}
?>
Help me how should I verify google ReCaptcha with my HTML form.
<?php
if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
// Your site secret key obtained from Google
$secret = '#####################################';
$grResponse = $_POST['g-recaptcha-response'];
// Verify with Google Servers
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $grResponse);
$responseData = json_decode($verifyResponse);
if ($responseData->success) {
ob_start();
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from = '';
$email_subject = "Contact Form";
$email_body = "User Name: $name \n" . "User Email: $email \n" . "Phone Number: $phone \n" . "User Message: \n $message \n";
$to = "my_Email_here";
$headers = "From: $email_from \r\n";
$headers = "Reply-To: $email \r\n";
$result = mail($to, $email_subject, $email_body, $headers);
if ($result) {
echo "Sent Successfully";
}
else {
echo "Something Went wrong. Please try again";
}
} else {
echo "################################";
}
} else {
echo "################################";
}
?>
Please check below code:
I have tried to create php email form. The form was basically working, but I wanted to add validation functions like I did on 'name'
However, It doesn't work when I empty 'name' and just sent an email.
Any help would be really appreciated.
htmlfile
<form action="php_mini.php" method="post">
Name:
<input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Email:
<input type="text" name="email">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Phone:
<input type="text" name ="phone">
<br><br>
Comment:
<!--<textarea name="comment"></textarea>-->
<!--<input type = "text" name = "comment">-->
<textarea name="comment" rows="5" cols="40"></textarea>
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
<input type="submit" value="Submit">
</form>
php
echo '<pre>';
print_r( $_POST );
echo '</pre>';
$headers = 'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'From: Design_customers <customers.com' . " \r\n" .
//'Reply-To: vader#deathstar.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//error msg
$nameErr = $emailErr = $phoneErr = "";
$name = $email = $comment = $phone = "";
//receiver
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
$email = $_POST['email'];
/*$message = $_POST['comment'];*/
$message = 'You got a message from a customer.:
Name: '.$_POST['name'].'
Email: '.$_POST['email'].'
Phone: '.$_POST['phone'].'
Comment: '.$_POST['comment'];
//sender
$from = 'From: Customer';
$subject = 'Customer Inquiry';
mail( $email, $subject, $message, $from );
use if(isset($_POST["name"])) to stop the error. [Notice: Undefined index: name in C........]
better you have validate this form with java Script. i have attached a snipped part of you form with javascript.
<form action="" method="post" onsubmit="return(Validate());" name="myform">
Name:
<input type="text" name="name">
<span class="error">* <div id="name_error" style="color:red;"></div> </span>
<br><br>
</form>
<script type="text/javascript">
<!--
// Form validation code will come here.
function Validate()
{
if( document.myform.name.value == "" )
{
alert( "Please provide your name!" );
name_error.textContent = "Name is Required.!";
document.myform.name.focus() ;
return false;
}
return( true );
}
//-->
</script>
I have a form that sends info to php doc. When testing, I submit and array displays but won't go to email address.
<?php require_once("/includes/fbcheader.php");
?>
<div class="comments">
<h3> We Welcome All Comments </h3>
<form action="/php/contact-send.php" method="post"
id="contact-form" class="form">
<p class="input-block">
<label for="form-name" >Name</label>
<input type="text" value name="name" id="form-name"
autofocus placeholder="Please enter name" required>
</p>
<p class="input-block">
<label for="form-email" >Email</label>
<input type="email" value name="email" id="form-email"
required placeholder="Please enter E-Address">
<input type="hidden" value name="age" id="age">
</p>
<p class="input-block">
<label for="form-subject" >Subject</label>
<input type="text" value name="subject" id="form-subject"
required placeholder="Please enter subject">
</p>
<p class="textarea-block">
<label for="form-message">Comment</label>
<textarea name="message" id="form-message"
cols="70" rows="10"></textarea>
</p>
<div class="clear"></div>
<input type="hidden" name="firstname" id="firstname">
<input type="submit" value="Send" id="form-submit">
<p class="hide" id="response"></p>
<div class="hide">
<label for="spam-check">Do not fill out this field</label>
<input name="spam-check" type="text" value id="spam-check">
</div>
</form>
</div>
</body>
</html>
This is the sending php execute code
<html>
<head>
<title>Contact-Send</title>
</head>
<body>
<?php
if(null!==($_POST["name"])) {
$name = $_POST["name"];
/* echo "Please enter 'Name'<br />"; */
} else {
$name = "";
}
if(null!==($email = $_POST["email"])) {
$email = $_POST["email"];
/* echo "Please enter 'E_Mail'<br />"; */
} else {
$email = "";
}
if(null!==($email = $_POST["subject"])) {
$form_email = $_POST["subject"];
/* echo "Please enter 'Subject'<br />"; */
} else {
$subject = "";
}
if(null!==($message = $_POST["message"])) {
$message = $_POST["message"];
/* echo "Please enter your message<br />"; */
} else {
$message = "";
}
/*echo "{$name}, {$email}, {$message}";*/
$email_from = '$Email';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "me#myaddress.com";
?>
<br>
<?php
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $form_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("thank-you.html");
?>
</body>
</html>
On submit I simply get the array displaying the content of the input fields
A quick look at your PHP code I see the following:
This: if(null!==($email = $_POST["email"])) will fail because you are running a check against nothing. it should be: if(null!==($_POST["email"])).
You have an undefined variable here: $email_from = '$Email'; this will simply output "$Email". It should be $email_from = $email; (as per your code). However you are simply re-assigning the variable here which you dont really need to be doing at all.
Your concatenating the $email_body and $to address.
You header("thank-you.html"); will fail because as per your code, output has already started here <?php (line 6).
You should be aiming for something closer to this for your contact-send.php:
<?php
if(null!==($_POST["name"])) {
$name = $_POST["name"];
/* echo "Please enter 'Name'<br />"; */
} else {
$name = "";
}
if(null!==($_POST["email"])) {
$email = $_POST["email"];
/* echo "Please enter 'E_Mail'<br />"; */
} else {
$email = "";
}
if(null!==($_POST["subject"])) {
$form_email = $_POST["subject"];
/* echo "Please enter 'Subject'<br />"; */
} else {
$subject = "";
}
if(null!==($_POST["message"])) {
$message = $_POST["message"];
/* echo "Please enter your message<br />"; */
} else {
$message = "";
}
/*echo "{$name}, {$email}, {$message}";*/
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message";
$to = "me#myaddress.com";
$headers = "From: $email \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: thank-you.html");
?>
Remember to set $to to your email address!
I've tried every php contact form tutorial but I can't get any of them to send the email properly. They all just show the code but don't send the email. This is the HTML code I have right now:
<form method="POST" action="scripts/contact.php">
<input id="name" name="name" placeholder="Name*" type="text" /> <br>
<input id="subject" name="subject" placeholder="Subject" type="text" /> <br>
<input id="email_address" name="email" placeholder="Email address*" type="text" /> <br>
<textarea id="message" name="message" placeholder="Your message*"></textarea>
<input id="submit" type="submit" name="submit" value="Send" />
</form>
and this is the php I have:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = "From: $email" . $email;
mail($name,$email,$subject,$message,$headers);
echo "Mail Sent.";
?>
I have decent knowledge of HTMl but next to none of PHP. Any help would be greatly appreciated.
first you be sure that php file is exist in path scripts/contact.php
second look at phpinfo or php.ini for find is smptp server was setting or not
use following php code:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: '.$email. "\r\n";
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully.";
}
else
{
echo "Error in Mail Sent.";
}
?>
My Contact form doesnt email the phone number. I have the field in the form and in the script also.
<form id="contact" action="">
<fieldset>
<label for="name" id="name_label">Name <br /></label>
<input type="text" name="name" id="name" value="" class="text-input" />
<label for="email" id="email_label">E-mail<br /></label>
<input type="text" name="email" id="email" value="" class="text-input" />
<label for="phone" id="phone_label">Phone number<br /></label>
<input type="text" name="phone" id="phone" value="" class="text-input" />
<label for="msg" id="msg_label">Preferred contact time</label>
<textarea cols="60" rows="10" name="msg" id="msg" ></textarea> <br class="clear" />
<input type="submit" name="submit" class="button" id="submit_btn" value="Send Message →"/>
</fieldset>
</form>
and the following PHP script:
<?php
$emailTo = 'test#test.co.uk';
$subject = 'Advanced Contact Form';
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$phone=$_REQUEST['phone'];
$msg=$_REQUEST['msg'];
$body = "Name: $name \n\nEmail: $email \n\nphone: $phone \n\nMessage: $msg";
$headers = 'From: '.$name.' <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
echo 'Mail sent';
?>
The email sends fine but the phone number doesnt show up in the email.
Any ideas what the issue could be?
EDIT: my url looks like this:
www.website.com/contact.html?name=test&email=test%40test.com&phone=0112+2255555&msg=2312312312312&submit=Send+Message+%26%238594%3B
Here is a basic example of how you should process a form for email, first process the values and build upon an error array if any.
Then send, NEVER Trust user input especially contact forms.
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$emailTo = 'test#test.co.uk';
$subject = 'Advanced Contact Form';
$name=(isset($_POST['name'])?$_POST['name']:null);
$email=(isset($_POST['email'])?$_POST['email']:null);
$phone=(isset($_POST['phone'])?$_POST['phone']:null);
$msg=(isset($_POST['msg'])?$_POST['msg']:null);
$error=array();
$cont=true;
//name
if(isset($name)){
if(strlen($name)<=1){
$cont=false;
$error['error_name']='Enter your real name!';
}else{
$name=preg_replace('/[^a-zA-Z0-9\(\)\:\?.\&,_ -]/s', '', $name);
}
}else{
$error['error_name']='Enter your name!';
$cont=false;
}
//email
if(isset($email)){
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
}else{
$cont=false;
$error['error_email']='Email Invalid';
}
}else{
$cont=false;
$error['error_email']='Please enter your email!';
}
//phone
if(isset($phone)){
if(strlen($phone)<=1){
$cont=false;
$error['error_phone']='Enter your real number!';
}else{
$phone=preg_replace('/[^0-9]/s', '', $phone);
}
}else{
$error['error_phone']='Enter your number!';
$cont=false;
}
//message
if(isset($msg)){
if(strlen($msg)<=1){
$cont=false;
$error['error_message']='Enter your real message!';
}else{
$msg=preg_replace('/[^a-zA-Z0-9\(\)\:\?.\&,_ -]/s', '', $msg);
}
}else{
$error['error_message']='Enter your message!';
$cont=false;
}
if(empty($error) && $cont ==true){
//send mail
$body = "Name: $name \n\n
Email: $email \n\n
phone: $phone \n\n
Message: $msg";
// Additional headers
$headers ='MIME-Version: 1.0'."\r\n";
$headers.='Content-type: text/html; charset=iso-8859-1'."\r\n";
$headers.='Reply-To: '.$email."\r\n";
$headers.='From: '.$name.' <'.$email.'>'."\r\n";
$headers.="X-Mailer: Remote Mail\r\n";
// Mail it
$status = (mail($emailTo, $subject, $body, $headers))?'Mail Was Sent':'Error Sending email';
}else{
//error with form values
$status='';
foreach($error as $type=>$reason){
$status .= $reason.'<br />';
}
}
//Echo your form or status page
echo $status;
}else{
//Echo your form
}
?>