Make PHP mail form - php

I've an HTML contact form and I want to use it in my footer, but I don't know how to do it!!
Here is my form:
<div class="footer-right">
<p>Contact Us</p>
<form action="#" method="post">
<input type="text" name="email" placeholder="Email" />
<textarea name="message" placeholder="Message"></textarea>
<button>Send</button>
</form>
</div>

The <button> tag will not submit the form. You should change it to: <button type="submit">Send</button>
For handling the form you could add something like this in the beginning of your file::
<?
//Mail sending function
$subject = "Web Form";
$from = $_POST['email'];
$to = "youremail#example.com";
//data
$msg = $_POST['message'];
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
mail($to,$subject,$msg,$headers);
echo "Mail Sent.";
?>

Since you tagged it as PHP, this answer will get you started.
Change:
<button>Send</button>
to:
<input type="submit" value="Send">
Change:
<form action="#" method="post">
to:
<form action="mailer.php" method="post">
Create a file named mailer.php and place it in the same folder as the filename of the document you presented, and in that file, add the following contents:
<?php
$recipient="receipient#example.com";
$subject="new message from ".$_POST['email']." - contact form";
$message=$_POST['message'];
mail($recipient, $subject, $message);
?>
<p>Your message has been sent.</p>
Note that you should add custom HTML between the last two lines of the above code or your HTML won't validate with W3C standards.

Related

How to send the exact edited text using CKEditor

I have created a contact me page that uses CK Editor to edit the content of the mail. I am using xampp/ localhost to test the mail. I want the mail to be exactly like i edit it in the editor
For example the bold part in editor doesnot show up bold in the mail it is sent as
<strong>SOMETHING IN BOLD</strong>
How do i use CK editor or any other so that the mail is sent as
SOMETHING IN BOLD
and not as shown above.
Thank you.
HTML Code
<form action="upload.php" method="post" style="height:100%">
<input name="articleTitle" value="Title" maxlength="50"
id="articleTitle">
<textarea name="article-body">
Compose your article...
</textarea>
<script>
CKEDITOR.inline( 'article-body' );
</script>
<br>
<input name="senderName" type="text" value="Name" maxlength="50"
id="name" class="textBox">
<input name="senderEmail" value="Email address" maxlength="50"
id="email" class="textBox">
<input name="abtyou" value="About Yourself" maxlength="150"
id="abtyou" class="textBox">
<input type="submit" name="submit" class="button">
</form>
PHP Code
<?php
$thankYou="";
if(isset($_POST["submit"])) {
$recipient="zain.farid#live.com";
$subject="New Guest Post";
$sender=$_POST["senderName"];
$senderEmail=$_POST["senderEmail"];
$senderAbout=$_POST["abtyou"];
$message=$_POST["article-body"];
$title=$_POST["articleTitle"];
$mailBody="Name: $sender\nEmail: $senderEmail\nAbout Sender: $senderAbout\nTitle: $title\n\n$message";
mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");
$thankYou="Thank you! Your post has been submitted.";
}
?>
Looks like you're not sending an HTML-header with the email. Here you can see how you can do this: (example 3) http://www.w3schools.com/php/func_mail_mail.asp
<?php
if(isset($_POST["submit"])) {
// ... email settings ...
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: $sender <$senderEmail>" . "\r\n";
mail($recipient, $subject, $mailBody, $headers);
// ...
}
?>

PHP mail() is not accepting variables

For some reason the $name, $email and $message variables aren't being passed into the $msgcontents and $headers variables. After I fill out the form and click submit the message in my inbox looks like this:
Subject: Message from Contact Form
From: Unknown sender
Date: Mon, November 17, 2014 7:44 pm
To: myemail#myemail.com
Name:
Email:
Message:
I've used var_dump() to see if my variables are being populated after clicking submit and they are but for some reason they are not being picked up by $msgcontents and $headers. Where am I going wrong?
Here's my code:
<?php
$name = trim(htmlspecialchars($_POST['name'], ENT_QUOTES));
$email = trim($_POST['email']);
$message = trim(htmlspecialchars($_POST['message'], ENT_QUOTES));
$to = "myemail#myemail.com";
$subject = "Message From Contact Form";
$msgcontents = "Name: $name <br> Email: $email <br> Message: $message";
$headers = array("MIME-VERSION: 1.0",
"Content-type: text/html; charset=iso-8859-1",
"From: $name <$email>",
"Reply-To: contact#info84.com",
"X-Mailer: PHP/" . PHP_VERSION
);
$headers = implode("\r\n", $headers);
$mailsent = mail($to, $subject, $msgcontents, $headers);
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="contactform" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" novalidate>
<input type="text" name="name" placeholder="Your Name">
<input type="email" name="email" placeholder="Your Email">
<textarea name="message" placeholder="Your Message"></textarea>
<input type="submit" name="submitform" value="send">
</form>
<?php
var_dump($to);
?>
<br>
<?php
var_dump($subject);
?>
<br>
<?php
var_dump($msgcontents);
?>
<br>
<?php
var_dump($headers);
?>
<br>
<?php
var_dump($name);
?>
<br>
<?php
var_dump($email);
?>
<br>
<?php
var_dump($message);
?>
</body>
</html>
I think you check the message sended from first page load (without the form)
Try to wrap your email sending code, like this:
if (isset($_POST['submitform'])) {
//...
mail(...);
//...
}
Then, when page loads no message been sended, but when you click a submit button, mail been sended

post to form to same page

Basically I am trying to re work a plugin so what I need to do is post the form data on the Cart.php form on the same page. Below is the set up I have but the $_POST info is not returning anything when email is sent:
Cart.php
<form id='SimpleEcommCartCartForm' action="" method="post">
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
</div>
</div>
</form>
<?php
//Send the email
$to = "test#gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
$send = mail($to, $subject, $emailBody, $headers);
?>
It sends the mail with nothing in it (or missing information) if you load it right away, and it's because of a specific reason.
As it stands, your code will send you an Email as soon as you load the page, so it's best to wrap your (PHP) code inside an isset() to work in conjunction with a (named) submit button, which seems to be missing in your originally posted question/code.
Plus, you have two undefined variables:
$mine and $flygt, so you'll need to define those to fit your needs.
I.e.: if(isset($_POST['submit'])) and <input type="submit" name="submit" value="Send">
Sidenote: It's best to check for empty fields, but that's another topic; see my footnotes.
Tested and working, and receiving all info and I've replaced your present mail function with if(mail($to, $subject, $emailBody, $headers)){...}
<form id='SimpleEcommCartCartForm' action="" method="post">
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
<input type="submit" name="submit" value="Send">
</div>
</div>
</form>
<?php
//Send the email
if(isset($_POST['submit'])){
$to = "test#gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
// $mine = " Mine variable"; // replace this
// $flygt = " Flygt variable"; // replace this
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
// $send = mail($to, $subject, $emailBody, $headers);
if(mail($to, $subject, $emailBody, $headers)){
echo "Mail sent.";
}
else{
echo "Sorry, something went wrong.";
}
} // brace for if(isset($_POST['submit']))
?>
If you're still having problems:
Add error reporting to the top of your file(s) right after your opening <?php tag, which will help during production testing.
error_reporting(E_ALL);
ini_set('display_errors', 1);
Footnotes:
You are open to XSS attacks (Cross-site scripting).
Use the following (PHP) filter function: FILTER_SANITIZE_FULL_SPECIAL_CHARS
$name = filter_var($_POST['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
Equivalent to calling htmlspecialchars() with ENT_QUOTES set. Encoding quotes can be disabled by setting.
To check for empty fields, you can add this below if(isset($_POST['submit'])){
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone'])){
echo "Fill out all the fields";
exit;
}
|| means "OR", which could very well be replaced by OR if you want, however || has precedence over OR.
This is a very basic method; there are other ways of accomplishing this, but you get the gist of it.
The issue here is that the mail is sent before the submit since you didn't test of the existence of $_POST variables, try something like this:
<form id='SimpleEcommCartCartForm' action="" method="post">
<input type="hidden" name="action" value="mailing"/>
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
</div>
<input name="submit" type="submit"/>
</div>
</form>
<?php
if(isset($_POST['mailing'])){
// Send the email
$to = "test#gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
$send = mail($to, $subject, $emailBody, $headers);
}
?>
Once the email is sent, you haven't told PHP to output anything to the page.
You could add something like echo 'Mail sent'; to the end of the PHP so if the script is being executed correctly then you'll know about it.
If the problem lies within the email not being sent at all, this may be a problem with your server and you should look for a tutorial on how to set up mail correctly. If you're using shared hosting, there are some companies that do not allow the use of the PHP mail() function so I would check with them first.
Amongst this I would recommend that you use something like PHPMailer, a full email library for PHP. Then you could configure for your emails to be sent from anywhere that supports SMTP (not sure if others are supported but there most likely is). PHPMailer will work with shared hosting.
Updated - Code that you can try
Try this out in replacement of $emailBody:
$emailBody = <<<EOT
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<h1>Pump Inquiry</h1>
<h3>From: $name<h3>
<h3>Phone: $phone<h3>
<p>Minetuff Parts $mine</p>
<p>Flygt Parts $flygt</p>
</body>
</html>
EOT;
NB: Some email clients only allow the use of inline CSS, so if you did decide to add anything to your <style> tag in the <head>, don't expect it to work very well or at all in some cases.
As someone has mentioned before, you'll also need to protect against cross-site scripting.

Form for sending mail not sending

I have a "tell a friend" pop up email form that allows users to share my page with an email address that they enter. It pops up fine, but I can't get the form to send the email.
html:
<div id="tellfriend">
Close
<form id='tellafriend_form' method="post" action="#sendMessage" name="tellafriend_form">
<label for="name">Your Name:</label>
<input type="text" id="name" name="name" />
<label for="to">Friend's email:</label>
<input type="text" id="to" name="to" />
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject" />
<label for="message">Message:</label>
<textarea id="message" name="message"></textarea>
<input type="submit" name="submit" value="Submit">
</form>
</div><!-- #tellfriend -->
javascript that handles the "pop up":
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script>
$(function() {
$('#tellfriend').hide();
$('#sendMessage').click(function(e) {
$("#tellfriend").fadeToggle('fast');
});
});
</script>
php that's supposed to send the mail:
<?
if (isset($_POST['Submit'])) {
// This will check to see if the form has been submitted
$senders_name = $_POST['name'];
// The person who is submitting the form
$recipient_friend = $_POST['to'];
// The forms recipient
$subject = $_POST['subject'];
// The subject line
$message = $_POST['message'];
// The message being sent
mail($recipient_friend, "From $senders_name", $subject, $message);
if (isset($_POST['your_email'])) {
echo "<br>Your friend has been contacted <br><br>Thank you $senders_name";
}}
?>
Disclaimer: PHP newbie, hoping to learn. Thanks!
The order of your parameters in mail function is not correct. see this
it should be
mail($recipient_friend, $subject, $message);
if you want to use headers then do this
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: '.$recipient_friend.' <'.$recipient_friend.'>' . "\r\n";
$headers .= 'From: '.$sender.' <'.$senderEM.'>' . "\r\n";
Then call mail like this
mail($recipient_friend, $subject, $message, $headers);
You have one error in your PHP code:
if (isset($_POST['Submit'])) {
should be:
if (isset($_POST['submit'])) {
with a lowercase "s".
Indeed the name of you submit button is "submit" but the value is "Submit".
You could eventually do that:
if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') {
And your mail parameters are not correct like boug said.
You have 2 errors
first:
if (isset($_POST['submit']))
// $_POST is case sensitive
second:
if (isset($_POST['your_email']))
// you dont have an inout named 'your_email'

PHP contact form not sending

I have a contact form on a page that sends the details of the form to an email address. You can view it here, www.wonder.ie
The HTML for the form is the following:
<form id="form" name="form27" class="wufoo page" enctype="multipart/form-data" method="post" action="mailer.php">
<ul>
<li id="foli1">
<label class="op" id="title1" for="Field1">Name</label>
<div><input id="Field1" name="name" type="text" class="op required" value="" maxlength="255" tabindex="1" onkeyup="handleInput(this);" onchange="handleInput(this);" /></div>
</li>
<li id="foli2">
<label class="op" id="title2" for="Field2">Email</label>
<div><input id="Field2" name="email" type="text" class="op required email" value="" maxlength="255" tabindex="2" onkeyup="handleInput(this);" onchange="handleInput(this);" /></div>
</li>
<li id="foli3">
<label class="op" id="title3" for="Field3">Inquiry</label>
<div><textarea id="Field3" name="message" class="op required" rows="10" cols="50" tabindex="3" onkeyup="handleInput(this);" onchange="handleInput(this);"></textarea></div>
</li>
</ul>
<input id="button" name="saveForm" class="btTxt submit" type="submit" value="Submit" />
</form>
And for my PHP it is this:
<?php
if(isset($_POST['submit'])) {
$to = "AN_EMAIL#ADDRESS.COM";
$subject = "Email from Wonder.ie";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
} else {
echo "blarg!";
}
?>
Does everything look correct? I know I have the form names matched correctly with the PHP but I can't figure out why I'm not receiving the email you know - FYI the PHP on the site has a real email address, not AN_EMAIL#ADDRESS.COM. Once I hit the submit button I am taken to mailer.php but I notice the echo "blarg!" so my guess is the email is not being sent.
Thank you!
You should change
if(isset($_POST['submit'])) {
to
if(isset($_POST['saveForm'])) {
Try changing
if(isset($_POST['submit'])) {
to
if(isset($_POST['saveForm'])) {
This is because $_POST looks for the name of a form input, not the type.
If nothing above helps, try and see if you can debug the code.
Catching PHP mail() errors and showing reasonable user error message
In your PHP code you check if $_POST['submit'] is set, but in your HTML code you gave the submit button the name of saveForm so you should change the line
if(isset($_POST['submit'])) {
to
if(isset($_POST['saveForm'])) {
Hope this helped you :)
Some email servers won't accept emails without appropriate headers and you haven't provided any. This is what I use.
http://us2.php.net/manual/en/function.mail.php
$header = "From: ".$fromText."\r\n";
$header .= "Cc: ".$ccText."\n";
$header .= "Reply-To : ".$fromText."\r\n";
$header .= "Return-Path : ".$fromText."\r\n";
$header .= "X-Mailer: PHP\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
mail($toText, $subjectText, $msgText, $header, '-f'.$fromText);
you are using
if(isset($_POST['submit'])) { but, you have save the submit button name assaveForm So, use if(isset($_POST['saveForm'])) {
your problem is it's pour out blarg.
it's definitely the post does not reach your
code->
mail($to, $subject, $body);
and the name of the submit should be changed to
'saveForm'
by the way :)..
just tried
mail($to, $subject, $body);
in your x.php , upload it and chage to , subject and body to right things
and if it's sent then the mail function works okay.
if(#mail($emailRecipient, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
this is also a good code I have found in stackoverflow
to check if mail function works.
In your html your have
<input id="button" name="saveForm" class="btTxt submit" type="submit" value="Submit" />
but in the php file when you check for $_POST["submit"], which is not right.
You need to change if(isset($_POST['submit'])) to if(isset($_POST['saveForm']))
or if(isset($_POST['submit'])) to if(isset($_POST))
HTML form mail sending guide in PHP http://top-answers.net/webhost/web-hosting.html

Categories