Unable to send mails with php mail function [duplicate] - php

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

Related

How to send a confirmation email to sender and to me after form submission

I would like to send a confirmation email to the user who submitted the form and another email to me with the details which the user inputted, here is my form:
<form class="" action="." id="dateForm" method="POST">
<input type="text" class="form-control" id="dfName" placeholder="Name" required>
<input type="email" class="form-control" id="dfEmail" placeholder="Email" required>
<input type="tel" class="form-control" id="dfPhone" placeholder="Phone Number" required>
<input type="text" class="form-control" id="dfDate" placeholder="Schedule a call" required>
<textarea id="dfMessage" rows="5" class="form-control" placeholder="Your Message" required></textarea>
<button type="submit" class="">SUBMIT</button>
</form>
And here is my php which works perfect for sending the emails to me.
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$date = trim($_POST['date']);
$message = trim($_POST['message']);
function is_email_valid($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
if( isset($name) && isset($email) && isset($phone) && isset($date) && isset($message) && is_email_valid($email) ) {
$to = "myemail#gmail.com";
$subject = "New inquiry request from Olho";
$body = <<<EOD
<strong>Name:</strong> $name <br>
<strong>Email:</strong> $email <br> <br>
<strong>Phone:</strong> $phone <br>
<strong>Booking Date:</strong> $date <br>
<strong>Message:</strong> $message <br>
EOD;
$headers = "From: $name <$email>\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $body, $headers);
So what I would like is to receive another email after the one send to me which will contain a confirmation message like "Thank you for contacting us, we will get back to you as soon as possible."
I've tried to use this but I am not receiving the confirmation email:
$conf_subject = 'Your recent enquiry';
$conf_sender = 'Olho';
$msg = $name . ",\n\nThank you for your recent enquiry. A member of our
team will respond to your message as soon as possible.";
$headers2 = "From: $conf_sender <myemail#gmail.com>\r\n";
$headers2 .= 'MIME-Version: 1.0' . "\r\n";
$headers2 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail( $email, $conf_subject, $msg, $headers2 );
You should wait for first mail to be completed and then should send second email as below code:
if (mail($to, $subject, $body, $headers)) {
$conf_subject = 'Your recent enquiry';
$conf_sender = 'Olho';
$msg = $name . ",\n\nThank you for your recent enquiry. A member of our
team will respond to your message as soon as possible.";
$headers2 = "From: $conf_sender <myemail#gmail.com>\r\n";
$headers2 .= 'MIME-Version: 1.0' . "\r\n";
$headers2 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail( $email, $conf_subject, $msg, $headers2 );
}
Hope it helps you.

PHP contact form sends message through senders address

Recently I've been having problems with my PHP contact form. It's worked great for about two years, and I haven't changed anything, so I don't really understand what the problem is. Here's the code:
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match ( "/[\r\n]/", $str );
}
if(isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message'];
// check to see if name or email have header injections
if (has_header_injection($name) || has_header_injection($email)){
die();
}
if ( !$name || !$email || !$msg ) {
echo '<h4 class="error">All Fields Required</h4>Go back and try again';
exit;
}
// add the recipient email to a variable
$to = "example#example.net";
// Create a subject
$subject = "$name sent you an email";
// construct your message
$message .= "Name: $name sent you an email\r\n";
$message .= "Telephone: $tel\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
$message = wordwrap(message, 72);
// set the mail header
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n\r\n";
// Send the Email
mail( $to, $subject, $message, $headers );
?>
<!--- END PHP CONTACT FORM -->
<!-- Show Success message -->
<h2>Thanks for contacting Us!</h2>
<p align="center">Please allow 24 hours for a response</p>
<p>« Go to Home Page</p>
<?php } else { ?>
<form method="post" action="" id="contact-form">
<label for="name">Your Name</label>
<input type="text" id="name" name="name">
<label for="tel">Your Phone Number</label>
<input type="tel" id="tel" name="tel">
<label for="email">Your Email</label>
<input type="email" id="email" name="email">
<label for="message">the date/time you wish to sign up for</label>
<textarea id="message" name="message"></textarea>
<br>
<input type="submit" class="button next" name="contact_submit" value="Sign Up">
</form>
<?php } ?>
However, when the contact form is submitted, instead of sending the information to the body of the email, it sends it in the "From" section of the email. For example, the email might say:
To: Web Developer
From: Bob Smith 888-888-8888 mondays, wednesdays fridays
Subject: Bob Smith sent you an email!
Body:
X-Priority: 1X-MSMail-Priority: high
message
I don't really know what's going on, so any help would be appreciated!
You are adding all that info in the "from" header.
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
Change your headers to this:
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$email}>\r\n"; // Removed all extra variables
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n";
and it should work.
You are already sending the $message, containing all the above data in the body as well.
Why you haven't experienced this before is however a mystery.
NOTE: You only need to have one \r\n after each header.
You should also change this row:
$message = wordwrap(message, 72);
to
$message = wordwrap($message, 72); // Adding $ in front of the variable.

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>

PHP form not submitting entries to email. I'm missing something

Updated: I have a simple PHP form that should submit data to an email address but it isn't sending it. it's just sending the field names.
Here's the code:
<?php
$ToEmail = 'email#yahoo.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Surname: ".$_POST["surname"]."\r\n";
$MESSAGE_BODY .= "Designation: ".$_POST["designation"]."\r\n";
$MESSAGE_BODY .= "Phone: ".nl2br($_POST["phone"])."\r\n";
$MESSAGE_BODY .= "Email: ".nl2br($_POST["email"])."\r\n";
$MESSAGE_BODY .= "Opt in: ".nl2br($_POST["send"])."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<form class="form-signin" name="index" action="index.php" method="post">
<img src="img/coffee.png" width="235" height="227">
<h2 class="form-signin-heading">Enter your information</h2>
<input name="name" type="text" class="input-block-level" id="name" placeholder="Name">
<input type="text" class="input-block-level" name="surname" id="surname" placeholder="Surname">
<input type="text" class="input-block-level" name="designation" id="designation" placeholder="Designation">
<input type="text" class="input-block-level" name="phone" id="phone" placeholder="Cell Number">
<input type="text" class="input-block-level" name="email" id="email" placeholder="Email">
<label class="checkbox">
<input type="checkbox" value="Send-me-helpful-information" name="send" checked> <p>Send me helpful information</p>
<input name="submit" type="submit" value="submit" class="btn btn-large btn-primary" id="go" rel="leanModal" href="#thankyou">
<p>Terms & Conditions Apply.
Click HERE</p>
</form>
Please help
Comment: It's sending now but submits a blank email on page load. Does anyone know a fix for this?
Change your submit button to this:
<input type="submit" value="Submit" />
Your PHP should look more like
if(!empty($_POST)) { // This is to say if the form is submitted.
// All your PHP post stuff here. The email sending stuff.
$errors = array();
// Do this for all your required fields.
if(isset($_POST['email']) && $_POST['email'] != '') { // If there's a variable set and it isn't empty.
$mailheader = "From: " . $_POST['email'] . "\r\n";
} else {
$errors[] = "No email submitted";
}
print_r($_POST); // This is for debugging. Remove it when satisfied.
echo $MESSAGE_BODY; // Also for debugging. Ensure this is fine.
if(empty($errors)) {
if(mail()) { // Your mail function
// Successful mail send, either redirect or do whatever your do for successful send
} else {
// Mail failure, inform user the sending failed. Handle it as you wish. Errors will be sent out.
}
} else {
print_r($errors); // See what's going error wise.
}
}
<form name="form1" id="form1" action="" method="post"> // form like this
<input type="submit" name="submit" id="submit" value="submit"> // submit button like this
</form>
<?php if(isset($_REQUEST['submit']))
{
$ToEmail = 'email#yahoo.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Surname: ".$_POST["surname"]."\r\n";
$MESSAGE_BODY .= "Designation: ".$_POST["designation"]."\r\n";
$MESSAGE_BODY .= "Phone: ".$_POST["phone"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
} ?>
<?php
if(isset($_POST['submit'])){
//your sending email php code
}
?>
function mail()
uses SMTP Server so make sure it is perfectly configured and installed if you are using it on your local server such as WAMP or ZAMP.
rest if it still not working put your php code in between:
<?php
if(isset($_POST['submit']))
{
// ur php code here
}
?>
Since your form is posting some values, you need to have an <input type="submit"> to post those. name is just an indentifier, if you will call it "submit", it won't work.
Try adding <input type="submit" value="Whatever you want your text to be"> instead of <button name="submit" type="submit" class="btn btn-large btn-primary"><a id="go" rel="leanModal" href="#thankyou">Submit</a></button>.
If you have another problem, please explain it more. What errors do you get?
Try adding some echo to see where your program stops working.
Try this
<?php
if($_POST['submit']!="")
{
$ToEmail = 'email#yahoo.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Surname: ".$_POST["surname"]."\r\n";
$MESSAGE_BODY .= "Designation: ".$_POST["designation"]."\r\n";
$MESSAGE_BODY .= "Phone: ".$_POST["phone"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
}
?>

html showing in php mail

I'm trying to send a php mail but it seems that I have a error in my foreach because the mail shows html..
This is my code:
<form method="post">
<fieldset>
<legend>Contact Form</legend>
<label for="fullname">Votre Nom :
<input id="fullname" name="fullname" type="text" value="nelson" />
</label>
<label for="emailaddress" class="margin">Votre e-mail:
<input id="email" name="email" type="text" value="" />
</label>
<label for="message">Message:<br />
<textarea id="message" name="message" cols="40" rows="8"></textarea>
</label>
<p>
<input id="submit-button" class="button gray stripe" type="submit" name="submit" value="Envoyer le message" />
</p>
</fieldset>
</form>
<?php
foreach ($_POST as $value) {
$value = strip_tags($value);
$value = htmlspecialchars($value);
}
$name = $_POST["fullname"];
$email = "email:" .$_POST["email"];
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["message"];
$to="email#hotmail.com";
$suject="site internet";
if (isset($_POST['submit'])) {
mail($to, $suject, $message);
echo"mail had been sent";
}
?>
Can anyone help me please
You need to set the Content-type header in your email message:
$name = $_POST["fullname"];
$email = "email:" .$_POST["email"];
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["message"];
$to="email#hotmail.com";
$suject="site internet";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (isset($_POST['submit'])) {
mail($to, $suject, $message, $headers);
echo"mail had been sent";
Your message body also needs to be contained in <html> tags.
Your foreach is kind of useless, just do that. More fast than a loops
$name = strip_tags(htmlspecialchars($_POST["fullname"]));
$email = "email:" .strip_tags(htmlspecialchars($_POST["email"]));
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .strip_tags(htmlspecialchars($_POST["message";));
To send email containing HTML you must set the header so that the email client knows that the email contains HTML. You also have to make the body of your email an HTML document.
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";
mail($to, $suject, $message, $header);
And then in the message itself:
<html>
<head></head>
<body>
Content here
</body>
</html>

Categories