Why is message not appearing when using mail() function in PHP? - php

This is probably a noob question, but hey I'm a noob so here it goes. When I am attempting to send an e-mail with the mail() function, the message does not appear.
Live example: enter link description here
Here's my html code:
<div id="column-wide">
<!-- DIV to push bottom of page down -->
<div style="height: 430px">
<div id="generic-container">
<!--*********DO NOT EDIT ABOVE THIS LINE**********-->
<h1>Contact us!</h1>
<p>You can contact us at admin#viddir.com OR you can use our special mail press below.</p>
<br />
<form method="post" action="contactus.php">
<textarea type="text" id="message" name="message"></textarea>
<br />
<input class="btn" type="submit" value="send" name="submit" />
</form>
<!--*********DO NOT EDIT BELOW THIS LINE**********-->
</div>
</div>
</div>
PHP code:
<?php
$to = "juggernaut1776#gmail.com";
$subject = "Message from Viddir";
$msg = $POST__['message'];
mail($to, $subject, $msg);
?>

Change
$msg = $POST__['message'];
To
$msg = $_POST['message'];

Related

Contact Form submission failing?

I'm currently working on my portfolio. As of right now I'm trying to get the contact form to work. So I don't really know php that well and I'm not that good at it, but I've been googling and trying different ideas for myself. As of right now none of them have worked. Below I will attach the contact form html portion as well as the php I have now. Any help would be appreciated greatly
<section id="contact-form">
<!--Contact form section-->
<form method="POST" name="emailForm" action="index.php">
<!--Beginning of form and mail to my email address-->
<div class="contact-left">
<!--Contact info on the left side-->
<h1 class="c-l-heading">Contact</h1>
<!--Major heading-->
<div class="f-name">
<!--Name portion-->
<font>Name</font>
<input type="text" name="name" placeholder="Full Name" required/>
<!--Input field with a placeholder-->
</div>
<div class="f-email">
<!--email portion-->
<font >Email</font>
<input type="email" name="name" placeholder="Example#gmail.com" required/>
<!--Input field with a placeholder-->
</div>
</div>
<div class="contact-right">
<!--Contact info on the right side-->
<div class="message">
<!--Message portion-->
<font >Message</font>
<textarea name="message" rows="5" cols="20" placeholder="Write Message..." required></textarea>
<!--Textbox created-->
</div>
<button>submit</button>
<!--Submit button-->
</div>
</form>
</section>
PHP section now:
<?php
if(!isset($_POST['submit']))
{
echo "Error! You need to submit the form!";
}
//Collection
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validation
if(empty($name) || empty($visitor_email))
{
echo "Name and email are required"
exit;
}
$email_from = 'desmondlambkin17#gmail.com';
$email_subject = "dnjls.ca Contact Submission";
$email_body = "Name: $name\n"
"Email address: $visitor_email\n"
"Message: $message";
$to = "desmondlambkin17#gmail.com";
$headers = "From: $email_from";
//Send Email
mail($to, $email_subject, $email_body, $headers);
?>
Currently when I try to submit an email it says This page isn't working, and is currently unable to handle the request

How to redirect after an email has been sent

I'm very new to PHP however, I have managed to get working an email system from a contact form on my front end of a webpage. The problem I have that once the email is sent, I wish to hide the form and display a message saying "thank you for the email."
I have tried to look for solutions, but the only answer I could find was to use this code: header("Location: sentmail.html"); From here then I redirect it to another HTML page, Identical to the contact page with the form removed. I feel this is bad practice as it will require many duping pages for how many contact forms are across the website.
Is there a way to display a "thank you for your message" Once the email has been sent rather then redirect to a sentmail.html page.
Thank you for taking the time to read.
`<?php
extract($_POST);
if(isset($_POST['submit']))
$name = $_POST['first_name'];
$email = $_POST['email_address'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mailTo = "jamie#sharpsdigital.co.uk";
$headers = "From: MLS systems Contact Form ";
$txt = "$name has sent you an email from the address. " .$email. "\n\n With the message of.\n\n" .$message. " \n\n This is an automated response from the contact form, Please click on the blue email address above to respond." ;
mail ($mailTo, $subject, $txt, $headers);
header("Location: sentmail.html");
?>`
This is the PHP Code, and below the form code
<section class="free-appoinment-area">
<div class="container">
<form action="sendmail.php" method="post" class="free-appoinment-form">
<div class="row">
<div class="col-md-12">
<div class="sec-title text-left">
<h1>Any queries</h1><span class="decor"></span>
</div>
<div class="row">
<div class="col-md-12">
<div class="input-field">
<input name="first_name" placeholder="Your Name*" type="text">
<div class="icon-holder">
<span class="flaticon-people"></span>
</div>
</div>
</div>
<div class="col-md-12">
<div class="input-field">
<input name="subject" placeholder="Your Subject" type="text">
<div class="icon-holder">
<span class="flaticon-people"></span>
</div>
</div>
</div>
<div class="col-md-12">
<div class="input-field">
<input name="email_address" placeholder="Email Address*" type="text">
<div class="icon-holder">
<span class="flaticon-note"></span>
</div>
</div>
</div>
<div class="col-md-12">
<div class="input-field">
<textarea name="message" placeholder="Your Message..."></textarea>
<div class="icon-holder comment">
<span class="flaticon-social-1"></span>
</div>
</div>
</div>
<div class="col-md-12">
<button type="submit" data-type="submit" name="submit" value="submit">Submit</button>
</div>
</div>
</div>
</div>
</form>
</div>
</section>
you can do it with php and css.
the php way is by using if else statement, like bellow
if(isset($_POST['submit'])){
$name = $_POST['first_name'];
$email = $_POST['email_address'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mailTo = "jamie#sharpsdigital.co.uk";
$headers = "From: MLS systems Contact Form ";
$txt = "$name has sent you an email from the address. " .$email. "\n\n With the message of.\n\n" .$message. " \n\n This is an automated response from the contact form, Please click on the blue email address above to respond." ;
if(mail ($mailTo, $subject, $txt, $headers)){
echo "Thanks for your contact.";
}
}else{ ?>
your form goes here
<?php }?>
There are hundreds of possibilities, but simply put, you can grab the return value of mail() and display contents according to this:
$success = mail('...');
form.php
if($success) {
echo 'Thank you!';
} else {
// output form here
}
Of course you can simply set a flag to true in your form handler too, but this is too broad to explain here.

Email not sending with PHPmail function [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
My page structure is the following
-index.php
-include send-form.php
-include contact-us-form.php
-include footer.php
my index.php file is the following (shortened)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
<?php include 'navigation-bar.php';?>
<?php include 'contact-us-form.php';?>
<?php include 'footer.php';?>
</body>
below is my contact-us-form
<div class="container">
<div class="row">
<div class="column">
<form method="post">
<label for="name">Name*</label>
<input type="text" id="name" name="name" placeholder="Your name.." required>
<label for="email">Email*</label>
<input type="text" id="email" name="email" placeholder="Your email.." required>
<label for="message">Message*</label>
<textarea id="message" name="message" placeholder="Your Message" style="height:170px" required></textarea>
<div style="text-align: center;">
<input type="submit" value="submit" >
</div> <!-- /submit button -->
</form> <!-- /form -->
<div id="error_message" style="width:100%; height:100%; display:none; ">
<h4>Error</h4>
Sorry there was an error sending your form.
</div>
<div id="success_message" style="width:100%; height:100%; display:none; ">
<h2>Success! Your Message was Sent Successfully.</h2>
</div>
</div><!-- /column -->
<div class="column2">
</div> <!-- /column2 -->
</div> <!-- /row-->
</div> <!-- /container -->
below is my send-form.php
if (isset($_POST["submit"])){
$to = '***************#gmail.com';
$subject = 'Message from website';
$message = 'MESSAGE: ' . $_POST [ "message" ] . ' </n> NAME: ' . $_POST['name'] ;
$from = $_POST[ "email" ];
if(mail($to, $subject, $message)){
echo 'message sent';
} else{
echo 'Unable to send email. Please try again.';
}
}
For some reason the email isn't sending. I can get the email to send if I put a action="/send-form.php" in the contact form but this bring me to another page.. What I am trying to do is for the user to send the form and stay on the same page where the message which is hidden will then be unhidden when clicked.. Any help on why my email isn't sending would be greatly appreciated!
I see you used $_POST["submit"] in your code. But you didn't define it on your form.
<input type="submit" value="submit">
Just put name="submit" on it and the problem must be solved.
<input type="submit" name="submit" value="submit">

Emailing a form with PHP and HTML error message "Cannot POST mail.php"

Second time I've posted about this website I'm coding but I'm making good progress! I'm trying to set up a form that when the user fills out will send to my email - not too difficult right? Well when I eventually hit the submit button, I get the error:
Cannot POST /mail.php
This is the HTML for the form that I've got:
<form method="post" action="mail.php">
<div class="field half first">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
</div>
<div class="field half">
<label for="email">Email</label>
<input type="text" name="email" id="email" />
</div>
<div class="field">
<label for="message">Message</label>
<textarea name="message" id="message" rows="5"></textarea>
</div>
<ul class="actions">
<li>Send Message</li>
</ul>
</form>
And this is my mail.php file
<?php
$myemail = "MYEMAIL";
$name = check_input($_POST['name'], "Enter your name");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your message");
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
$message = "
Name: $name
E-mail: $email
Subject: $subject
Message:
$message
";
mail($myemail, $subject, $message);
header('Location: thankyou.html');
exit();
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
Both files are in the same directory so I have no idea why this isn't working :( Thanks guys <3
Edit: This is what the form looks like
Edit 2: Changed button to:
<li><input type="submit" value="Send Message" class="button submit" /></li>
but still getting same problem :/
Edit 3: Can also confirm that
<li><button type="submit" value="Send Message" class="button submit" /></li>
doesn't make a difference :((
You'll need to use a button for that
<button class="yourclasshere" type="submit" name="sendMail">Send Mail</button>
In your PHP you'll need to add a line of code that checks to see if the form is being submitted. This can be accomplished with the following:
if(isset($_POST['sendMail'])) {
//validate your inputs and submit the form here
}
What this does is checks to see if you've clicked the submit button (in this example you're going by the name of the button which I've named 'sendMail') and if you have, you can set the values of your other variables (name, email, message) using your existing function.
From there if everything is good, you should be able to send your mail.

how to configure email address on submit button in html contact form

I am unable to configure my email address on the submit button in this piece of code:
<div id="contact" class="spacer">
<div class="container contactform center">
<h2 class="text-center wowload fadeInUp">Get in touch with us</h2>
<div class="row wowload fadeInLeftBig">
<div class="col-sm-6 col-sm-offset-3 col-xs-12">
<form class="cmxform" id="commentForm" method="post" action="email.php">
<fieldset>
<input type="text" placeholder="Subject" id="csubject" name="subject" minlength="2" type="text" required>
<input type="text" placeholder="Email" id="cemail" type="email" name="email" required>
<textarea rows="5" placeholder="Message" id="ccomment" name="comment" required></textarea>
<input class="submit btn btn-primary" type="submit" value="Submit">
</fieldset>
</form>
</div>
</div>
I tried linking it to a php page (email.php), but it says server error. I don't know what to do. Can someone please help me?
To send an email you need the mail() function http://php.net/manual/en/function.mail.php
With your code you need to name the submit like name="submit" then inside action.php file you have to write something like this (or inside the same php where you have this code):
if(isset($_POST["submit"]))
{
//Remember to do input validations
$subject = $_POST["subject"];
$from = $_POST["email"];
$comment = $_POST["comment"];
$body = "User with email $from send the next comment: $comment";
//then here you set your email
$to = "myemail#email.com"; //change this
mail($to,$subject,$body);
}
This is only to explain some basic usage and set the variables you need, I advice you read also PHPmailer class

Categories