PHP mail form not working - php

I am making a feedback form for my website using php, I thought that it was working, but then it took an arrow to the code.
Anyways, the form has 4 fields: "name", "email", "subject", and "message".
I get the "Message failed" alert if all four fields have content.
Seeing as that is the case, I tested with content in 3 of the 4 fields, which gives me 4 combinations:
combo 1 - "name", "email", "subject"
combo 2 - "name", "email", "message"
combo 3 - "name", "subject", "message"
combo 4 - "email", "subject", "message"
The results were as follows:
combo 1 - "Thank you for your message."
combo 2 - "Thank you for your message."
combo 3 - "Message failed."
combo 4 - "Thank you for your message."
Here is the html code I am using:
<form action="contact.php" method="post">
<input type="hidden" name="page" value="contact" />
<input type="hidden" name="req" value="submit" />
Your Name: <input type="text" name="name" />
<br/>
Your Email: <input type="text" name="email" />
<br/>
Subject: <input type="text" name="subject" size="69" />
<br/>
Message:
<br/>
<textarea cols="63" rows="8" name="message"></textarea>
</br>
<input type="submit" value="Send" />
<input type="reset" value="Clear" />
</form>
And here is contact.php:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
$mail_to = 'email#my_website.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '."\n";
$body_message .= $field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for your message.');
window.location = './contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed.');
window.location = './contact.html';
</script>
<?php
}
?>
Is there anything wrong with the code? I don't understand why it is doing this.

Great answer from Anton but I wanted to dig a little bit more into this and expand his answer.
If you want to make sure someone sent the email, require it hardcoded with php (and html5 would be cool too, but this can be avoid so you still need php). I also deleted the javascript bit as it felt irrelevant there. This is what I would try and since it's short I'd do it all in the same page. There are many subtle changes:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
//If all required fields are filled
if (!empty($field_name)&&!empty($field_email)&&!empty($field_message))
{
$mail_to = 'email#my_website.com';
$subject = 'Message from a site visitor: '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '."\n";
$body_message .= $field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
echo "Thank you for your message.";
}
//If not all required fields are filled display form again.
else
{ ?>
Please fill all the required fields.<br>
<form action="" method="post">
<input type="hidden" name="page" value="contact" />
<input type="hidden" name="req" value="submit" />
<?php //The php bits are to retrieve the valid fields ?>
Your Name*: <input required type="text" value="<?php echo $field_name; ?>" name="name"/>
<br/>
Your Email*: <input required type="email" value="<?php echo $field_email; ?>" name="email" />
<br/>
Subject: <input type="text" name="subject" value="<?php echo $field_subject; ?>" size="69" />
<br/>
Message*:
<br/>
<textarea required cols="63" rows="8" name="message"><?php echo $field_message; ?></textarea>
</br>
<input type="submit" value="Send" />
<input type="reset" value="Clear" />
</form>
<?php
}
?>
Otherwise, if you don't care about who sends it, you could just put something like this
$field_name = $_POST['name'];
$field_email = $_POST['email'];
if (empty($field_email)) $field_email="anonymous#MYPAGE.com";
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
Last thing, I hope this is just a pseudo code as it doesn't comply almost any html rule. You need < html > tags, < body > etc...
Just tested and worked great (;

The problem is probably this:
Quote from http://php.net/manual/en/function.mail.php
When sending mail, the mail must contain a From header. This can be
set with the additional_headers parameter, or a default can be set in
php.ini.
Failing to do this will result in an error message similar to Warning:
mail(): "sendmail_from" not set in php.ini or custom "From:" header
missing. The From header sets also Return-Path under Windows.
In other words, if you do not have a header containing a proper "From: adress#email.com" an error will occur.

Related

Trouble with PHP

I'm new to PHP. Basically I'm a graphic designer turned frontend designer. I know html css and simple jquery. I need to include this contact form in a website, but doesn't seem to work. Plz help. If someone could point out if anything is wrong with the code, it would be great help.
Thanks in advance.
This is the html part of the contact form :
<div class="form">
<form action="mail.php" method="POST">
<label>Name</label>
<br>
<input type="text" name="name" placeholder="Type Here">
<br><br><br>
<label>Email</label>
<br>
<input type="email" name="email" placeholder="Type Here">
<br><br><br>
<label>Message</label>
<br>
<textarea name="message" rows="20" cols="20" placeholder="Type Here"> </textarea>
<br><br><br>
<label>*What is 2+2? (Anti-spam)</label>
<br>
<input type="text" name="human" placeholder="Type
<br><br><br><br>
<input id="submit" name="submit" type="submit" value="SUBMIT">
</form>
This is the php :
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" From: $name \n Message: $message";
$recipient = "saurabhdey84#gmail.com";
$subject = "Vibhor Sogani Website Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! Your message has been sent." . "-" . <a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
Change your echo like this,
echo "Thank You! Your message has been sent." . "- <a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
^// no need of contenation here
If you're calling php page, it is just taking blank values, try these changes and let me know,
include_once("home.html");
if(isset($_POST['submit']))
{
**Your php code here**
}
HTML:
Change your text control to
<input type="text" name="human" placeholder="Type the answer"/>
Formatting mistake detected:
<input type="text" name="human" placeholder="Type
<br><br><br><br>
<input id="submit" name="submit" type="submit" value="SUBMIT">
PHP:
Last echo statement should be like this: (missed double-quotes)
echo "Thank You! Your message has been sent." . "-" . "<a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
not like this:
echo "Thank You! Your message has been sent." . "-" . <a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
To be better:
echo "Thank You! Your message has been sent. - <a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
No need any concatenation here!
Check if your POSTS are getting sent to the server script by echoing $_POST['name']. If it is , then it is a problem with the mail() function.
You are using the default php mail() function.It doesnt work without the correct headers. I see that you have used just one 'reply From' header.So you need to look deeper into using mail function(). An alternate for sending emails could be the phpmailer library which is used widely
try this, and put required at closing tags of input
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "maild#gmail.com";
$subject = " contact form" ;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$mesg = '$name $email $message';
$kk=mail($to,$subject,$mesg,$headers);
if($kk){
?>
<script>
window.location.href='thankyou.html';
</script>
}
}
?>
This is how it is usually
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
Like Ive said before , phpmailer library is a more reliable alternate

Email form showing ,PHP Content instead of executing and sending email

I'm trying to get a 'contact us email form' to work on my website but when I press the 'send' button, it doesnt send the email and it shows the entire contents of the .php file thats supposed to make it send? Can anybody help me please? my html page i called 'Contact_Us.html' and the PHP file is called 'contact.php".
Here is the HTML
<form action="contact.php" method="post">
Your name<br>
<input type="text" name="cf_name"><br>
Your e-mail<br>
<input type="text" name="cf_email"><br>
Message<br>
<textarea name="cf_message"></textarea><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
and here is the .PHP
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'user#domain.com';
$subject = 'Message from a visitor on The New Moston Club Wesbite '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you, we have received your message. We will contact you shortly!');
window.location = 'Contact_Us.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to thenewmostonclub#outlook.com sorry for any inconvenience');
window.location = 'Contact_Us.html';
</script>
<?php
}
?>
does it show in your browsers URL bar c:/user/.../yourscript.php if yes then try localhost/yourscript.php

Sending mail directly from a form

I have an Contact us page on my website. what i want is when someone fills the form and click on send button. The message should be arrived to my gmail. i wrote the following code for it. its not working. is there any other way i can accomplish the same.
Html code:
<form id="ContactForm" action="contacts.php" method="post">
<div>
<div class="wrapper"> <strong>Name:</strong>
<div class="bg">
<input type="text" class="input" name="name">
</div>
</div>
<div class="wrapper"> <strong>Email:</strong>
<div class="bg">
<input type="text" class="input" name="email">
</div>
</div>
<div class="textarea_box"> <strong>Message:</strong>
<div class="bg">
<textarea cols="1" rows="1" name="message"></textarea>
</div>
</div>
<span>Send</span> <span>Clear</span> </div>
</form>
php code
<?php
session_start();
$to = "someemail#gmail.com";
$subject = "Someone Tried to contact you";
$message = $_POST['message'];
$fromemail = $_POST['email'];
$fromname = $_POST['name'];
$lt= '<';
$gt= '>';
$sp= ' ';
$from= 'From:';
$headers = $from.$fromname.$sp.$lt.$fromemail.$gt;
mail($to,$subject,$message,$headers);
echo "mail sent";
exit();
?>
Firstly, you should check your inputs for PHP injection.
$message = stripslashes($_POST['message']);
$fromemail = stripslashes($_POST['email']);
$fromname = stripslashes($_POST['name']);
Apart from that, there doesn't seem to be anything wrong with your mail script. The problem is most likely caused from your PHP server. Does your web hosting definitely provide PHP mail? Most free web hosts do not provide this as they are often used for spamming.
Sorry, but your code is crappy (especially, those concatenations). Use Swift mailer which provides OOP-style and does all the header job for you. And make sure you've got any mail server installed (did you check if you have any?).
PHP form:
<?php
header( 'Content-Type: text/html; charset=utf-8' );
// Your Email
$receiver = 'max.mustermann#domain.tld';
if (isset($_POST['send']))
{
$name = $_POST['name']
$email = $_POST['email'];
if ((strlen( $_POST['subject'] ) < 5) || (strlen( $_POST['message'] ) < 5))
{
die( 'Please fill in all fields!' );
}
else
{
$subject = $_POST['subject'];
$message = $_POST['message'];
}
$mailheader = "From: Your Site <noreply#" .$_SERVER['SERVER_NAME']. ">\r\n";
$mailheader .= "Reply-To: " .$name. "<" .$email. ">\r\n";
$mailheader .= "Return-Path: noreply#" .$_SERVER['SERVER_NAME']. "\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
$mailheader .= "Content-Type: text/plain; charset=UTF-8\r\n";
$mailheader .= "Content-Transfer-Encoding: 7bit\r\n";
$mailheader .= "Message-ID: <" .time(). " noreply#" .$_SERVER['SERVER_NAME']. ">\r\n";
$mailheader .= "X-Mailer: PHP v" .phpversion(). "\r\n\r\n";
if (#mail( $receiver, htmlspecialchars( $subject ), $message, $mailheader ))
{
echo 'Email send!';
}
}
?>
HTML form:
<form action="mail.php" method="post">
Name: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Subject: <input type="text" name="subject" /><br />
Message: <textarea name="message" cols="20" rows="2"></textarea><br />
<input name="send" type="submit" value="Send Email" />
</form>

HTML form to PHP script not sending mail

I'm using a template to create my website and it came with a contact page and form all set out but it did not have a php contact script so I wrote that up and set it as the action on the html form and it still won't send me anything to my email... which I have set up through gmail ( i changed the domain email exchange DNS to the gmail settings)
in the html contact form i have the following code:
<div id="contact_form"><form method="post" name="contact" action="contact-form-handler.php">
<label for="name">Name:</label> <input type="text" id="name" name="name" class="required input_field" /><div class="cleaner h10"></div>
<label for="email">Email:</label> <input type="text" id="email" name="email" class="validate-email required input_field" /><div class="cleaner h10"></div>
<label for="subject">Subject:</label> <input type="text" name="subject" id="subject" class="input_field" /><div class="cleaner h10"></div>
<label for="text">Message:</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea><div class="cleaner h10"></div>
<input type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l" />
<input type="reset" value="Reset" id="reset" name="reset" class="submit_btn float_r" />
</form>
and the contact-form-handler.php contains this code bellow to process the html form:
<?php
$to = 'info#jamesreborne.co.uk';
$to .= 'damgxx#gmail.com';
// Assigning data from the $_POST array to variables
$name = $_post['sender_name'];
$email = $_post['sender_email'];
$subject = $_post['sender_subject'];
$text = $_post['sender_text'];
// Construct email subject
$content = 'www.jamesreborne.co.uk Message from visitor ' . $name;
// Construct email body
$body_message = 'From: ' . $name . "\r\n";
$body_message .= 'E-mail: ' . $email. "\r\n";
$body_message .= 'Subject: ' . $subject . "\r\n";
$body_message .= 'Message: ' . $text;
// Construct email headers
$headers = 'From: ' . $email . "\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
mail($to, $content, $body_message, $headers);
$mail_sent = mail($to, $content, $body_message, $headers);
if ($mail_sent == true){ ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php }
else { ?>
<script language="javascript" type="text/javascript">
alert('Message not sent. Please, notify the site administrator info#jamesreborne.co.uk');
window.location = 'contact.html';
</script>
<?php
}
?>
if anyone can help that would be great, thanks
$subject = $_POST['subject'];
$text = $_POST['text'];
Also there is no form field for name and email. Add that.
There is also an error in the part where you set recipients' emails - they are not separated so the $to variable is info#jamesreborne.co.ukdamgxx#gmail.com. It should me more like this:
<?php
$to = 'info#jamesreborne.co.uk';
$to .= ', damgxx#gmail.com';
First your $to string adds two emails in wrong way,
it should be:
$to = 'info#jamesreborne.co.uk, ';
$to .= 'damgxx#gmail.com';
Even if you correct that you wont get subject and message value. AFAIK $_POST is case sensetive(please correct if wrong). So you will have to make it $_POST not $_post.
Then the names of the inputs in html form and in php code are not matching. They should be:
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$text = $_POST['text'];
If a input field in form is subject, then
$subject = $_POST['subject'];
NOT
$subject = $_POST['sender_subject'];
EDIT:
If you are still not getting email, then your server might not have mail server installed.
Install postfix and try.

Issue with a PHP Online Order Form

I've created a contact form for a website with multiple fields. The clear button works perfectly, but when I click the Submit button, I inadvertently download the .php file associated with the webpage, instead of sending an email with the order form to the specified address. I'm really new to php here, so any help would be greatly appreciated.
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_quantity = $_POST['cf_quantity'];
$field_which = $_POST['cf_which'];
$field_size = $_POST['cf_size'];
$field_school = $_POST['cf_school'];
$field_college = $_POST['cf_college'];
$field_message = $_POST['cf_message'];
$mail_to = 'xxx#gmail.com';
$subject = 'TShirt Order - xxx.Org '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail Address: '.$field_email."\n";
$body_message .= 'Quantity of Shirts: '.$field_quantity."\n";
$body_message .= 'Shirt Style(s): '.field_which."\n";
$body_message .= 'Size(s): '.field_size."\n";
$body_message .= 'School: '.field_school."\n";
$body_message .= 'College: '.field_college."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for placing an order with xxx.Org. We will contact you shortly.');
window.location = 'contact_page.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to xxx#gmail.com');
window.location = 'contact_page.html';
</script>
<?php
}
?>
the html code is:
<form action="contact.php" method="post">
Full Name:
<input type="text" name="cf_name">
<br />
<br />
Email Address:
<input type="text" name="cf_email">
<br />
<br />
Quantity of TShirts:
<input type="numeric" name="cf_quantity">
<br />
<br />
Which Style(s)?
<input type="text" name="cf_which">
<br />
<br />
Size(s):
<input type="text" name="cf_size">
<br />
<br />
Name of School:
<input type="text" name="cf_school">
<br />
<br />
Name of College:
<input type="text" name="cf_college">
<br />
<br />
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
Are you completely sure the server accepts php? Seams to me the server doesn't understand it should use the php interpeter and instead of activating the php commands it just lets you download the file.
If you're not sure try and make a file with a simple in it, call that and see if it works.
best regards,
Mark

Categories