PHP sendmail, values not passing through to email - php

I have a contact form on an html page that is using a php script on seperate php page to send text values entered into input boxes in an email
Everything is working except the text entered in the forms input boxes are not passing through, they are blank
Below is what I am doing:
HTML
<form class="form-inline" action="mail_handler.php" method="post"
enctype="multipart/form-data">
<div class="fullwidth">
<div class="left-feild col-md-6">
<div class="form-group">
<input type="text" class="form-control color01 background13 border-color08" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<input type="email" class="form-control color01 background13 border-color08" name="email" placeholder="Email" required>
</div>
<div class="form-group">
<input type="text" class="form-control color01 background13 border-color08" name="number" placeholder="Phone/Cellphone Number">
</div>
<div class="form-group">
<input type="text" class="form-control color01 background13 border-color08" name="company" placeholder="Company name">
</div>
</div>
<div class="right-feild col-md-6">
<div class="form-group fullwidth">
<textarea class="form-control color01 background13 border-color08" rows="3" placeholder="type your message here" name="message" required></textarea>
</div>
</div>
</div>
<div class="submitbutton fullwidth">
<button type="submit" value="submit" name="submit" class="btn more background07 color01 color01-hover01" >book</button>
</div>
</form>
PHP
<?php
$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$company = $_POST['company'];
$message = $_POST['message'];
$from = 'From: info#recipient.com';
$to = 'myemail#gmail.com'; //email
$subject = 'website booking enquiry';
$message = "From: $name\n Number: $number\n E-Mail: $email\n Company:
$company\n Message: $message\n ";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers = 'From: info#recipient.com' . "\r\n" .
'Reply-To: $email' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
header( "Location: index.html" );//if u wish to get redirected
?>

This is getting too long for comments, therefore I have submitted the following.
You have quite a few errors in your code.
You've missed a concatenate/dot for your last header and is "breaking the chain" as it were, and variables don't get parsed in single quotes
Also the first header should not have a leading dot.
$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$company = $_POST['company'];
$message = $_POST['message'];
$from = 'From: info#recipient.com';
$to = 'myemail#gmail.com'; //email
$subject = 'website booking enquiry';
$message = "From: $name\n Number: $number\n E-Mail: $email\n Company:
$company\n Message: $message\n ";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= 'From: info#recipient.com' . "\r\n" .
"Reply-To: $email" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
header( "Location: index.html" );//if u wish to get redirected
exit;
You should also check for empty fields, because anyone can submit with nothing filled and you will get empty results.
I.e.:
if(!empty($_POST['name']) || !empty($_POST['email'])) {
// Assign variables to the POST arrays and process mail
$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$company = $_POST['company'];
$message = $_POST['message'];
// ... rest of your code
}
Note: || means "OR". You can use that, or && which means "AND", or a mix of both.
Footnotes:
\n will not show the data on separate lines, should that be the goal, since you are using HTML headers.
You will need to use <br> if that is what you want to do here.
Consult the manual on mail:
http://php.net/manual/en/function.mail.php
A few other alternatives to PHP's mail(), are PHPMailer and Swiftmailer:
https://github.com/PHPMailer/PHPMailer
http://swiftmailer.org/
As stated in comments:
enctype="multipart/form-data" - remove that from your form element. – CBroe"
That can be safely removed since you're not dealing with files.
If this is a server issue, then you will need to find out what the problem is and is beyond the scope of the question.

Related

Contact Form Has No Content

Form submission is received, but the content fields are empty.
Changed "$_POST['Email']" to "$_GET['Email']"
Reviewed 10 other posts on the forum and troubleshooted for 3 hours.
HTML CODE
<form action="callform.php" method="post" class="request-form ftco-
animate">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Full
Name">
</div>
<div class="form-group">
<input type="text" name="email" class="form-control" placeholder="Email
Address">
</div>
<div class="form-group">
<div class="form-field">
<div class="select-wrap">
<div class="icon"><span class="ion-ios-arrow-down"></span></div>
<select name="dropdown" id="Selection" class="form-control">
<option value="">Select Your Services</option>
<option value="">White Paper</option>
<option value="">Brown Paper</option>
<option value="">Black Paper</option>
<option value="">Red Paper</option>
<option value="">Yellow Paper</option>
<option value="">Orange Paper</option>
<option value="">Blue Paper</option>
<option value="">Green Paper</option>
<option value="">Purple Paper</option>
<option value="">Clear Plastic</option>
<option value=""></option>
</select>
</div>
</div>
</div>
<div class="form-group">
<input type="text" name="phone" class="form-control" placeholder="Phone">
</div>
<div class="form-group">
<textarea type="text" name="message" id="message" cols="30" rows="2"
class="form-control" placeholder="Message"></textarea>
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-primary py-3 px-4"
name="submit">
</div>
</form>
PHP
<?php
$EmailFrom = ".$email";
$mailTo = "my_email";
$Subject = "New Request";
$name = $_POST['name'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$txt = "You have received a Request from ".$name.".\n\n".$message;
mail($mailTo, $Subject, $dropdown, $message, $headers);
/* Redirect visitor to the thank you page */
header('Location: ""?msg=success');
?>
This is what I receive:
From: CGI-Mailer
To: mailpost#mymail.com
Subject: New Call Request
You have received a Request from .
Please check this
You have used wrong mail format : This is right one
mail(to,subject,message,headers,parameters);
$mailTo = "my_email";
$subject = "New Request";
$name = $_POST['name'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$txt = "You have received a Request from ".$name." ".$message;
mail($mailTo, $subject, $txt, $headers);
/* Redirect visitor to the thank you page */
header('Location: ""?msg=success');
?>
You have an extra parameter in your mailTo function.
This:
mail($mailTo, $Subject, $dropdown, $message, $headers);
should become
mail($mailTo, $Subject, $message, $headers);
You were sendind the value of $dropdown as the message and $message as headers. The function was not throwing errors since it accepts an optional parameter value
The rest of your code looks fine if you keep using $_POST since you are using that method in the form definition
Please use this code for send your form email it's working
$body = 'Name:-'.$_POST['name'];
$body .= 'Email:-'.$_POST['email'];
$body .= 'Services:-'.$_POST['dropdown'];
$body .= 'Phone:-'.$_POST['phone'];
$body .= 'Message:-'.$_POST['message'];
$subject = "Your email subject here";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .="From: noreply#yourdomainname.com \r\n";
$to_email = "Your email address here";
if(#mail($to_email,$subject,$body,$headers))
{
echo 'email send sucessfully';
}
else
{
echo "Message could not be sent.";
exit();
}
As I see, after submission, form fields values are sent correctly. I dont find any major issues. But you should do some basic validation before catch fields data in this way -
<?php
if (isset($_POST['submit'])){
$EmailFrom = "your from email goes here";
$Subject = "New Request";
$name = $_POST['name'];
$mailTo = 'mail to email goes here';
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$headers = 'MIME-Version: 1.0' . "\r\n";
// Additional headers
$headers = 'From: ' .$EmailFrom. "\r\n";
$headers .= 'Reply-To: '.$email. "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$txt = "You have received a Request from ".$name.".\n\n".$message;
mail($mailTo, $Subject, $txt, $headers);
/* Redirect visitor to the thank you page */
header('Location: ""?msg=success');
}
?>
However, your mail function contains wrong parameter as I made the correction above. also you can concatenate other fields to $txt variable if you need and add the html markup.

How to fix PHP contact-form errors: HTML_MIME_NO_HTML_TAG, MIME_HTML_ONLY, MISSING_DATE

I'm not good at PHP so please be patient and understandable. The e-mails from my contact-form always go into the spam folder. I ran a test on this site and it tells me that
there is no HTML tag in my HTML-only message (HTML_MIME_NO_HTML_TAG)
my message only has text/html MIME parts and I should add text/plain (MIME_HTML_ONLY)
my message has a missing header: date (MISSING_DATE)
they don't have a mail server (MX Record) behind my domain name and I should post a DNS record (MX type) for my domain name
I know that some of these questions have been asked before, but I don't know what to do and I think I need personal help. Thanks!
EDIT: I have two more problems:
there is no SPF record and I should add my domain name to my DNS zone file
the message is not signed by DKIM
My HTML:
<div class="contact-form">
<form id="contact-form" method="post" action="contact-form-handler.php">
<input name="name" type="text" class="form-control" placeholder="Your Name" required>
<br>
<input name="email" type="email" class="form-control" placeholder="Your Email">
<br>
<textarea name="message" class="form-control" placeholder="Message" rows="40" required></textarea><br>
<input type="submit" class="form-control" value="SEND MESSAGE">
</form>
</div>
My PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: mywebsite.com';
$to = 'someone#something.net';
$subject = 'Subject-line';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= 'From: '. $email. "\r\n" .
$headers .= "Reply-To: ". $email. "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$message = nl2br($message);
$status = mail($to, $subject, $message, $headers);
if($status)
{
echo '<p>Your Message has been send!</p>';
} else {
echo '<p>Something went wrong. Please try again.</p>';
}
?>

Processing a form on the same page

So, I've been messing with this for a day now and I've read a lot tutorials and even posts on stackoverflow. I know it's possible to have all your php form processing steps on the same page as your form, but for some reason, my form won't send an email. Any help?
<div id="mc_embed_signup">
<form method="post" action="index.php" enctype="text/plain" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#address.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if($_POST['submit']) {
mail ($to, $subject, $body, $headers); //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>
Remove the enctype="text/plain" that's a main factor.
Plus, you also need to check if fields were left empty using (if)empty().
Not doing so, you may receive emails that either do not contain the user's email, name, message or a combination of all.
Assuming this file is called index.php if not, use action=""
<div id="mc_embed_signup">
<form method="post" action="index.php" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#example.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if(isset($_POST['submit']) && !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']) ){
// check if mail was sent
if(mail ($to, $subject, $body, $headers)){ //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Mail was not sent. Check your logs.</p>";
}
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>
This might help,
if(isset($_POST['submit'])) {
if(empty($_POST[name]) && empty($_POST[email]) && empty($_POST[message])){
echo 'Fields cannot be empty';
} else{
$name = $_POST['name'];
$to = $_POST['email'];
$subject = "Hello";
$message = $_POST['message'];
$from = $_POST['email'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail ($to, $subject, $body);
}
}else{
echo "<p>Something went wrong, go back and try again!</p>";
}

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 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>

Categories