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.
Related
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";
}
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);
I am trying to create a simple contact form that takes user-inputted data and e-mails it to me. however, when I use the following code and the user presses submit, the page is redirected to a blank page (/form.php) - not even the "Thank you!" shows up on it, nor is the e-mail even sent. Can someone point out any errors I'm making? thanks!
PHP:
<?php
if (isset($_POST['email'])) {
$myEmail = "myemail#gmail.com";
$name = $_POST['name'];
$message = $_POST['message'];
$email = $_POST['email'];
$subject = "Message from $email";
$message = "
Name: $name
E-mail: $email
Message:
$message
";
/* Sends to e-mail. */
mail($myEmail, $subject, $message, "hello");
?>
Thank you!
<?php
}
?>
HTML:
<form id="form" method="post" name="contact-form" action="form.php">
Name: <br>
<input type="text" name="name" /><br><br>
Email:<br>
<input type="text" name="email" /><br><br>
Message:<br>
<textarea name="message" placeholder="Tell me anything!"></textarea><br><br>
<input type="submit" value="Send">
</form>
You're declaring $message with your message (as part of the mail() header) then you're including $message inside it, in turn hashing and bashing your header.
Try this method instead.
<?php
if (isset($_POST['email'])) {
$myEmail = "myemail#gmail.com";
$name = $_POST['name'];
$message = $_POST['message'];
$email = $_POST['email'];
$subject = "Message from $email";
$message2 = "
Name: $name
E-mail: $email
Message:
$message
";
/* Sends to e-mail. */
if(mail($myEmail, $subject, $message2, "hello")){
echo "<b>Thank you</b>"; // HTML bold text.
}
else{
echo "<h2>Sorry, there was an error.</h2>";
}
}
?>
If you want to send email as HTML you need to use the following:
$headers = "From: $email" . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
then change what is above:
if(mail($myEmail, $subject, $message2, "hello"))
to read as:
if(mail($myEmail, $subject, $message2, $headers))
Fixed 2 problems.
<?php
if (isset($_POST['email'])) {
$myEmail = "myemail#gmail.com";
$name = $_POST['name'];
$message = $_POST['message'];
$email = $_POST['email'];
$subject = "Message from $email";
$message = "
Name: ".$name."
E-mail: ".$email."
Message:
".$_POST['message']."
";
/* Sends to e-mail. */
mail($myEmail, $subject, $message);
?>
Thank you!
<?php
}
?>
It's my first time trying to make a contactform. And I've got a few problems
It's works, I get the email, but I don't get the name the name field with me in the email.
HTML:
<form method="post" name="contactform" action="scripts/contact.php">
<h3>Name</h3>
<input type="text" name="name">
<h3>Email Address</h3>
<input type="text" name="email">
<h3>Message</h3>
<textarea name="message"></textarea>
<br/><input class="submit" type="submit" value="Send Form">
</form>
PHP:
<?php
$to = "name#domane.com";
$subject = "Contact Us";
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $name, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "One of the field are not filled as requirred"; }
?>
$name is my problem. I've I have it in, the email comes from hostmaster#domane.com, If I delete it, everything works fine. But I wan't the name to be sent to me. How?
Or should I do it completely different?
Also, if you leave all the fields blank, the "user" doesn't get any error message, and a blank email is sent to me.
Hope you can help me. :)
Michael Berkowski is correct. What you'll need to do is add the name to your message's body (not in the sense of the input name= attribute, rather the body of the email).
Something like this:
<?php
$to = "name#domane.com";
$subject = "Contact Us";
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$headers = "From: $email";
$body = "Name: $name\r\n";
$body .= "Message: $message";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "One of the field are not filled as requirred"; }
?>
Revised:
HTML:
<form method="post" name="contactform" action="scripts/contact.php">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
<label for="email">Email Address</label>
<input type="text" name="email" id="email" />
<label for="message">Message</label>
<textarea name="message" id="message"></textarea>
<br/><input class="submit" type="submit" value="Send Form" />
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$body = "Name: $name\r\n";
$body .= "Message: $message";
$to = "name#domane.com";
$from = "automailer#mydomainname.com (Website Automailer)";
$subject = "Contact Us";
$headers = "From: $from\r\n" .
"Reply-To: $email ($name)";
$sent = mail($to, $subject, $body, $headers) ;
if($sent) { echo "Your mail was sent successfully"; }
else { echo "One of the field are not filled as requirred"; }
?>
You should read the mail function documentation on php.net.
Have a look at the function signature:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Now you're placing $name as the "$additional_headers" argument. You should pass $name and any extra relevant data in a $message argument instead.
Having that said, here's the correct code to send a message:
$sent = mail($to, $subject, "A message from $name: $message", $headers);
You should read more about how email messages are constructed. Instead of just putting a user defined message in there you probably want to specify some email headers, containing a more beautiful FROM: and the like...
It seems everything is in place:
PHP:
<?php
if (!empty($_POST['name'])){
$msg = "name". $_POST['name'];
}else{
$fname = NULL;
echo "Name Required.<br />";
}
if (!empty($_POST['email'])){
$msg = "email". $_POST['email'];
}else{
$lname = NULL;
echo "Email Required.<br />";
}
if (!empty($_POST['www'])){
$msg = "Website". $_POST['www'];
}else{
$lname = NULL;
echo "Website Required.<br />";
}
if (!empty($_POST['comment'])){
$msg = "Comment". $_POST['comment'];
}else{
$email = NULL;
echo "A comment is required.<br />";
}
$recipient = "myemail#gmail.com";
$subject = "Form Feedback";
$mailheaders = "Reply-to". $_POST['email'];
//send the mail
mail($recipient, $subject, $msg, $mailheaders);
?>
HTML:
<div id="contact" style="height:280px; margin:1px 0;">
<form id="contactLP" method="post" action="inc/php/contact_validate.php">
<div class="align"><input type="text" name="name" tabindex="1" /></div>
<div class="align"><input type="text" name="email" tabindex="2" /></div>
<div class="align"><input type="text" name="www" tabindex="3" /></div>
<div class="align"><textarea id="txta" name="comment" cols="15" rows="5" tabindex="4"></textarea></div>
<span style="color:transparent;">test</span>
<br><br>
<div class="align"><input type="submit" class="submit" name="sendForm" id="SubmitContact" value="" tabindex="5" /></div>
</form>
</div><!--CONTACT-->
When I fill it out correctly and submit, it says "Thanks for your message" or something similiar, but then I get nothing in email.
I tried running this both on a server on the internet, along with on my local server running on my workstation.
Am I doing something wrong above???????
Yes, you are "name; $_POST['name'] "; should be "name". $_POST['name']; in every instance you use that string.
Your $msg is only holding the current value.
Try something like this for all your value assignment to $msg variable
$msg .= "Comment". $_POST['comment'];
mail function
You seem to have screwed up the $mailheaders variable slightly (reply-to section), try this code in a stand alone script. If even it fails, you may have to check your mail function and how it is set up on the server. (change the email addresses obviously)
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com';
mail($to, $subject, $message, $headers);