php mail form doesn't work [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I think I fixed this. This was the original:
<?php
if(isset($_POST['email'])) {
$email = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];;
$subject = $email = $_POST['subject'];;
$email_to = $email;
$header = 'From: 1totheN <website#1tothen.com>' . "\r\n";
function clean_text($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_text($email)."\n";
$email_message .= "Email: ".clean_text($email)."\n";
$email_message .= "Message: ".clean_text($message)."\n";
#mail($email_to, $subject, $email_message, $header);
echo "Your message has been sent.";
}
?>
Here's the markup on the HTML page:
<form class="contact-form" id="subscribeForm" action="mail.php" method="post">
<input type="text" name="name" class="required" placeholder="YOUR NAME">
<input type="text" name="email" class="required email" placeholder="EMAIL ADDRESS">
<input type="text" name="subject" class="required last" placeholder="SUBJECT">
<textarea name="message" placeholder="YOUR MESSAGE"></textarea>
<input type="submit" name="submit" value="SUBMIT YOUR MESSAGE">
<h6 id="form_result">Your email will not be published.</h6>
</form>
This is the fix:
$email_to = "webmail#1tothen.com";
$header = 'From: 1totheN <webmail#1tothen.com>' . "\r\n";
$email_message .= "Name: ".clean_text($name)."\n";
I think that fixes it. I added an email to the $email_to field, and added the $name. Seems to work.

The below now works. It's just small syntactic mistakes, you had a few additional ; and this line is wrong and presumably a mistake: $subject = $email = $_POST['subject'];
I also moved the function outside of the if statement...it's not incorrect to have it there, it just looks a bit weird.
function clean_text($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
if(isset($_POST['email'])) {
$email = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$email_to = $email;
$header = 'From: 1totheN <website#1tothen.com>' . "\r\n";
$email_message .= "Name: ".clean_text($email)."\n";
$email_message .= "Email: ".clean_text($email)."\n";
$email_message .= "Message: ".clean_text($message)."\n";
#mail($email_to, $subject, $email_message, $header);
echo "Your message has been sent.";
}
?>

Related

Difficulty connecting a contact form using PHP

I've tried multiple different ways to set up a contact form and connect it to an email address using PHP. This is my first time using PHP, however I know I've written the .php form correctly.
Whenever I send the form, I get redirected to an "Error 405" page.
I've Googled for a few hours, and some websites give confusing instructions about downloading PHP (which I have tried about 5 times with no success), others say that VS Code on Mac should already be able to use PHP.
I'll leave my HTML and PHP forms below (I've tried two different PHP forms with no success).
It is implied that this should be a very easy process, simply connect the HTML to PHP via "action: mail.php" attribute in the form tag, and that should be everything. Yet I have scoured the internet and have had no luck so far... Any help would be appreciated!
(I'll leave the code below, first is HTML and second/third are the two different PHP forms I have tried).
The link to my Github is here as well: "https://github.com/cjmaret/abt-website"
<form class="contact-form__form-section" action="mail.php" method="POST" accept-charset="UTF-8">
<h2 class="contact-form__form-section__title">Get In Touch</h2>
<p class="contact-form__form-section__subtitle">Fill out the form or call ###-###-#### to request an
estimate or more information. We look forward to assisting you!</p>
<fieldset class="fieldset">
<select class="input input_select" id="dropdown" name="dropdown" required>
<option value>Subject*</option>
<option value="accounts-receivable">Accounts Receievable</option>
<option value="accounts-payable">Accounts Payable</option>
<option value="human-resources">Human Resources</option>
<option value="quotes-estimates">Quotes/Estimates</option>
<option value="w9-certificates">W9/Certificates of Insurance</option>
<option value="careers">Careers</option>
<option value="general-feedback">General Feedback</option>
<option value="request-service">Request Service</option>
</select>
<div class="input">
<input class="text-input" type="text" name="name" placeholder="Name*" required>
</div>
<div class="input">
<input class="text-input" type="text" name="company" placeholder="Company">
</div>
<div class="input">
<input class="text-input" type="email" name="email" placeholder="Email*" required>
</div>
<div class="input">
<input class="text-input" type="tel" name="phone" placeholder="Phone">
</div>
<div class="input">
<textarea class="textarea" type="text" name="message" placeholder="How Can We Help?"></textarea>
</div>
<div class="g-recaptcha" data-sitekey="#########################"></div>
<button type="submit" class="button-primary button-primary_place_contact-form">
<p class="button-primary__text button-primary__text_place_contact-form">Get Started</p>
</button>
</fieldset>
</form>
<?php
if (isset($_POST['Email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "cjmaret#gmail.com";
$email_subject = "New form submissions";
function problem($error)
{
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br><br>";
echo $error . "<br><br>";
echo "Please go back and fix these errors.<br><br>";
die();
}
// validation expected data exists
if (
!isset($_POST['Name']) ||
!isset($_POST['Email']) ||
!isset($_POST['Message'])
) {
problem('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['Name']; // required
$email = $_POST['Email']; // required
$message = $_POST['Message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email)) {
$error_message .= 'The Email address you entered does not appear to be valid.<br>';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br>';
}
if (strlen($message) < 2) {
$error_message .= 'The Message you entered do not appear to be valid.<br>';
}
if (strlen($error_message) > 0) {
problem($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string)
{
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Name: " . clean_string($name) . "\n";
$email_message .= "Email: " . clean_string($email) . "\n";
$email_message .= "Message: " . clean_string($message) . "\n";
// create email headers
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your success message below -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
#!/usr/bin/php
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Phone: $phone \n Company: $company \n Email: $email \n Subject: $dropdown \n Message: $message";
$recipient = "cjmaret#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

Unable to send mails with php mail function [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$contact = $_POST['num'];
$email = $_POST['email'];
$message = $_POST['message'];
$ToEmail = 'info#kesems.com';
$EmailSubject = 'School Enquiry';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY = "Phone: ".$_POST["num"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
header('Location:contact-us.php');
}
?>
contact-us.php
<form role="form" action="contact-us.php" id="main-contact-form" class="contact-form" name="contact-form" method="post">
<div class="row ml0">
<div class="form-group">
<input type="text" class="form-control" name="name" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="text" class="form-control" name="num" required="required" placeholder="Contact number">
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" required="required" placeholder="Email address">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" name="message" class="form-control" rows="3" placeholder="Any Queries/suggestions" style="resize:none"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" value="Send Message" class="btn btn-primary btn-lg"/>
</div>
</div>
</form>
Simple script that sends email headers...still not working.
is code is correct?
any particular solution for this?
any particular solution for this?
thank you in advance.
Try this it worked for me
// 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: yourmail#gmail.com" . "\r\n" .
"Reply-To: no-reply#gmail.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$to = "tomail#gmail.com";
$subject = "This is subject";
$from = "yourmail#gmail.com";//optional
$message = ' this is my message hello';
if (mail($to, $subject, $message, $headers, 'ADMIN')) {
echo "mail sent"
}
else{
echo "error cannot send mail";
}
Check whether all the arguments are actually set (using ternary), just because the submit is set does not mean that all the arguments are set:
$name = isset($_POST['name']) ? $_POST['name'] : "";
$contact = isset($_POST['num']) ? $_POST['num'] : "";
$email = isset($_POST['email']) ? $_POST['email'] : "";
$message = isset($_POST['message']) ? $_POST['message'] : "";
Check these values and make a condition that you display some error if something is not set.
If you're certain the values are set then you need to change
$ToEmail = 'info#example.com';
to
$ToEmail = $email;
Otherwise nothing's going to happen.
Finally, I recommend identifying your html inputs with a proper id, instead of a name.
Unrelated to the error but yet important to you
Like already pointed out by #Fred -ii-, a proper concatenation (.=) is required instead of overwriting (=) in the second header assignment of $MESSAGE_BODY:
MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY = "Phone: ".$_POST["num"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";
Should be:
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Phone: ".$_POST["num"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";

PHP Message success function [HELP]

anyone help me with my contact form? it will not work, it sends a message but no confirmation of message succes? everything works fine exceptg the Message succesfull send function :(
here are the codes hope someone can help me
Contact.php
<?php
#error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
#ini_set('display_errors', true);
#ini_set('html_errors', true);
#ini_set('error_reporting', E_ALL ^ E_NOTICE ^ E_WARNING);
ini_set("log_errors", 1);
ini_set("error_log", "php-error.log");
error_log( "Hello, errors!" );
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "info#arjen079.com";
$email_subject = "Contact Application Website!";
$email_noreply = "noreply#arjen079.com";
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$subject = $_POST['subject']; // required
$message = $_POST['message']; // required
$email_message = "Form Details Below:\n\n";
$email_messagesender = "Here is a copy of the form you filled in:\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Subject: ".clean_string($subject)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_messagesender .= "First Name: ".clean_string($name)."\n";
$email_messagesender .= "Email: ".clean_string($email)."\n";
$email_messagesender .= "Subject: ".clean_string($subject)."\n";
$email_messagesender .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
// send a copy to sender
$headers = 'From: '.$email_noreply."\r\n".
'Reply-To: '.$email_noreply."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email, "Thanks For contacting!", "Thanks for contacting me, I will reply to you shortly. \n\n----------------\n\n" . $email_messagesender, $headers);
echo 'Message Succesfully Sent!<script>$(\'response-message\').text(\'Message Succesfully Sent!\');var form = document.getElementById("contact-form");form.reset();</script>';
}
?>
Contact form:
<div class="container">
<div class="row">
<div class="span7">
<form action="http://www.arjen079.com/contact.php" method="post" class="contact-form" id="contact-form">
<h1>Contact</h1>
<hr class="fancy-hr">
<input type="text" name="name" placeholder="Name" class="required">
<input type="email" name="email" placeholder="Email" class="required">
<input type="text" name="subject" placeholder="Subject" class="required">
<textarea name="message" placeholder="Message" class="required"></textarea>
<div class="response-message"></div>
<input type="submit" value="Submit" name="submit" class="float-right">
<div class="clear"></div>
</form>
Thanks already! ;)
echo 'Message Succesfully Sent!<script>$(\'response-message\').text(\'Message Succesfully Sent!\');var form = document.getElementById("contact-form");form.reset();</script>';
The error is in this part.
$(\'response-message\') should be $(\'.response-message\').
Notice the . you're missing? You're trying to target the ELEMENT called response-message, not the div with the class of it.

PHP Contact form won't work

I've tried every php contact form tutorial but I can't get any of them to send the email properly. They all just show the code but don't send the email. This is the HTML code I have right now:
<form method="POST" action="scripts/contact.php">
<input id="name" name="name" placeholder="Name*" type="text" /> <br>
<input id="subject" name="subject" placeholder="Subject" type="text" /> <br>
<input id="email_address" name="email" placeholder="Email address*" type="text" /> <br>
<textarea id="message" name="message" placeholder="Your message*"></textarea>
<input id="submit" type="submit" name="submit" value="Send" />
</form>
and this is the php I have:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = "From: $email" . $email;
mail($name,$email,$subject,$message,$headers);
echo "Mail Sent.";
?>
I have decent knowledge of HTMl but next to none of PHP. Any help would be greatly appreciated.
first you be sure that php file is exist in path scripts/contact.php
second look at phpinfo or php.ini for find is smptp server was setting or not
use following php code:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: '.$email. "\r\n";
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully.";
}
else
{
echo "Error in Mail Sent.";
}
?>

Sending a form to email not working

I am trying to make a form that will be sent to my email. But it display errors on submission. Javascript to validate the form is not shown but it works fine.
I am not sure what's wrong with my code below, been trying to figure out all day and reading various threads but to no avail.
Below is my php code to handle the form.
<?php
if(isset($_POST['name'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "me#gmail.com";
$email_subject = "Nexwave Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
$name = $_POST['name']; // required
$designation = $_POST['designation'];
$company = $_POST['company'];
$contact = $_POST['contact'];
$email = $_POST['email']; // not required
$users = $_POST['users']; // required
$error_message = "";
$string_exp = "/^[A-Za-z .'-]+$/";
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Designation: ".clean_string($designation)."\n";
$email_message .= "company: ".clean_string($company)."\n";
$email_message .= "Contact ".clean_string($contact)."\n";
$email_message .= "email: ".clean_string($email)."\n";
$email_message .= "Number of Users: ".clean_string($users)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
echo (int) mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Below is my html code:
<form name="form" action="send_mail.php" onSubmit="return validateForm();" method="POST">
Name<br/><input name="name" type="text" style="width:90%;"/><br/>
Designation<br/><input name="designation" type="text" style="width:90%;"/><br/>
Company<br/><input name="company" type="text" style="width:90%;"/><br/>
Contact Number<br/><input name="contact" type="text" style="width:90%;"/><br/>
Email<br/><input name="email" type="text" style="width:90%;"/><br/>
Number of Users<br/><input name="users" type="text" style="width:90%;"/>
<span style="text-align:right;display:block;width:174px;"><input style="height:25px;margin-top:20px;margin-bottom:10px;background-color:#ffffff;border:0;color:#009110;" type="submit" class="submit" value="Submit" /></span>
</form>
below is the error:
This is what i get even after changing the email_address and email_from.
"; echo $error."
"; echo "Please go back and fix these errors.
"; die(); } $name = $_POST['name']; // required $designation = $_POST['designation']; $company = $_POST['company']; $contact = $_POST['contact']; $email = $_POST['email']; // not required $users = $_POST['users']; // required $error_message = ""; $string_exp = "/^[A-Za-z .'-]+$/"; $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Name: ".clean_string($name)."\n"; $email_message .= "Designation: ".clean_string($designation)."\n"; $email_message .= "company: ".clean_string($company)."\n"; $email_message .= "Contact ".clean_string($contact)."\n"; $email_message .= "email: ".clean_string($email)."\n"; $email_message .= "Number of Users: ".clean_string($users)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email."\r\n" . 'X-Mailer: PHP/' . phpversion(); echo (int) mail($email_to, $email_subject, $email_message, $headers); ?> Thank you for contacting us. We will be in touch with you very soon.
your help is very much appreciated
You have many errors present in this script as mentioned by question comments above.
email form name email_address is not the same as $_POST['email']
you are not defining $email_from before you call it.
I would do some simple testing before posting a question like this and try to echo out all the variables one by one and see what you get. This would show you the two mentioned errors. And if you cannot solve it and still have errors then please post your error message so others can be of more assistance than just it doesn't work.

Categories