This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
Im trying to fix this issue for the past 3 days, but nothing is working..
I have a contact form and I wrote a php code to get the messages to my email.
Here is my contact form? [ I use bootstrap ]
<form class="col-lg-6" action="kontakt-confirmation.php" method="POST">
<div class="form-group">
<label>Namn <span id="mustdo">*</span></label>
<input id="namn" name="namn" class="form-control" type="text" aria-describedby="nameHint"
placeholder="Namn" required>
<small id="nameHint" class="form-text text-muted">Fyll i formuläret nedan så kontaktar vi
dig!!</small>
</div>
<div class="form-group">
<label>E-postadress <span id="mustdo">*</span></label>
<input id="email" name="email" class="form-control" type="email" placeholder="Din e-post" required>
</div>
<div class="form-group">
<label>Subject <span id="mustdo">*</span></label>
<input id="subject" class="form-control" type="text" placeholder="Your subject.." required>
</div>
<div class="form-group">
<label for="message">Meddelande <span id="mustdo">*</span></label>
<textarea id="message" name="message" class="form-control" rows="5" required></textarea>
</div>
<button type="submit" name="submit" class="btn btn-lg btn-outline-primary">Skicka</button>
</form>
Here is my PHP code:
<?php
if(isset($_POST)) {
$namn = $_POST['namn'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = '??????????';
$subject = 'Ticket!';
$message = "Namn: ".$namn."\n"."Email ".$email."\n"."Problem / Question: "."\n\n".$message;
$headers = "From: ".$email;
if(mail($to, $subject, $message, $headers)) {
echo "Thanks for contacting us!";
} else {
echo "Problem!";
}
}
?>
when I press on submit, i get the Problem message. What can i change to fix this?
thanks
Hope this resolves your answer to the question :)
if(isset($_POST)) {
$namn = $_POST['namn'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = '??????????';
$subject = 'Ticket!';
$message = "Namn: ".$namn."\n"."Email ".$email."\n"."Problem / Question: "."\n\n".$message;
$headers = "From: ".$email;
// 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";
if(mail($to, $subject, $message, $headers)) {
echo "Thanks for contacting us!";
} else {
echo "Problem!";
}
}
Related
I've been trying to send mail through the contact form on my website. I've used the POST method and that has seemed to work for another one of my projects, but now I'm puzzled as to what the problem is within the code. I've literally copy-pasted the code and still seem to come across the error. Also is there a way in which the name of the sender is displayed rather than the gibberish that actually ends up showing?
//PHP CODE
<?php
function has_header_injection($str){
return preg_match("/[\r\n]", $str);
}
if(isset ($_POST['submit'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$msg = $_POST['msg'];
if(has_header_injection($name) || has_header_injection($email)){
die();
}
if(!$fname || !$lname || !$email || $msg){
echo '<h4> All fields required!</h4>';
exit;
}
$to = 'mymail8#gmail.com'; //FOR ELUCIDATION
$subject = $fname . $lname ."sent you via your contact form";
$message = "Name: ". $fname . $lname;
$message .= "Email: $email\r\n";
$message .= "Message:\r\n$msg";
$message = wordwrap($message, 100);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: $lname <$email>\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
mail($to, $subject, $message, $headers);
}
else{
echo "failed to send mail!";
}
?>
//FORM (HTML)
<form action="mail-handler.php" class="msg" method="POST">
<div class="name">
<div class="fname">
<div>FIRST NAME</div>
<input type="text" placeholder="Enter your first name" id="fname" name="fname">
</div>
<div class="lname">
<div>LAST NAME</div>
<input type="text" placeholder="Enter your last name" id="lname" name="lname">
</div>
</div>
<div class="add-info">
<div class="mail">
<div>E-MAIL</div>
<input type="text" placeholder="Enter your email" id="mail" name="email">
</div>
<div class="phn">
<div>PHONE NUMBER</div>
<input type="text" placeholder="Enter your phone number" id="phn" name="phn">
</div>
</div>
<div class="msg-box">
<div>MESSAGE</div>
<input type="text" placeholder="Enter your Message" id="msg" name="msg" >
</div>
<button type="submit" id="sub" name="aubmit">Submit</button>
</form>
I tested to receive to my own email, for some reason I get everything but its not showing the content of what I type. It doesnt show the actual message when I receive it. Can anyone help? Thanks!
<div class="contact-heading">
</div>
<form role="form" class="contact-form" action="contact-us.php" method="post">
<div class="form-group">
<label class="" for="exampleInputPassword1">Name</label>
<input type="text" name="name" class="form-control" id="exampleInputPassword1" placeholder="Name:" required >
</div>
<div class="form-group">
<label class="" for="exampleInputEmail2">Email address</label>
<input type="email" name="email" class="form-control" id="exampleInputEmail2" placeholder="E-Mail:" required >
</div>
<div class="form-group">
<label class="" for="exampleInputEmail3">Company</label>
<input type="text" name="company" class="form-control" id="exampleInputEmail3" placeholder="Company:" required >
</div>
<div class="form-group">
<label class="" for="exampleInputEmail4">Message</label>
<textarea class="form-control textarea" name="message" rows="8" id="exampleInputEmail4" placeholder="Message:"></textarea>
</div>
<button type="submit" class="btn btn-submit">Submit</button>
</form>
</div>
<?php
$to = 'emailsample#yahoo.com';
$subject = "Contact us";
$name = stripslashes($_REQUEST['name']);
$email = stripslashes($_REQUEST['email']);
$company = stripslashes($_REQUEST['company']);
$message = stripslashes($_REQUEST['message']);
$msg .= "Name: ".$name."\r\n";
$msg .= "E-mail: ".$email."\r\n";
$msg .= "Company: ".$company."\r\n";
$message .= "Topicjk: ".$message."\r\n";
$msg .= "Subject: ".$subject."\r\n\n";
$msg .= "---Message--- \r\n";
$msg .= "\r\n\n";
$mail = #mail($to, $subject, $msg, "From:".$email);
if($mail) {
header("Location:contact-us.html");
} else {
echo 'Message could not be sent! Please try again.';
}
?>
Because you are not passing $message (which has content) to mail in this line.
Change from
$mail = #mail($to, $subject, $msg, "From:".$email);
To
$mail = #mail($to, $subject, $message, "From:".$email);
or
Change from
$message .= "Topicjk: ".$message."\r\n";
To
$msg .= "Topicjk: ".$message."\r\n";
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I created a form styled with bootstrap and followed a PHP tutorial on how to make the form functional (when info is filled an email to be send to a specified email) but it doesn't work.
I have read a lot of threads on Stackoverflow but couldn't find a solution.
I configured my php.ini file (I think correctly) and still the mail() function doesn't work.
Below is my code. I am an absolute beginner in PHP and I really appreciate your help!
<?php
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset($_POST['contact_submit'])){
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
if (has_header_injection($name)||($email)) {
die;
}
if ( !$name || !$email || !$message ) {
echo '<h2 class="success">All fields are required</h2> Try again';
}
$to = "example#email.com"; (this has my true email)
$subject = "$name sent you a message via example's contact form";
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message:\r\n$msg";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email>\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
mail($to, $subject, $message, $headers);
?>
<h2 class="success"> Thank you! Your project request is submitted. We will get back to you within 24 hours.</h2>
<p>« Go to home page</p>
<?php } else { ?>
<form action="" class="contact-form" id="contact-form" method="post" role="form">
<div class="form-group">
<label class="sr-only" for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" required="" placeholder="Name">
</div>
<div class="form-group">
<label class="sr-only" for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" required="" placeholder="Email">
</div>
<div class="form-group">
<textarea class="form-control" id="message" name="message" placeholder="Enter a brief description of your project" required=""></textarea>
</div>
<button type="submit" class="btn btn-custom" name="contact_submit">Send</button>
</form>
<?php } ?>
Are you working on localhost, if yes then email functionality is not going to work, put your code on server.
I have a problem with receiving mail. I have tried different mail providers (microsoft, yahoo, gmail) and still not receiving mail. I am working on web site for my friend and I bought a bootstrap template with php contact.
I am using xampp and I have tried sending the mail that way and I have upload it on some other friends web site in sub folder and still not receiving it.
Here is the code:
HTML index.php
<form class="js-contact-form" role="form" action="mail/contact.php" method="post" data-parsley-validate>
<div id="msgInfo"></div>
<input type="text" class="form-control wow fadeInLeft name" data-wow-delay="0.2s" placeholder="Name" required data-parsley-error-message="Enter name">
<input type="email" class="form-control wow fadeInLeft email" data-wow-delay="0.4s" placeholder="Email" required data-parsley-error-message="Enter email">
<textarea class="form-control wow fadeInLeft message" data-wow-delay="0.6s" rows="6" placeholder="Message" required data-parsley-error-message="Enter message"></textarea>
<button type="submit" class="btn btn-lg wow fadeInUp" data-wow-delay="1s">Send message</button>
</form>
Here is the contact.php code:
<?php
// PHP script for sending email
//
// Configuration
//
$toEmail = ""; // replace with your email where you would like to send email
$subject = 'Contact form from my website'; // replace with subject you want to receive
$body = 'You have received email from website:'; // replace with text that you want to receive in email
$from = ''; // replace with email that will look like sender
//
// ----- do not edit after this line if you don't understand what you are doing -----
//
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "Invalid input";
return false;
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body .= "\n";
$body .= "Name: $name\n";
$body .= "Email: $email\n";
$body .= "Message: $message\n";
$headers = "From: $email\n";
$headers .= "Reply-To: $toEmail";
$res = mail($toEmail, $subject, $body, $headers);
echo "OK";
return true;
?>
This isn't my first time that I am working with php, in the past I had a working php(2-3 years ago) and I have tested that also but it doesn't work -.-
Can someone help me please.
You have to add name attribute for all input/textarea tags in your form so you can find them in your $_POST array
<form class="js-contact-form" role="form" action="mail/contact.php" method="post" data-parsley-validate>
<div id="msgInfo"></div>
<input type="text" class="form-control wow fadeInLeft name" data-wow-delay="0.2s" placeholder="Name" required data-parsley-error-message="Enter name" name="name">
<input type="email" class="form-control wow fadeInLeft email" data-wow-delay="0.4s" placeholder="Email" required data-parsley-error-message="Enter email" name="email">
<textarea class="form-control wow fadeInLeft message" data-wow-delay="0.6s" rows="6" placeholder="Message" required data-parsley-error-message="Enter message" name="message"></textarea>
<button type="submit" class="btn btn-lg wow fadeInUp" data-wow-delay="1s">Send message</button>
</form>
and change this line in your php code ($ToEmail variable is always empty)
$res = mail($toEmail, $subject, $body, $headers);
with this
$res = mail($email, $subject, $body, $headers);
Try to replace with this
$body .= "\n";
$body .= "Name: ".$name."\n";
$body .= "Email: ".$email."\n";
$body .= "Messagee: ".$message."\n";
and change the headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = 'From: '.$name."\r\n".'Reply-To: '.$email."\r\n".'X-Mailer: PHP/' . phpversion();
Update
if($res){
echo "OK";
}
else{
echo "failed."
}
By this you can know whether your mail function in working or not.
Enable errors in php if not enabled using:
ini_set('display_errors',1);
I have a contact form in my website and it doesn't work properly. I get the email but the email it's empty, I don't get the information from the form.
the part of the html in my index.html
<form role="form" form name="contactform" method="post" action="send_form_email.php">
<div class="form-group">
<label>Nombre</label> <input type="text" name="nombre" class="form-control" placeholder="Introduce tu nombre" >
</div>
<div class="form-group">
<label>Apellidos</label> <input type="text" name="apellidos" class="form-control" placeholder="Introduce tus apellidos" >
</div>
<div class="form-group">
<label>Email</label> <input type="emal" name="email" class="form-control" placeholder="Introduce tu email" >
</div>
<div class="form-group">
<label>Telefono</label> <input type="text" name="telefono" class="form-control" placeholder="Introduce tu telefono" >
</div>
<div class="form-group">
<label>Mensaje</label> <textarea name="mensaje" class="form-control" rows="7"></textarea>
</div>
<button type="submit" class="btn btn-default" value="Submit"> Enviar
</form>
</div>
the part of the php file called "send_form_email.php"
<?php
$nombre = $_POST['nombre'];
$apellidos = $_POST['apellidos'];
$telefono = $_POST['telefono'];
$email = $_POST['email'];
$mensaje = $_POST['mensaje'];
$formcontent=" $nombre $apellidos $email $telefono $mensaje";
$recipient = "deniz946#gmail.com";
$subject = "Subject from $nombre";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! We will get back to you as soon as possible!" . " -" . "<a href='./index.php'> Back to site</a>";
?>
<input type="emal" name="email" class="form-control" placeholder="Introduce tu email" > change to
<input type="email" name="email" class="form-control" placeholder="Introduce tu email" >
you have typo in type attribute.
Try this
<?php
$nombre = $_POST['nombre'];
$apellidos = $_POST['apellidos'];
$telefono = $_POST['telefono'];
$email = $_POST['email'];
$mensaje = $_POST['mensaje'];
$formcontent = "{$nombre} {$apellidos} {$email} {$telefono} {$mensaje}";
$recipient = "deniz946#gmail.com";
$subject = "Subject from $nombre";
if(PHP_OS == "Linux" || PHP_OS == "Darwin") $new_line = "\n"; /* if Linux or Mac */
elseif(PHP_OS == "WINNT") $new_line = "\r\n"; /* if Windows */
$mailheader = "MIME-Version: 1.1" . $new_line;
$mailheader .= "Content-type: text/html; charset=utf-8" . $new_line;
$mailheader .= "From: ".$recipient.$new_line;
$mailheader .= "Return-Path: " . $recipient . $new_line;
$mailheader .= "Reply-To: " . $recipient . $new_line;
mail($recipient, $subject, $formcontent, $mailheader, "-r" . $recipient) or die("Error!");
echo "Thank You! We will get back to you as soon as possible!" . " -" . "<a href='./index.php'> Back to site</a>";
?>
Perhaps I can help you with an example I used myself:
The HTML contact Form:
<div class="row">
<!-- Content -->
<div id="content" class="8u skel-cell-mainContent">
<section class="12u">
<header>
<h2>Contact</h2>
</header>
<form method="post" action="mail.php">
<div class="row half">
<div class="6u">
<input name="subject" placeholder="Name" type="text" class="text" />
</div>
<div class="6u">
<input name='email' placeholder="Email" type="text" class="text" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row half">
<div class="12u">
<!--a href="mail.php" class="button">Submit</a-->
<input type="submit" class="button" value="Submit">
</div>
</div>
</form>
</section>
</div>
The PHP processing of the form:
<?php
//mail(to,subject,message,headers,parameters)
// Check if the "from" input field is filled out
//if (isset($_POST['from']))
//{}
$to = 'mail#xxxxxx.com'; // my email address
$subject = $_POST['subject']; //name client
$feedback = 'thank you, we will reply soon.';
$header = $_POST['email']; //Email client
$message = $_POST['message'];
$message = wordwrap($message, 70);
$message = <<<EMAIL
Dear (YOUR WEBSITE NAME HERE),
My name is: $subject
$message
Email Client: $header
EMAIL;
// send mail
if($_POST){
mail($to, $subject, $message, $header);
echo $feedback;
}
?>
Remember very carefully, the part from Dear... to EMAIL has to be against the left side of the screen, without any tabs or spaces. Else it won't work!
Hopefully you can use this to your success :)