PHP script not grabbing 'phone number' value from form - php

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
}
?>

Related

How to set PHP variables in email TO header?

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";
}
}
?>

Php mail function sends email every time page is refreshed

I used the mail function to do a contact form and it all works fine. However, I just noticed that after sending an email, every time a refresh the page, even though the fields are empty, an email keeps being sent.
My code looks like this:
<form role="form" method="POST">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<select name="situation" id="situation">
<option>Select Current Situation</option>
<option class="placeholder" value="Unemployed">Unemployed</option>
<option class="placeholder" value="Employed">Employed</option>
</select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject_line = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'myemail#email.co.za';
$to = 'myemail#email.co.za';
$subject = 'SchoemanLaw lead ...';
$body ="From: $name <br/> E-Mail: $email <br/> Mobile: $mobile Subject: $subject_line <br/> Situation: $situation";
//$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers optional/headers
$headers .= "From:$from";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// Check if mobile has been entered
if (!$_POST['mobile']) {
$errMobile = 'Please enter your number';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMobile) {
if (mail($to,$subject,$body,$headers)) {
$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
echo $result;
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
echo $result;
}
}
}
?>
</form>
How can I make the site forget all the details from the input fields once an email is sent?
I tried to follow this question here but I don't seem to be able to make it work on my site
The easiest way is to add an forwarding in your code, like that:
EDIT: at #CD001 commentary
if (mail($to,$subject,$body,$headers)) {
$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
echo $result;
// header('Location: ?successfull-submit'); exit; // this one would fail because above is an output.
echo '<meta http-equiv="refresh" content="0; url=?successfull-submit">'; // its not a good /nice alternative but it "works".
Redirect to ?sent=1 without sending any output. And check 'sent' to determine whether or not to display the success message. Try below (assuming your script is contact.php). Also make sure
contact.php
<?php
$result = '';
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject_line = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'myemail#email.co.za';
$to = 'myemail#email.co.za';
$subject = 'SchoemanLaw lead ...';
$body ="From: $name <br/> E-Mail: $email <br/> Mobile: $mobile Subject: $subject_line <br/> Situation: $situation";
//$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers optional/headers
$headers .= "From:$from";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// Check if mobile has been entered
if (!$_POST['mobile']) {
$errMobile = 'Please enter your number';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMobile) {
if (mail($to,$subject,$body,$headers)) {
//$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
//echo $result;
header('Location:' . 'contact.php?sent=1');
exit;
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
//echo $result;
}
}
}
if(isset($_GET['sent'])) {
$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
}
echo $result;
?>
<form role="form" method="POST">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<select name="situation" id="situation">
<option>Select Current Situation</option>
<option class="placeholder" value="Unemployed">Unemployed</option>
<option class="placeholder" value="Employed">Employed</option>
</select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
try like this,in your success message part..
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject_line = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'myemail#email.co.za';
$to = 'myemail#email.co.za';
$subject = 'SchoemanLaw lead ...';
$body ="From: $name <br/> E-Mail: $email <br/> Mobile: $mobile Subject: $subject_line <br/> Situation: $situation";
//$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers optional/headers
$headers .= "From:$from";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// Check if mobile has been entered
if (!$_POST['mobile']) {
$errMobile = 'Please enter your number';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMobile) {
if (mail($to,$subject,$body,$headers)) {
echo "<script>alert('Mail sent Successfully');</script>";
echo "<script>window.location = 'contact.php';</script>";
} else {
echo "<script>alert('Mail not sent');</script>";
echo "<script>window.location = 'contact.php';</script>";
}
}
}
?>
while redirect to another page you can restrict that duplication problem....
Just try redirecting to another page or another function that discards the old $_POST data.
You could use session variable and reset it after each request.
Or, prevent reloading same page using javascript.
Or, redirect to some other page upon completion (if possible)
You can try to add CSRF token to your page to prevent double submission.
Refer to this link: How to prevent multiple form submission on multiple clicks in PHP
When the user refreshes the page, it is possible that the same parameters are getting posted again. As a result, if (isset($_POST["submit"])) This condition becomes true and mail will be sent every time user reloads.
One solution is to redirect to the same page or to a different page on success full completion.
ie,
if (mail($to,$subject,$body,$headers)) {
$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
echo $result;
}
Instead of the above method, redirect user to the same page or different page and show a message there. If you want to show the same page you can redirect with a flag in the query string as ?show_success_msg= true.
Then do this.
if(isset($_GET['show_success_msg']) && $_GET['show_success_msg'] == 'true') {
$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
echo $result;
}
Complete solution here:
<?php
// Handle PHP code always on Top of the page (ie, Not after your HTML), You cant send headers if you have any content above it.
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject_line = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'myemail#email.co.za';
$to = 'myemail#email.co.za';
$subject = 'SchoemanLaw lead ...';
$body ="From: $name <br/> E-Mail: $email <br/> Mobile: $mobile Subject: $subject_line <br/> Situation: $situation";
//$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers optional/headers
$headers .= "From:$from";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// Check if mobile has been entered
if (!$_POST['mobile']) {
$errMobile = 'Please enter your number';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMobile) {
if (mail($to,$subject,$body,$headers)) {
$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
echo $result;
} else {
header("Location: your_page.php?show_success_msg=true")
}
}
}
?>
<form role="form" method="POST">
<?php if(isset($_GET['show_success_msg']) && $_GET['show_success_msg'] ==
'true') {
$result='<div class="alert alert-success">Thank You ! We will be in touch
soon</div>';
echo $result;
} ?>
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<select name="situation" id="situation">
<option>Select Current Situation</option>
<option class="placeholder" value="Unemployed">Unemployed</option>
<option class="placeholder" value="Employed">Employed</option>
</select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>

sending form info to email

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!

Confirmation Email will not send

So i have this contact form, everything sends fine after the submit button is pressed. However the confirmation email does not seem to send...
I couldnt find the answer on here anywhere, or i would not have asked
The Confirmation email code
<?php
$your_email = "jp.vaughan#icloud.com"; // email address to which the form data will be sent
$subject = "Contact Email";
$thanks_page = "/contact/thankyou.html";
if (isset($_POST["submit"])) {
$nam = $_POST["name"];
$ema = trim($_POST["email"]);
$org = trim($_POST["organisation"]);
$com = $_POST["comments"];
$loadtime = $_POST["loadtime"];
if (get_magic_quotes_gpc()) {
$nam = stripslashes($nam);
$ema = stripslashes($ema);
$org = stripslashes($org);
$com = stripslashes($com);
}
$error_msg=array();
if (empty($nam) || !preg_match("~^[a-z\-'\s]{1,60}$~i", $nam)) {
$error_msg[] = "The name field must contain only letters, spaces, dashes ( - ) and single quotes ( ' )";
}
if (empty($ema) || !filter_var($ema, FILTER_VALIDATE_EMAIL)) {
$error_msg[] = "Your email must have a valid format, such as name#mailhost.com";
}
if (empty($org) || !preg_match("~^[a-z\-'\s]{1,60}$~i", $org)) {
$error_msg[] = "The Organisation must contain only letters, spaces, dashes ( - ) and single quotes ( ' )";
}
$limit = 1000000000000000000000000000000000000;
if (empty($com) || !preg_match("/^[0-9A-Za-z\/-\s'\(\)!\?\.,]+$/", $com) || (strlen($com) > $limit)) {
$error_msg[] = "Your message must contain only letters, digits, spaces and basic punctuation ( ' - , . )";
}
$totaltime = time() - $loadtime;
if($totaltime < 7) {
echo("<p>Please fill in the form before submitting!</p>");
echo '</ul>
<form method="post" action="', $_SERVER['PHP_SELF'], '">
<input placeholder="Your Name*" name="name" type="text" id="name" value="'; if (isset($_POST["name"])) {echo $nam;}; echo '">
<input placeholder="Your Email Address*" name="email" type="email" id="email"'; if (isset($_POST["email"])) {echo $ema;}; echo '">
<input placeholder="Your Organisation*" name="organisation" type="text" id="organisation" value="'; if (isset($_POST["organisation"])) {echo $org;}; echo '">
<textarea placeholder="Your Message" name="comments" rows="5" cols="50" id="comm">'; if (isset($_POST["comments"])) {echo $com;}; echo '</textarea>
<input type="hidden" name="loadtime" value="', time(), '">
<input type="submit" name="submit" value=" SEND" id="submit">
</form>';
exit;
}
if ($error_msg) {
echo '
<p>Unfortunately, your message could not be sent. The form as you filled it out is displayed below. Make sure each field is completed. Please address any issues listed below:</p>
<ul class="err">';
foreach ($error_msg as $err) {
echo '<li>'.$err.'</li>';
}
echo '</ul>
<form method="post" action="', $_SERVER['PHP_SELF'], '">
<input placeholder="Your Name*" name="name" type="text" id="name" value="'; if (isset($_POST["name"])) {echo $nam;}; echo '">
<input placeholder="Your Email Address*" name="email" type="email" id="email"'; if (isset($_POST["email"])) {echo $ema;}; echo '">
<input placeholder="Your Organisation*" name="organisation" type="text" id="organisation" value="'; if (isset($_POST["organisation"])) {echo $org;}; echo '">
<textarea placeholder="Your Message" name="comments" rows="5" cols="50" id="comm">'; if (isset($_POST["comments"])) {echo $com;}; echo '</textarea>
<input type="hidden" name="loadtime" value="', time(), '">
<input type="submit" name="submit" value=" SEND" id="submit">
</form>';
exit();
}
$email_body =
"Name of sender: $nam\n\n" .
"Email of sender: $ema\n\n" .
"Organisaition: $org\n\n" .
"COMMENTS:\n\n" .
"$com" ;
if (!$error_msg) {
mail ($your_email, $subject, $email_body, "From: $nam <$ema>" . "\r\n" . "Reply-To: $nam <$ema>");
die ("Thank you. Your message has been sent to the appropriate person.");
exit();
}
$sendto = $_POST["email"]; // this is the email address collected form the form
$ccto = "jp.vaughan#icloud.com"; //you can cc it to yourself
$subjectCon = "Email Confirmation"; // Subject
$message = "the body of the email - this email is to confirm etc...";
$header = "From: auto-confirm#moibrahimfoundation.org\r\n";
// This is the function to send the email
if(isset($_POST['submit'])) {
mail($sendto, $subjectCon, $message, $header);
}}
?>
you are exiting the code after sending the first email.
if (!$error_msg) {
if (mail ($your_email, $subject, $email_body, "From: $nam <$ema>" . "\r\n" . "Reply-To: $nam <$ema>");
die ("Thank you. Your message has been sent to the appropriate person."); //you are exiting here
exit(); //additional exit here. the second email won't be sent if there is no error.
}
$sendto = $_POST["email"]; // this is the email address collected form the form
$ccto = "jp.vaughan#icloud.com"; //you can cc it to yourself
$subjectCon = "Email Confirmation"; // Subject
$message = "the body of the email - this email is to confirm etc...";
$header = "From: auto-confirm#moibrahimfoundation.org\r\n";
// This is the function to send the email
if(isset($_POST['submit'])) {
mail($sendto, $subjectCon, $message, $header);
should be
$success='';
if (!$error_msg) {
if (mail ($your_email, $subject, $email_body, "From: $nam <$ema>" . "\r\n" . "Reply-To: $nam <$ema>")){
$success="Thank you. Your message has been sent to the appropriate person.";
}else{
$success="Your message cannot be sent";
}
}
$sendto = $_POST["email"]; // this is the email address collected form the form
$ccto = "jp.vaughan#icloud.com"; //you can cc it to yourself
$subjectCon = "Email Confirmation"; // Subject
$message = "the body of the email - this email is to confirm etc...";
$header = "From: auto-confirm#moibrahimfoundation.org\r\n";
// This is the function to send the email
if(isset($_POST['submit'])) {
mail($sendto, $subjectCon, $message, $header);
die($success);
if your first mail is also not sending please check the mail configuration in your php.ini and verify that you can send mail through it.
use this tool for checking ur mail is sending or not...also it will show all parameters of mail function...
Test Tool

Send php email from contact form

Hi i am a newbie to php and am following this tutorial
http://tutorialpot.com/2011/06/fancy-contact-form-with-inline-validation/#comment-1771
i am wondering where do i put in my email address so users can send a email to me
thanks in advance
<?php
function checkLen($str,$len=2) //&len definens the minimun length of the input fields
{
return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
}
function checkEmail($str)
{
return preg_match("/^[\.A-z0-9_\-\+]+[#][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
}
foreach($_POST as $k=>$v)
{
$_POST[$k]=stripslashes($_POST[$k]);
$_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
}
//session names must be same with that in contact form
session_name("tpot_contact");
#session_start();
if (isset($_POST['send'])){
$err = array();
if(!checkLen('name'))
$err[]='The name field is too short or empty!';
if(!checkLen('email'))
$err[]='The email field is too short or empty!';
else if(!checkEmail($_POST['email']))
$err[]='Your email is not valid!';
if(!checkLen('subject'))
$err[]='You have not selected a subject!';
if(!checkLen('message'))
$err[]='The message field is too short or empty!';
if((int)$_POST['captcha'] != $_SESSION['expected'])
$err[]='Wrong security code!';
if(count($err))
{
$_SESSION['errStr'] = implode('<br />',$err);
header('Location: '.$_SERVER['HTTP_REFERER']);
exit();
}
//submission data
$IP=$_SERVER['REMOTE_ADDR'];
$name=$_POST['name'];
$email=$_POST['email'];
$date=(gmdate(" Y/m/d "));
$time = date('H:i:s');
$message=$_POST['message'];
$from="noreply#tutorialpot.com";
$subject = " from ".$_POST['name']." | contact form";
$headers = "From: ".$from."\r\n";
$headers .= "Reply-to: ".$from."\r\n";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//checks whether send to my email address is set
if ($cc == 1) {
$headers .= 'Cc:'. $_POST['email']."\r\n";
}
$msg =
"<p><strong>Name: </strong>" .$name. "</p>
<p><strong>Email Address: </strong>" .$email. "</p>
<p><strong>Enquiry: </strong>" .$_POST['subject']. "</p>
<p><strong>Message: </strong>" .$message. "</p>
<br/> <br/>
<p>This message was sent from the IP Address:" .$ipaddress." on".$date. "at".$time."</p>";
if(#mail($email, $subject, $msg, $headers))
{
$success=array();
$success[]='Your message has been sent! | Thank you';
$_SESSION['sent'] = implode('<br />',$success);
header('Location: '.$_SERVER['HTTP_REFERER']);
exit();
}
else{
$err[]='your message could not be sent due to a network problem please try again.!';
$_SESSION['errStr'] = implode('<br />',$err);
header('Location: '.$_SERVER['HTTP_REFERER']);
exit();
}
}
?>
<div class="fieldContainer">
<label for="name" >*Name: </label>
<input class="validate[required,minSize[3]] input1" id="name" name="name" type="text" autofocus="autofocus" placeholder="NAME"/><br /><br />
<label for="email">*Email</label>
<input class="validate[required,custom[email]] input1" id="email" name="email" type="text" placeholder="EMAIL" /><br /><br />
<label for="subect" >*Subject</label>
<select id="dropdown4" name="subject" class="validate[required] input1">
<option selected="selected" value="">--Choose--</option>
<option value="Quote">Quote</option>
<option value="Suggestion">Suggestion</option>
<option value="Question">Question</option>
<option value="Business Proposal">Business Proposal </option>
<option value="Advertising">Advertising</option>
<option value="Complaint">Complaint</option>
<option value="Other">Other</option>
</select><br /><br />
<label for="message" >*Message</label>
<textarea rows="10" cols="15" name="message" class="validate[required,minSize[3],maxSize[300]] input1" id="message" placeholder=" MESSAGE CONTENTS"></textarea><br /><br />
<legend>*Human Verification (HELP US FIGHT SPAM)</legend>
<label for="captcha">25+9=</label>
<input type="text" class="validate[required,custom[integer]] input1 " name="captcha" id="captcha" maxlength="2" placeholder="DO A LITTLE MATH" />
<p>
<input type='checkbox' id='cc' name='cc' value='1' />
Send a copy to your email address
</p>
</div>
<div class="signupButton">
<input name="send" type="submit" class="btnsubmit" id="btnsubmit" />
<!--<input class="blackb" type="submit" name="send" id="submit"/>-->
</div>
</form>
This tutorial seems to have a few mistakes (at least at a first glance).
It uses $cc however this variable is not defined anywhere.
It sends the message to $email, but $email = $_POST['email'] (line 42) so it sends that e-mail to the e-mail address provided in the form (?). You want to fix line 62 with:
if(#mail('yourem#il.com', $subject, $msg, $headers))
It is also not sanitising the inputs so you can inject headers into the e-mail message. More explained here.
Finally, if the CC functionality was properly implemented (i.e. $cc was defined), you would send a copy of the e-mail to the sender (CC) thus revealing your e-mail address (not good if you want to avoid spam). You should send a separate e-mail to the sender.
It is this section that sends the message as mail returns bool. The first paramters is the to address (see link)
if(#mail($email, $subject, $msg, $headers))
http://php.net/manual/en/function.mail.php
In the example the email will go to the address the user enters as seen as $email is populated with the posted value
$email=$_POST['email'];
but you can hardcode to what ever you want.
if(#mail('youremail#domain.com', $subject, $msg, $headers))

Categories