PHP - Form Submit Doesn't Work [duplicate] - php

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.

Related

Unable to send mail via PHP mail function

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>

PHP code is sending emails to spam, how to prevent it? [duplicate]

This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 1 year ago.
I'm new to PHP and HTML working on a website both codes seem to be working.
I'm trying to edit an existing contact us form and send the form to a default email but it is getting to the spam folder.
Here is my HTML code part:
<div class="row-fluid">
<div class="span8" id="divMain">
<h1>Contact Us</h1>
<h3 style="color:#;"></h3>
<hr>
<!--Start Contact form -->
<form name="enq" action="contact-form-handler.php" method="POST" onsubmit="return validation();">
<fieldset>
<input type="text" name="name" id="name" value="" class="input-block-level" placeholder="Name" maxlength="80" />
<input type="text" name="email" id="email" value="" class="input-block-level" placeholder="Email" maxlength="80" />
<textarea rows="11" name="message" id="message" class="input-block-level" placeholder="Message" maxlength="1024"></textarea>
<div class="actions">
<input type="submit" value=" Send " name="submit" id="submitButton" class="btn btn-inverse pull-left" title="Click here to submit your message!" />
</div>
</fieldset>
</form>
<!--End Contact form -->
</div>
and here is my PHP code:
<?php
$name=$_POSt['name'];
$vistor_email=$_POST['email'];
$message=$_POST['message'];
$email_from='ex2#gmail.com';
$email_subject="New request to A-Akawi";
//$email_body="User Name:$name.\n"."User Email:$vistor_email.\n"."User message:$message.\n";
$body = <<<EMAIL
You have received a new request from $name with the following email address $vistor_email.
The following request is:
$message.
EMAIL;
$to ='ex1#gmail.com';
$headers .= "From: Johnson Smith <noreply#ksar.com> . \r\n" ;
$headers .='Reply-To: '. $to . "\r\n" ;
$headers .= "Organization: Sender Organization\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
mail($to,$email_subject,$body,$headers);
header("location: contact.html");
?>
I'm getting it to the spam even after adding the headers how to fix it?
As far as I know you also need to add the Return-Path: to the header of your e-mail.
But there could be many more reasons why your email is going to the spam.
The spf records in your DNS could be configurate wrong or the IP of your server is listed in spam filters

How can i solve the issue with mail [duplicate]

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

I am not receiving mail php

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

PHP Undefined index error with form

I keep getting an error as follows when i submit my form. I'm not sure what it is. I'm running the site on WAMP (windows) and im writing it in dreamweaver CS6. Please note I'm fairly new to PHP. Thanks!
Here is the code:
Notice: Undefined index: message in S:\wamp\www\latech\contact.php on line 29
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
<?php
// Check for Header Injections
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset($_POST['contact_submit'])) {
// Assign trimmed form data to variables
// Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message']; // no need to trim message
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die(); // If true, kill the script
}
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 = "brad#brightsidestudios.ca";
// Create a subject
$subject = "$name sent a message via your contact form";
// Construct the message
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
// If the subscribe checkbox was checked
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
// Add a new line to the $message
$message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
}
$message = wordwrap($message, 72); // Keep the message neat n' tidy
// Set the mail headers into a variable
$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";
// Send the email!
mail($to, $subject, $message, $headers);
?>
<!-- Show success message after email has sent -->
<h5>Thanks for contacting Anise Technologys!</h5>
<p>Please allow 24 hours for a response.</p>
<p>« Go to Home Page</p>
<?php
} else {
?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you#website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
<?php
}
?>
</div>
</div>
</body>
</html><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
<?php
// Check for Header Injections
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset($_POST['contact_submit'])) {
// Assign trimmed form data to variables
// Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message']; // no need to trim message
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die(); // If true, kill the script
}
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 = "brad#brightsidestudios.ca";
// Create a subject
$subject = "$name sent a message via your contact form";
// Construct the message
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
// If the subscribe checkbox was checked
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
// Add a new line to the $message
$message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
}
$message = wordwrap($message, 72); // Keep the message neat n' tidy
// Set the mail headers into a variable
$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";
// Send the email!
mail($to, $subject, $message, $headers);
?>
<!-- Show success message after email has sent -->
<h5>Thanks for contacting Anise Technologys!</h5>
<p>Please allow 24 hours for a response.</p>
<p>« Go to Home Page</p>
<?php
} else {
?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you#website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
<?php
}
?>
</div>
</div>
</body>
</html>
Your name attribute is msg your calling it message in PHP.
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
$msg = $_POST['message']; // no need to trim message
Also here...
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
The first occurrence should be $message = because there is nothing to concatenate when it hasn't been initialized.
You are using
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
Rather use
<textarea id="msg" name="message" spellcheck="true" ></textarea>

Categories