For some reason i cant receive any email from my submit form - is there a problem with my formmail script?
<form action="contact_process.php" method="post" enctype="application/x-www-form-urlencoded" class="three">
<legend><strong>Form</strong></legend>
<fieldset>
<p>
<label for="name">Your Name</label>
<input type="text" name="name"></p>
<p>
<label for="email">Your Email</label>
<input type="text" name="email"></p>
<p>
<label for="subject">Subject</label>
<input type="text" name="subject"></p>
<p>
<label for="EnquiryType">Enquiry Type</label>
<select type="text" name="EnquiryType">
<option value="general">General</option>
<option value="other">Other</option>
</select></p>
<p>
<label for="message">Message</label>
<textarea type="text" name="message" class="msg"></textarea></p>
</fieldset>
<input type="submit" name="submit" value="Submit">
<input type="reset" name="Submit2" value="Reset">
</form>
contact_process.php
<?php
# bool has_injection (String $var, [String $var, ...])
function has_injection () {
$values = func_get_args();
for ($i=0, $count=func_num_args(); $i<$count; $i++) {
if ( stristr($values[$i], "%0A") || stristr($values[$i], "%0D") || stristr($values[$i], "\\r") || stristr($values[$i], "\\n")
|| stristr($values[$i], "Bcc") || stristr($values[$i], "Content-Type") ) {
return true;
}
}
return false;
}
$error = '';
if (isset($_POST) && count($_POST)>0) {
# The form has been submitted
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$EnquiryType = $_POST['EnquiryType'];
$message = $_POST['message'];
if ($name && $email && $subject && $EnquiryType && $message) {
if (has_injection($name, $email, $subject, $EnquiryType, $message)) {
# You've got another spammer at work here
$error = 'No spamming';
exit(0);
}
else {
# It's safe to send the message
mail('my#email.com', $subject, $message, $EnquiryType, $message,"From: $name <$email>");
}
}
else {
$error = 'Please fill in all the forms';
}
}
?>
u send 6 parameters..
i checked that and i get elow error
mail() expects at most 5 parameters, 6 given in C:\xampp\htdocs\parixan\contact_process.php on line 31
see here
update
$message = $_POST['EnquiryType']."\r\n".$_POST['message'];
$headers = 'From: $name <$email>' . "\r\n" .
'Reply-To: $name <$email>' . "\r\n" ;
then use
mail('my#email.com', $subject, $message, $headers);
I made a pretty good PHP email script that sends. I've been programming a website of my own (PHP, HTML) and I'm trying to find a way to receive emails. I've heard about POP3, and others but I can't ever find much on them.
<?php
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$body = "This email was sent from a no-reply email service. \n\n $message";
$headers = "From: $from";
mail($to, $subject, $body, $headers);
?>
Hope that helps ^^
Related
I've been using the same contact form for quite a while:
<?php
if (isset($_POST['submit']) ) {
$name = $_POST['names'];
$email = $_POST['email'];
$message = $_POST['message'];
$robot = $_POST['robot'];
$from = 'online#domain.co.uk';
$to = 'info#domain.co.uk';
$subject = 'Online Enquiry';
$headers = "From: DOMAIN <no-reply#domain.co.uk> \r\n";
$headers .= 'Reply-To:'. $email . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion(); // Sender's Email
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
} ?>
<?php if (isset($_POST['submit'])) {
if ($name != '' && $email != '' && $message !='') {
if ($robot != 'yes') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p class="approved"><strong>Your message has been submitted</strong></p>';
} else {
echo '<p class="warning"><strong>Something went wrong!</strong></p>';
}
} else if ($_POST['submit'] && $robot == 'yes') {
echo '<p class="warning"><strong>Looks like you are spam!</strong></p>';
}
} else {
echo '<p class="warning"><strong>Please complete all fields.</strong></p>';
}
}
?>
<form id="contact" method="post" action="#contact">
<label>Name</label>
<input name="names" type="text" placeholder="NAME">
<label>Email</label>
<input name="email" type="email" placeholder="EMAIL">
<label>Message</label>
<textarea rows="10" name="message" placeholder="MESSAGE"></textarea>
<input id="capture" name="robot" type="checkbox" value="yes">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
It works absolutely fine on other sites but doesn't work on one domain? submit simply returns the error "Something went wrong!". the site is on the same cloud server as many of the other sites that work fine. It's driving me mad because I cant see an error.
Turning debugging on I get the error:
Notice: Undefined index: robot in /form-contact.php on line 6
Ive tried removing the robot check all together and it still doesn't work, but generates no error when debugging?
Can anyone offer any advice?
When your checkbox is not checked - it will not be sent.
Replace
$robot = $_POST['robot'];
to
$robot = isset($_POST['robot']) ? 'yes' : 'no';
Your Condition Matches the case. Try checking the value of $robot on submit.
<input id="capture" name="robot" type="checkbox" value="yes">
if ($robot != 'yes') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p class="approved"><strong>Your message has been submitted</strong></p>';
} else {
echo '<p class="warning"><strong>Something went wrong!</strong></p>';
}
I'm trying to make php contact form work on webhost we are currently using. I found with google this trick to get it work when smtp is off:
http://www.top-answers.net/webhost/web-hosting.html
What I found it only gives me white screen on browser when I upload it to server but as offline it shows the form correctly.
Is there way to make this work or should I seek for another solution?
Thank you
<?php
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$company = trim($_POST['company']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$msg = $_POST['message'];
if (has_header_injection($name) || has_header_injection($company) ||has_header_injection($email) ||has_header_injection($phone)) {
die();
}
if ( !$name || !$company || !$email || !$phone || !$msg ) {
echo '<h4 class="error">Kaikki kentät ovat pakollisia.</h4>Palaa takaisin ja yritä uudelleen';
exit;
}
$to = "info#mydomain.com";
$subject = "$name lähetti viestin lomakkeen kautta";
$message = "Nimi: $name\r\n";
$message .= "Yritys: $company\r\n";
$message .= "Sähköposti: $email\r\n";
$message .= "Puhelin: $phone\r\n";
$message .= "Viesti:\r\n$msg";
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe') {
$message .= "\r\n\r\n$name tilasi uutiskirjeen. Muista lisätä sähköpostilistalle!\r\n";
}
$message = wordwrap($message, 72);
require_once "Mail.php";
$from = "info#mydomain.com"; \\the mail-iD under your domain
$to = "myemail#myemail.com"; \\the TO gmail ID
$subject = "PHP Enquiry form";
$body = $email_message;
$host = "mail.mydomain.com"; \\ your domain mail server
$username = "info#mydomain.com"; \\the mail-id under your domain
$password = "secretsecurepassword"; \\your password for yourid#mydomain.com
$headers = array ('From' => $from, 'To' => $to, 'Reply-To'=> $email_from, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $body);
?>
<h5>Kiitos yhteydenotosta!</h5>
<p>Vastaamme kahden arkipäivän kuluessa.</p>
<?php } else { ?>
<form method="post" action="" id="contact">
<label for="name">Nimi</label>
<input type="text" id="name" name="name">
<label for="company">Yritys</label>
<input type="text" id="company" name="company">
<label for="email">Sähköposti</label>
<input type="email" id="email" name="email">
<label for="phone">Puhelin</label>
<input type="text" id="phone" name="phone">
<label for="message">Viesti</label>
<textarea id="message" name="message"></textarea>
<input type="checkbox" id="subscribe" name="subscribe" value="Subscribe">
<label for="subscribe">Tilaa uutiskirje</label>
<button type="submit" class="button next" name="contact_submit">Lähetä</button>
</form>
<?php } ?>
This question already has answers here:
How to '$_POST' back to the same page or to a different result page in php? [closed]
(3 answers)
Closed 9 years ago.
What I want to do is to create a simple contact form. I have a file called contact.php with the simple html form:
<form action="process-contact.php" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" value="Enviar">
</form>
And I have this php code:
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
$to = "email#domain.com";
if($name != '' && $country != '' && $email != '' && $message != ''){
mail($to, $subject, $message, $headers); //calling php mail function
echo 'Thank\'s for contacting us. We will be answering back soon.<br><br>Go back.';
}else{
echo 'Plese verify all the fields and try to send the form again.<br><br>Go back.';
}
The php code is in another file, but I would like to have the php code on the same file file. Is that possible? How can I do it?
Thank you.
You can put everything inside one file and using action=""
Your headers had errors in them and by using those, the Email ended up in my SPAM box, so I changed those with proper headers and put other details using the $message variable.
I added header('Location: thank_you.php'); and will redirect to that page if all the fields were filled. Create a file called thank_you.php or change it to go to your home page.
I replaced:
if($name != '' && $country != '' && $email != '' && $message != ''){
by:
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['country']) ||
empty($_POST['message'])) {
echo "Fill in all the fields. All marked by an asterisk are mandatory.";
}
Form and PHP handler (tested successfully)
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$to = "email#example.com";
$message = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['country']) ||
empty($_POST['message'])) {
echo "Fill in all the fields. All marked by an asterisk are mandatory.";
}
else {
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers); //calling php mail function
header("Location: thank_you.php");
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" name="submit" value="Enviar">
</form>
</body>
</html>
Or you can use this as your PHP handler
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$to = "email#example.com";
$message = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
if(!empty($_POST['name']) &&
!empty($_POST['email']) &&
!empty($_POST['country']) &&
!empty($_POST['message'])) {
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers); //calling php mail function
echo 'Thanks for contacting us. We will be answering back soon.<br><br>Go back.';
// to prevent re-submission, use header but NOT with echo.
// header("Location: thank_you.php");
}else{
echo 'Please verify all the fields.<br><br>Go back.';
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" name="submit" value="Enviar">
</form>
</body>
</html>
Set the action attribute of the form to the name of the file containing the form and php code and then include all your form processing php code in the following conditional.
if(count($_POST)) > 0)
This will make sure the php code runs only when the form is submitted.
You'll want to use the isset feature of PHP, perhaps.
$name = $_POST['name'];
if(isset($name) && $name != ''){
blah....
}
else {
blah...
}
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...
So I have a HTML form with some PHP but I'm not getting any email whatsoever after submitting it. I can't find the problem! can you help me out?
Tried changing the if(isset($_POST['submit'])) { to 'enviar' but still not working.
tried putting some echos to see where it's not working. the only statement that is stopping at the else statement is stripslashes.
The form snippet:
<div id="contact-wrapper">
<?php if(isset($hasError)) { //If errors are found ?>
<p class="error">Please check if you entered valid information.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p><strong>Email sent with success!</strong></p>
<p>Thank you for using our contact form <strong><?php echo $name;?></strong>, we will contact you soon.</p>
<?php } ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<div>
<label for="name"><strong>Name:</strong></label>
<input type="text" size="50" name="contactname" id="contactname" value="" class="required" />
</div>
<div>
<label for="email"><strong>E-mail:</strong></label>
<input type="text" size="50" name="email" id="email" value="" class="required email" />
</div>
<div>
<label for="subject"><strong>Subject:</strong></label>
<input type="text" size="50" name="subject" id="subject" value="" class="required" />
</div>
<div>
<label for="message"><strong>Message:</strong></label>
<textarea rows="5" cols="50" name="message" id="message" class="required"></textarea>
</div>
<input type="submit" value="enviar" name="submit" id="submit" />
</form>
</div>
and the PHP:
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'myemail#email.com'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
Before getting any email, you have to be sure the email is being sent.
$sent = mail($emailTo, $subject, $body, $headers);
var_dump($sent);
What does this code output when placed in the appropriate place?
Have you tried a simple php page to do a test Email (eg fixed content) and I assume the webserver has email configured and is prepared to relay for your address?
If this is WordPress contact form, for starters:
mail($emailTo, $subject, $body, $headers);
should be:
wp_mail($emailTo, $subject, $body, $headers);
See: wp_mail codex
Now you have to check your e-mail setup, as well as WordPress e-mail setup too.