Hi im a newbie so i tried to use some source code to create a contact form for my website it does not work so i need help here is my code:
HTML
<div class="col-md-6 col-sm-6">
<div class="row contact-form">
<form id="contact-form" action="php/mail.php">
<fieldset class="col-md-6 col-sm-6">
<input id="name" type="text" name="name" placeholder="Name">
</fieldset>
<fieldset class="col-md-6 col-sm-6">
<input type="email" name="email" id="email" placeholder="Email">
</fieldset>
<fieldset class="col-md-12">
<input type="text" name="subject" id="subject" placeholder="Subject">
</fieldset>
<fieldset class="col-md-12">
<textarea name="comments" id="comments" placeholder="Message"></textarea>
</fieldset>
<fieldset class="col-md-12">
<input type="submit" name="send" value="Send Message" id="submit" class="button">
</fieldset>
</form>
</div> <!-- /.contact-form -->
</div> <!-- /.col-md-6 -->
PHP
<?php
include 'functions.php';
if (!empty($_POST)){
$data['success'] = true;
$_POST = multiDimensionalArrayMap('cleanEvilTags', $_POST);
$_POST = multiDimensionalArrayMap('cleanData', $_POST);
// your email adress
$emailTo ="brinny#abvconstruction.co.za"; // "yourmail#yoursite.com";
// from email adress
$emailFrom ="contact#yoursite.com"; // "contact#yoursite.com";
// email subject
$emailSubject = "Mail from Web Contact Form ";
$name = $_POST["name"];
$email = $_POST["email"];
$comment = $_POST["comment"];
if($name == "")
$data['success'] = false;
if (!preg_match("/^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email))
$data['success'] = false;
if($comment == "")
$data['success'] = false;
if($data['success'] == true){
$message = "NAME: $name<br>
EMAIL: $email<br>
COMMENT: $comment";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html; charset=utf-8" . "\r\n";
$headers .= "From: <$emailFrom>" . "\r\n";
mail($emailTo, $emailSubject, $message, $headers);
$data['success'] = true;
echo json_encode($data);
}
}
Thank you. I changed the code to this :
<?php
include 'functions.php';
if (!empty($_POST)){
$data['success'] = true;
//your email adress
$emailTo ="brinny#abvconstruction.co.za"; //"yourmail#yoursite.com";
//from email adress
$emailFrom = $_POST["email"]; //"contact#yoursite.com";
//email subject
$emailSubject = $_POST["subject"];
$name = $_POST["name"];
$email = $_POST["email"];
$comments = $_POST["comments"];
if($name == "")
$data['success'] = false;
if (!preg_match("/^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email))
$data['success'] = false;
if($comments == "")
$data['success'] = false;
if($data['success'] == true){
$message = "NAME: $name<br>
EMAIL: $email<br>
COMMENT: $comments";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html; charset=utf-8" . "\r\n";
$headers .= "From: <$emailFrom>" . "\r\n";
mail($emailTo, $emailSubject, $message, $headers);
$data['success'] = true;
echo json_encode($data);
}`enter code here`
}
I would also like to know if i should use a php_redirect to get the browser to open the html file not the php file
The website is abvconstruction.co.za. If any one can check what is the error on my code
First, you have to add method="post" to your <form>, otherwise it's sending the data with GET.
Also remove these lines, which clear the POST data:
$_POST = multiDimensionalArrayMap('cleanEvilTags', $_POST);
$_POST = multiDimensionalArrayMap('cleanData', $_POST);
first remove these line, they clear the post values
$_POST = multiDimensionalArrayMap('cleanEvilTags', $_POST);
$_POST = multiDimensionalArrayMap('cleanData', $_POST);
Related
Good day,
Newbie here in PHP. I have been working on a website (free template) and got all the functions to work except the Contact Us part of the code. I don't get any errors it just does not send any email to the listed email or send back a response to the sender.
Here is the HTML Side of the code:
<form id="contact-form" action="php/mail.php">
<div class="control-group">
<div class="controls">
<input class="span6" type="text" id="name" name="name" placeholder="* Your name..."/>
<div class="error center" id="err-name">Please enter your name.</div>
</div></div>
<div class="control-group">
<div class="controls">
<input class="span6" type="email" name="email" id="email" placeholder="* Your email..."/>
<div class="error center" id="err-email">Please enter a valid email adress.</div></div></div>
<div class="control-group">
<div class="controls">
<textarea class="span6" name="comment" id="comment" placeholder="* Comments..."></textarea>
<div class="error center" id="err-comment">Please enter your comment.</div>
</div></div>
<div class="control-group">
<div class="controls">
<button id="send-mail" class="message-btn">Send message</button>
</div></div></form>
and this is the mail.php code used:
include 'functions.php';
if (!empty($_POST)) {
$data['success'] = true;
$_POST = multiDimensionalArrayMap('cleanEvilTags', $_POST);
$_POST = multiDimensionalArrayMap('cleanData', $_POST);
//your email adress
$emailTo = "myemail#gmail.com"; //"myemail#gmail.com";
//from email adress
$emailFrom = "myemail#gmail.com"; //"myemail#gmail.com";
//email subject
$emailSubject = "Mail from MyEmail";
$name = $_POST["name"];
$email = $_POST["email"];
$comment = $_POST["comment"];
if ($name == "")
$data['success'] = false;
if (!preg_match("/^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email))
$data['success'] = false;
if ($comment == "")
$data['success'] = false;
if ($data['success'] == true) {
$message = "NAME: $name<br>
EMAIL: $email<br>
COMMENT: $comment";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html; charset=utf-8" . "\r\n";
$headers .= "From: <$emailFrom>" . "\r\n";
mail($emailTo, $emailSubject, $message, $headers);
$data['success'] = true;
echo json_encode($data);
}
}
I am really stuck at this point and this is the only issue I have left hope someone can help point out what I am doing wrong.
Regards,
Rafael
in order for a form to send POST requests you need to specify it by adding the method attribute:
<form id="contact-form" action="php/mail.php" method="post">
Is your php mail script placed in the cgi directory? This is my mail script:
<?php $name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
$subject = "Some subject";
$to = 'info#xxx.de';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: ../contact/thx.html');?>
And it is placed in cgi directory.
After much research and help from CodeGoodie I think the blame lies with the Hosting. I had asked how I could check Server logs to detect the error and they just replied I need to upgrade to be able to use that feature (although it is supposed to be part of the feature already). Thanks for all the help guys. I did learn a lot :)
I've created an HTML5 form, which incorporates reCAPTCHA, and I've also written a PHP script that sends an email when the form is submitted. At the moment, the script redirects the user to an error or thankyou page, but I'm trying to adjust it to dynamically replace the form within a message within the same page.
I've tried the following script, but it displays the message as soon as the page loads, before any user interaction.
PHP/HTML:
<?php
if ($_POST) {
// Load reCAPTCHA library
include_once ("autoload.php");
$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
$emailFrom = $email;
$emailTo = "my#email.com";
$subject = "Contact Request";
// Prepare email body text
$body = "<strong>Name:</strong> $name <br /> <strong>Email:</strong> $email <br /> <strong>Message:</strong> $message";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $name <$emailFrom>" . "\r\n";
$secret = 'XXX';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'],$_SERVER['REMOTE_ADDR']);
echo 'Your message was submitted!';
} else {
?>
<div class="contact-form">
<form role="form" method="post" action="index.php">
<label for="name"><span>Name</span><input type="text" class="input-field" name="name" required data-errormessage-value-missing="Please enter your name." /></label>
<label for="email"><span>Email</span><input type="email" class="input-field" name="email" required data-errormessage-value-missing="Please enter your email address." /></label>
<label for="message"><span>Message</span><textarea name="message" class="textarea-field" required data-errormessage-value-missing="Please enter your message."></textarea></label>
<label><span> </span><div id="recaptcha"><div class="g-recaptcha" data-sitekey="6LcBawsTAAAAAKBPfGs1jApXNRLvR2MIPng0Fxol"></div></div></label>
<label><span> </span><input type="submit" value="" class="submit-button" /></label>
</form>
</div>
<?php
}
?>
I'm new to PHP, so I'm not sure if it's a syntax or semantics issue. Any help would be greatly appreciated!
Here's one way of doing it.
Check to see if the form has been submitted with if(isset($_POST['submit'])). You can also use if($_SERVER['REQUEST_METHOD'] == 'POST') to see if the form has been submitted.
Then we check if the email has been successfully sent, and if it has we set the $success_message variable.
We then check to see if the $success_message variable is set, and if it isn't, we show the form.
Also, note that I added name="submit" to the submit button element. This is how we're checking to see if the form has been submitted.
I also changed stripslashes() to strip_tags() to prevent any malicious code from getting through.
<?php
// Load reCAPTCHA library
include_once ("autoload.php");
if(isset($_POST['submit'])) {
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = trim(strip_tags($_POST['message']));
$emailFrom = $email;
$emailTo = "my#email.com";
$subject = "Contact Request";
// Prepare email body text
$body = "<strong>Name:</strong> $name <br /> <strong>Email:</strong> $email <br /> <strong>Message:</strong> $message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $name <$emailFrom>" . "\r\n";
$secret = 'XXX';
$lang = 'en';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'],$_SERVER['REMOTE_ADDR']);
// EDIT: repositioned recaptcha from OP's PasteBin script, as requested and adjusted messaging
// changed $success var to $message and added error message
// Original if statement, which redirected the user
if($resp->isSuccess()){
// send the email
if(mail($emailFrom, $subject, $body, $headers)) {
// set the success message
$success_message = 'The form was sent! Yay!';
} else {
// error message
$error_message = 'Could not send email';
}
} else {
$error_message = 'Prove you are a human!';
}
}
?>
<div>
<!-- quick and dirty way to print messages -->
<?php if(isset($success_message)) { echo $success_message; } ?>
<?php if(isset($error_message)) { echo $error_message; } ?>
</div>
<?php if(!isset($success_message)): ?>
<div class="contact-form">
<form role="form" method="post" action="index.php">
<label for="name"><span>Name</span><input type="text" class="input-field" name="name" required data-errormessage-value-missing="Please enter your name." /></label>
<label for="email"><span>Email</span><input type="email" class="input-field" name="email" required data-errormessage-value-missing="Please enter your email address." /></label>
<label for="message"><span>Message</span><textarea name="message" class="textarea-field" required data-errormessage-value-missing="Please enter your message."></textarea></label>
<div class="g-recaptcha" data-sitekey="6LcBawsTAAAAAKBPfGs1jApXNRLvR2MIPng0Fxol"></div>
<script type="text/javascript"
src="https://www.google.com/recaptcha/api.js?hl=<?php echo $lang; ?>">
</script>
<label><span> </span><input type="submit" name="submit" value="" class="submit-button" /></label>
</form>
</div>
<?php endif; ?>
Currently:
I have the PHP form below, where users select one of the checkboxes a notification comes as to which location they are looking to book.
Goal:
I would like it to now be that when someone clicks Canterbury an email is sent to email1#gmail.com, when to Broadstairs - email2#gmail.com, and when to deal - email3#gmail.com
I have been going in circles trying to make the changes but I get in a mess.
FYI I don't mind changing the checkboxes into a dropdown, but I would rather keep it this way.
Current PHP section:
<?php
ini_set('display_errors',0);
error_reporting(E_ALL);
$error = '';
if(isset($_POST['send']))
{
$name = $_REQUEST['Name'];
$telephone = $_REQUEST['Telephone'];
$email = $_REQUEST['Email'];
$message = $_REQUEST['Message'];
$headers = "From: $email";
$subject = "Web Contact Data";
$selectedProjects = 'None';
if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){
$selectedProjects = implode(', ', $_POST['projects']);
}
$fields = array();
$fields["Name"] = $name;
$fields["Telephone"] = $telephone;
$fields["Email"] = $email;
$fields["Message"] = $message;
$fields["Location"] = $selectedProjects;
$to = "email4#gmail.com" ; // change all the following to $_POST
$body = "You have recieved the following information:\n\n";
foreach($fields as $key => $value)
{
$body .= sprintf("%20s: %s\n",$key,$value);
} // end foreach($fields as $key => $value)
$subject2 = "Thank you for contacting us.";
$autoreply = "<html><body><p>Dear " . $name . ",</p><p>Message goes here.</p></body></html>";
$headers2 = 'MIME-Version: 1.0' . "\r\n";
$headers2 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers2 .= 'From: email4#gmail.com' . "\r\n";
$send=false;
if($name == '')
{
$error = "You did not enter your name, please try again.";
selected_values();
}
$send=false;
if($telephone == '')
{
$error = "You did not enter your telephone number, please try again.";
selected_values();
}
elseif(!preg_match("/^[[:alnum:]][a-z0-9_.'+-]*#[a-z0-9-]+(\.[a-z0-9-]{2,})+$/", $email))
{
$error = "You did not enter a valid email address, please try again.";
selected_values();
}
else
{
$send = mail($to, $subject, $body, $headers);
$send2 = mail($email, $subject2, $autoreply, $headers2);
if(!isset($error) && !$send)
{
$error = "We have encountered an error sending your mail, please notify [email]email4#gmail.com[/email].";
}
else
{
unset($_REQUEST['Name']);
unset($_REQUEST['Email']);
unset($_REQUEST['Telephone']);
unset($_REQUEST['Message']);
}
} // end else
}// end of if(isset($_POST['send']))
?>
And the HTML form is this:
<form method="post" action="./lead.php">
<ul>
<li>
<?php
if(isset($error))
echo '<div class="register_error">'.$error.'</div>';
if(isset($send) && $send== true){
=echo '<div class="contact-send-green">Thank you for your message.</div>';
}
if(!isset($_POST['send']) || isset($error))
?>
</li>
<li>
<input type="checkbox" name="projects[]" value="Broadstairs">
<label for="type2">Broadstairs</label>
<br /><br />
<input type="checkbox" name="projects[]" value="Canterbury">
<label for="type2">Canterbury</label>
<br /><br />
<input type="checkbox" name="projects[]" value="Deal">
<label for="type2">Deal</label>
</li>
<li>
<textarea name="Message" rows="5" cols="29"><?php if(isset($_REQUEST['Message'])) echo $_REQUEST['Message']; ?></textarea>
</li>
<li>
<input type="submit" name="send" value="Send Message" id="send-email">
</li>
</ul>
For single selection a simple switch(){} would do the magic.
switch($_POST['projects']){
case "Broadstairs":
$to = "email#email.com";
break;
case "Canterbury":
$to = "email#email.com";
break;
case "Deal":
$to = "email#email.com";
break;
}
And then remove the [] from the input names as you won't need them to be a array. (As such:)
<input type="checkbox" name="projects" value="Broadstairs">
And then perhaps considering using some JS/jQuery to check that only 1 is selected at a time?
There are 3 issues with the script you have provided
You want to select only a single email address however, you are using the name projects[] as the checkbox names - this will create an array, however since HTML5; if you use a name multiple times even without the [] it will create an array.
Given the above, you should most definitely as you mentioned change to a <select> box, or alternatively <radio> buttons.
You should not use $_REQUEST unless absolutely needed, you are providing an easy oppurtunity for sql injection.
Here is an example of how I would do it.
HTML
<form method="post" action="./lead.php">
<ul>
<li>
<?php
if(isset($error)) echo '<div class="register_error">'.$error.'</div>';
if(isset($send) && $send== true) echo '<div class="contact-send-green">Thank you for your message.</div>';
?>
</li>
<li>
<label for="Broadstairs"><input type="radio" name="projects" id='Broadstairs' value="Broadstairs"> Broadstairs</label>
<label for="Canterbury"><input type="radio" name="projects" id='Canterbury' value="Canterbury">Canterbury</label>
<label for="Deal"><input type="radio" name="projects" id='Deal' value="Deal">Deal</label>
</li>
<li>
<textarea name="Message" rows="5" cols="29"><?=(strlen($_POST['Message'])>0) ? $_POST['Message'] : ''; ?></textarea>
</li>
<li>
<button type='submit'>Send Message</button>
</li>
</ul>
And your PHP
<?php
if (!empty($_POST)) {
// Dispatch message to selected office
$name = $_POST['Name'];
$telephone = $_POST['Telephone'];
$email = $_POST['Email'];
$message = $_POST['Message'];
$subject = "Web Contact Data";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: My website <my-email#example.com>' . "\r\n";
if ($_POST['projects'] == 'Canterbury') $to = "example#gmail.com" ;
elseif ($_POST['projects'] == 'Broadstairs') $to = "example#gmail.com" ;
else $to = "example#gmail.com" ;
mail($to, $subject, $message, $headers);
// Dispatch "Thank You" email
$subject = "Your message has been received!";
ob_start();
?>
<html>
<body>
<p>Dear <?=$name?></p>
<p>Thank you for contacting us,</p>
<p>We have received your message and will respond to you as soon as possible.</p>
<p>This is an automatic response to your inquiry and it is not necessary to reply.</p>
<p>Thank you,</p>
</body>
</html>
<?
$message = ob_get_flush();
mail($email, $subject, $message, $headers)
}
?>
I would recommend Google Recaptcha too.
Hope this helps
You need to add if, elseif, and else condition to your code.
Replace $to = "email1#email.com" ; with following code
if($_POST['projects'] == 'Broadstairs')
{
$to = "email2#email.com";
}
else if($_POST['projects'] == 'Canterbury')
{
$to = "email3#email.com";
}
else if($_POST['projects'] == 'Deal')
{
$to = "email4#email.com";
}
else
{
$to = "email5#email.com";
}
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I copied the message form and PHP mail from a website. But it doesn't seem to work. It does not send anything or make any reaction. I tried to find the error, but I am not familiar with PHP. I tried editing the $emailFrom =... to $_POST['email']; but that doesn't work either..
HTML:
<div id="form-main">
<div id="form-div">
<form class="form" id="form1">
<p class="name">
<input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Naam"/>
</p>
<p class="email">
<input name="email" type="text" class="validate[required,custom[email]] feedback-input" placeholder="E-mail" />
</p>
<p class="text">
<textarea name="text" class="validate[required,length[6,300]] feedback-input" placeholder="Bericht"></textarea>
</p>
<div class="submit">
<input type="submit" value="Verstuur" id="button-blue"/>
<div class="ease"></div>
</div>
</form>
</div>
</div>
PHP:
<?php
include 'functions.php';
if (!empty($_POST)){
$data['success'] = true;
$_POST = multiDimensionalArrayMap('cleanEvilTags', $_POST);
$_POST = multiDimensionalArrayMap('cleanData', $_POST);
//your email adress
$emailTo ="lisa-ederveen#hotmail.com"; //"yourmail#yoursite.com";
//from email adress
$emailFrom =$_POST['email']; //"contact#yoursite.com";
//email subject
$emailSubject = "Mail from Porta";
$name = $_POST["name"];
$email = $_POST["email"];
$comment = $_POST["comment"];
if($name == "")
$data['success'] = false;
if (!preg_match("/^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email))
$data['success'] = false;
if($comment == "")
$data['success'] = false;
if($data['success'] == true){
$message = "NAME: $name<br>
EMAIL: $email<br>
COMMENT: $comment";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html; charset=utf-8" . "\r\n";
$headers .= "From: <$emailFrom>" . "\r\n";
mail($emailTo, $emailSubject, $message, $headers);
$data['success'] = true;
echo json_encode($data);
}
}
?>
Your form was incomplete, it missed the method (POST) and action (your php filename)
Try this instead:
<div id="form-main">
<div id="form-div">
<form action="sendEmail.php" method="POST" class="form" id="form1">
<p class="name">
<input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Naam"/>
</p>
<p class="email">
<input name="email" type="text" class="validate[required,custom[email]] feedback-input" placeholder="E-mail" />
</p>
<p class="text">
<textarea name="comment" class="validate[required,length[6,300]] feedback-input" placeholder="Bericht"></textarea>
</p>
<div class="submit">
<input type="submit" value="Verstuur" id="button-blue"/>
<div class="ease"></div>
</div>
</form>
</div>
</div>
sendEmail.php
<?php
//include 'functions.php';
if (!empty($_POST)){
$data['success'] = true;
//$_POST = multiDimensionalArrayMap('cleanEvilTags', $_POST);
//$_POST = multiDimensionalArrayMap('cleanData', $_POST);
//your email adress
$emailTo ="lisa-ederveen#hotmail.com"; //"yourmail#yoursite.com";
//from email adress
$emailFrom =$_POST['email']; //"contact#yoursite.com";
//email subject
$emailSubject = "Mail from Porta";
$name = $_POST["name"];
$email = $_POST["email"];
$comment = $_POST["comment"];
if($name == "")
$data['success'] = false;
if (!preg_match("/^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email))
$data['success'] = false;
if($comment == "")
$data['success'] = false;
if($data['success'] == true){
$message = "NAME: $name<br>
EMAIL: $email<br>
COMMENT: $comment";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html; charset=utf-8" . "\r\n";
$headers .= "From: <$emailFrom>" . "\r\n";
mail($emailTo, $emailSubject, $message, $headers);
$data['success'] = true;
echo json_encode($data);
}
}
?>
Firstly, your form: <form class="form" id="form1">
Forms default to GET if a method isn't specifically instructed.
Use POST like this if your HTML form and PHP are inside the same file:
<form class="form" id="form1" method="post">
since you are using POST arrays.
or
<form class="form" id="form1" method="post" action="your_handler.php">
if using a different file; I used your_handler.php as an example filename.
Also, <textarea name="text"...
that should be <textarea name="comment" as per your $_POST["comment"] array.
Using error reporting would have trigged an Undefined index text... notice.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
Footnotes:
I have no idea what multiDimensionalArrayMap('cleanEvilTags' does, so you'll have to check that.
If you're still not receiving mail, check your Spam.
Doing:
if(mail($emailTo, $emailSubject, $message, $headers)){
echo "Mail sent.";
}
and if it echoes "Mail sent", then mail() would have done its job. Once it goes, it's out of your hands.
You could look into using PHPMailer or Swiftmailer which are better solutions, as is using SMTP mailing.
Now, if (!empty($_POST)){ that isn't a full solution. It is best using a conditional !empty() for all your inputs. Your submit counts as a POST array and should only be relied on using an additional isset() for it.
If you're using this from your own computer, make sure that you've a Webserver installed. We don't know how your script is being used.
If you are using it from your own machine, make sure that PHP is indeed running, properly installed and configured, including any mail-related settings.
Additional notes:
You should also use full and proper bracing for all your conditional statements.
This has none:
if($comment == "")
$data['success'] = false;
which should read as
if($comment == ""){
$data['success'] = false;
}
Same thing for:
if($name == "")
$data['success'] = false;
Not doing so, could have adverse effects.
"I copied the message form and PHP mail from a website."
Again, about multiDimensionalArrayMap('cleanEvilTags'; if you don't have that function, then you will need to get rid of it and use another filter method for your inputs.
Consult the following on PHP.net for various filter options:
http://php.net/manual/en/filter.filters.php
http://php.net/manual/en/function.filter-input.php
http://php.net/manual/en/function.filter-var.php
I am having trouble sending the information from my PHP form to the email address. I am fairly new to PHP. Code is below:
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$myEmail = "shivambh28#gmail.com";
if (empty($name) || empty($subject) || empty($message)) {
$error = 'Please make sure to double check the fields for errors.';
} elseif (!filter_var($email1, FILTER_VALIDATE_EMAIL)) {
$error = 'Email is incorrect';
} else {
$headers .= "From: $email\r\n";
$headers .= "Reply-To: $myEmail\r\n";
$headers .= "Return-Path: $myEmail\r\n";
$headers .= "CC: $email\r\n";
$headers .= "BCC: $myEmail\r\n";
if ( mail($to,$subject,$message,$headers) ) {
$to = $myEmail;
$subject = $subject;
$message = $message;
$from = $email;
$headers = "From:" . $from;
echo "Mail Sent.";
} else {
echo 'failure to send email';
}
}
}
HTML:
<form id="contactForm" class="form-horizontal" action="<?php echo get_option('home'); ?>/email/" method="POST">
<input id="name" name="name" placeholder="Full Name" type="text">
<input id="subject" name="subject" placeholder="Subject" type="text">
<input id="email" name="email" placeholder="Email Address" type="email">
<textarea placeholder="Your Message" id="message" name="message" rows="10"></textarea>
<input type="submit" value="SEND" class="btn btn-primary btn-block">
</form>
NOTE: I am using WP CMS.
Your form is missing the method attribute. edit the code so that your form has method POST.
<form id="contactForm" class="form-horizontal" action="contact.tpl.php" method="POST">
secondly remove one of your mail function calls. if not your email will be sent twice
Form method POST is missing in the form tag.
<form id="contactForm" class="form-horizontal" action="contact.tpl.php" method="post">
You have wrong parameter $to in mail()
Try
....
....
//// use $email here not $to which is not initialised yet
if ( mail($email,$subject,$message,$headers) ) {
is place of
if ( mail($to,$subject,$message,$headers) ) {
Change your code.It was sending mail by 2times
$to = $myEmail;
$subject = $subject;
$message = $message;
$from = $email;
$headers = "From:" . $from;
if ( mail($to,$subject,$message,$headers) ) {
echo "Mail Sent.";
} else {
echo 'failure to send email';
}
And your form method is like POST
<form id="contactForm" class="form-horizontal" action="contact.php" method="post">
And main thing your file name is either contact.php or contact.tpl NOT contact.tpl.php
$to = $myEmail;
$subject = $subject;
$message = $message;
$from = $email;
$headers = "From:" . $from;
if(#mail($to, $subject, $message, $headers)) {
echo "Mail Sent";
} else {
echo "Fail";
}
There's a . (period) missing from the first $headers variable declaration. Might help.