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>
Related
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.
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";
This php contact form I'm using returns the message that my message has be sent but no email is received by the specified email address.
Here's the php:
<?php
$to = 'blahbahblah#gmail.com';
if($to) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$html = "";
$html .= "Name: " . htmlentities($name, ENT_QUOTES, "UTF-8") . "<br>\n";
$html .= "Email: " . htmlentities($email, ENT_QUOTES, "UTF-8") . "<br>\n";
$html .= "Message: " . htmlentities($message, ENT_QUOTES, "UTF-8") . "<br>\n";
$headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=utf-8\r\n";
$headers .= "From: " . $name . "<". $email .">\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$html = utf8_decode($html);
mail($to, $subject, $html, $headers);
if ($html)
echo 'ok';
else
echo 'error';
} else {
echo "error";
}
?>
And here's the html associated with it:
<form method="post" action="contact.php">
<p>
<input type="text" name="name" id="name" placeholder="Name" />
</p>
<p>
<input type="text" name="email" id="email" placeholder="Email" />
</p>
<p>
<input type="text" name="subject" id="subject" placeholder="Subject" />
</p>
<div class="textarea-wrapper">
<textarea name="message" id="message" cols="45" rows="10" placeholder="Message"></textarea>
</div>
<button id="submit">Send</button>
</form>
I realize issues like these are frequent, but I've unable to figure it out. Any insight is greatly appreciated.
"<br>\n"
use "\r\n" instead and try again
You need to change your SMTP settings per your conversation with the support rep. These are set in your PHP.INI
The From address should belong to the domain from where you are running the script. If your script is running on your-website.com then the From address should be like xyz#website-name.com
$headers = "From: xyz#website-name.com";
mail($to,$subj,$body,$headers);
To check the contact form that I have used, visit: http://manageproac.com/support/
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>
I wrote this code but it isn't sending it to my email. What can be wrong?
This is the code from the contact form:
<!-- send mail configuration -->
<input type="hidden" value="send-mail.php" name="to" id="to" />
<input type="hidden" value="Enter the subject here" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
This is the code for the send-mail.php
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['kurtfarrugia92#gmail.com'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send for each mail
foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}
?>
Basic PHP mail script.
file mail.php
<html>
<body>
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("someone#example.com", $subject,
$message, "From:" . $email);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mail.php'>
Email: <input name='email' type='text'><br>
Subject: <input name='subject' type='text'><br>
Message:<br>
<textarea name='message' rows='15' cols='40'>
</textarea><br>
<input type='submit'>
</form>";
}
?>
</body>
</html>
Do you have error reporting on? It may be handy to get more info about what is going on/wrong.
error_reporting(E_ALL);
Try:
echo "mail($mail, $subject, $msg, $headers)";
to see if what you are sending to mail() is exactly what you think it is.