PHP mail() is not accepting variables - php

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

Related

Contact form doesnt send the message [duplicate]

This question already has answers here:
Php form failing
(1 answer)
Simple form not sending data via _POST [duplicate]
(4 answers)
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
My contact form in php doesnt send the message to the mail so i need to know whats the problem here
You will find the html form here with file name : index.php
and php form with name : mail.php
<form class="form" action="mail.php" method="post" name="contactform">
<input class="name" type="text" placeholder="Name" name="name">
<input class="email" type="email" placeholder="Email" name="email" >
<input class="phone" type="text" placeholder="Phone No:" name="phone">
<textarea class="message" id="message" cols="30" rows="10" placeholder="Message"name="message" ></textarea>
<input class="submit-btn" type="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit']) ) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = 'From: phone';
$to = 'modysaid26#gmail.com';
$subject = 'message';
$body = "From: $name\n E-Mail: $email\n Phone Number: $phone\n Message:\n $message";
if (isset($_POST['submit'])) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been submitted</p>';
} else {
echo '<p>Something went wrong, please try again!</p>';
}
}
}
?>
<input class="submit-btn" name='submit' type="submit" value="Submit">
You are missing to add the name to submit button so your case if (isset($_POST['submit']) ) { fails
you dont need to put name on the form tag remove the class either:
<form action="mail.php" method="post">
First yor submit button name is missing, please use a
<input class="submit-btn" type="submit" value="Submit" name="submit">
The second you email command ( mail ($to, $subject, $body, $from) ) has no right email header. Insead your $from
please define header with following parameters
$email_headers = "From: ".$from_name." <".$from_email.">\r\n".
"Reply-To: ".$reply_to."\r\n" ;
if ($cc) $email_headers.="Cc: ".$cc."\r\n";
if ($bcc) $email_headers.="Bcc: ".$bcc."\r\n";
$email_headers.="MIME-Version: 1.0" . "\r\n" .
"Content-type: text/html; charset=UTF-8" . "\r\n";
$email_body=$_POST['message'];
and then send it using
mail($to, $subject, $email_body, $email_headers);
And then your email shouldbe send properly.

PHP Success message after submit

I'm using a simple php code to send mail from my website
<?php
$headers ="From:<$from>\n";
$headers.="MIME-Version: 1.0\n";
$headers.="Content-type: text/html; charset=iso 8859-1";
mail($to,$subject,$body,$headers,"-f$from");
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "contato#pedrogps.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thanks for your message";
?>
when i submit the email, it brings to another page (mail.php) with the "thanks for your message" message.
What can i do to display this message inside my html page without switching pages.
There are two ways one is use AJAX and another one:
make form action empty like
<form name="form1" action="" method="post">
before form start or top of the page write this php code:
if(isset($_POST)){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "contato#pedrogps.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thanks for your message"; // store this msg in a variable and display wherever you required to echo
}
If you do not want the webpage to load, when submitting, you'll need some JavaScript.
<form id="mailForm">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="message">
<input type="submit" value="Send">
</form>
<div id="result"></div>
<script>
$('#mailForm').submit(function(e) {
e.preventDefault();
var formData = $('#mailForm').serialize();
var url = "mail.php";
var posting = $.post(url, formData);
posting.done(function(data) {
$("#result").append(data);
});
});
</script>
Remember to load the jQuery library, read more about jQuery here: https://learn.jquery.com/about-jquery/how-jquery-works/. Read more about the jQuery.post() here: https://api.jquery.com/jquery.post/
If you do not care if the webpage loads, when submitting, then you can easily use the include function in PHP.
<form id="mailForm" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="message">
<input type="submit" name="mail" value="Send">
</form>
<div id="result">
<?php
if(isset($_POST['mail']))
{
include("mail.php");
}
?>
</div>
Maybe you should check some more things than only the $_POST['mail'] is set, but you'll get the idea.

Make PHP mail form

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.

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'

Categories