Email not sending with PHPmail function [duplicate] - php

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

Related

Contact Form is not displaying in php script

I have created a code snippet (given below) in php script for CONTACT Form. The Form is not displaying itself when there is no error shown in the Console. One issue is raised saying that "Audit usage of navigator.userAgent,navigator.appVersion, and navigator.platform".
I am using Windows 10 and sublime text editor. React JS is also installed in my computer.
Please help me to resolve the issue.
<?php
if(isset($_POST['send'])) {
$to_mail = $_POST['tomail'] ;
$subject = $_POST['subject'] ;
$body = $_POST['message'] ;
$from_mail = $_POST['frommail'];
$header = "From: $from_mail" ;
mail($to_mail, $subject, $body, $header) ;
if(mail($to_mail, $subject, $body, $header)){
echo "We have sent a mail to your mail id. Kindly check and confirm." ;
}else{
echo "No mail could be sent to you." ;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php include 'header.php' ; ?>
</head>
<body>
<div class="container-fluid" id="container-contact">
<div class="row">
<div class="col col-12">
<form class="cform" action="<?php echo htmlentities($_SERVER('PHP_SELF')) ;?>" method = "POST">
<div class="form-group">
<label>To</label>
<input type="email" class="form-control" name="tomail"> <br><br>
<label>Subject</label>
<input type="text" class="form-control" name="subject"> <br><br>
<label>Message</label>
<textarea type="text" class="form-control" name="message" `enter code here`cols="40" rows="15"></textarea> <br><br>
<label>From</label>
<input type="email" class="form-control" name="frommail"> <br><br>
<button><input type="submit" name="send" value="Send"></button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
#Subhajit
line number 37 : you have use the small bracket , but $_SERVER is the array and you have to use the big bracket [ and ]
<?php echo htmlentities($_SERVER('PHP_SELF')) ;?>
solution e.g echo htmlentities($_SERVER['PHP_SELF']) ;
there was the error "fatal error Function name must be a string" that due to you have used the server variable properties with small brackets.

PHP - html form sent to gmail account

I never used PHP before, but I need to set up an HTML form from to be sent from my website to my gmail account. Checked some tutorials and came up with this code. But for some reason it is not working. When I hit submit it goes to a "Page Not Found
Looks like you've followed a broken link or entered a URL that doesn't exist on this site." Not sure what am I doing wrong.
I have my HTML document, and I have my PHP document. Saved together.
<form action="contact.php" method="POST" name="form">
<div class="row">
<div class="col-12 col-md-6">
<div class="contact-form">
<label for="name">Please enter your name:</label>
<input name="name" type="text" class="form-control" placeholder="Harry Potter">
</div>
</div>
<div class="col-12 col-md-6">
<div class="contact-form">
<label for="mail">Your email address:</label>
<input name="mail" type="email" class="form-control" placeholder="harry#potter.com">
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="contact-form">
<label for="message">How can I help you?</label>
<textarea name="message" class="form-control" id="form-text" cols="30" rows="10" placeholder="Let's do some magic"></textarea>
</div>
</div>
</div>
<div id="submit" class="text-center">
<button name="submit" type="submit" class="btn mt-5 mx-auto">Submit</button>
</div>
</form>
<?php
if(isset($_POST["submit"])) {
$name=$_POST["name"];
$mailFrom=$_POST["mail"];
$message=$_POST["message"];
$mailTo = "myaddress#gmail.com";
$subject = "Form from my website";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from ".$name."\n\n".$message;
if(mail($mailTo, $subject, $txt, $headers)) {
echo "<h1>I recieved your email! Will be in touch with you soon.</h1>";
}
else{
echo "<h1>Something went wrong! Try again.</h1>";
}
header("Location: https://www.whatever.com");
}
?>

HTML Form Sent to Email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I'm having an issue with my html form sending to my email.
I have it set up so that it goes to a php file and it works fine, but when I change the filename from index.php to contact.php it doesn't send anymore.
contact.php:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style/stylesheet.css">
<script src="script/scripts.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>
<div class="banner">
<img src="images/banner.jpg">
</div>
<div class="navigation">
<div class="navbar">
Home
News
<div class="dropdown">
<button class="dropbtn">Parts
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Cases
Motherboards
Processors
Graphics Cards
Storage
Power Supplies
RAM
Other
</div>
</div>
<div class="dropdown">
<button class="dropbtn">Builds
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Placeholder
Placeholder
Placeholder
Placeholder
Placeholder
Placeholder
Placeholder
Placeholder
</div>
</div>
<div class="contact" id="navright">
Contact
</div>
</div>
</div>
<div class="contact_page">
<form class="contact_form" action="contactform.php" method="post">
<label>Full Name:</label><br/>
<input type="text" name="name" placeholder="Full Name"><br/>
<label>Your Email:</label><br/>
<input type="text" name="mail" placeholder="Email"><br/>
<label>Subject:</label><br/>
<input type="text" name="subject" placeholder="Subject"><br/>
<label>Message:</label><br/>
<textarea name="message" class="contacttext" placeholder="Message..."></textarea><br/>
<button class="submit" type="submit">Submit</button>
</form>
</div>
<div class="footer">
<div class="footertext">
<p>Here at Terry's Computers we do not claim to own any of
the products showed on this website.</p>
<p>Contact Us</p>
</div>
</div>
</body>
</html>
contactform.php:
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "terryjtowell#terrytowell.com";
$headers = "From: ".$mailFrom;
$txt = "You have received an Email from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: contact.php?mailsend");
}
?>
It's not my web hoster because it worked with this file.
index.php:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<meta charset="UTF-8">
<title>Email Form</title>
</head>
<body>
<main>
<p class="header">E-MAIL FORM</p>
<form class="contact-form" action="contactform.php" method="post">
<p class="title">Your Name</p>
<input type="text" name="name" placeholer="Full Name"><br/>
<p class="title">Your E-Mail</p>
<input type="text" name="mail" placeholer="Your E-mail"><br/>
<p class="title">Subject</p>
<input type="text" name="subject" placeholer="Subject"><br/>
<p class="title">Message</p>
<textarea name="message" maxrows="10" placeholder="Message"></textarea><br/>
<button type="submit" name="submit"><h2>SUBMIT</h2></button><br/>
</form>
</main>
</body>
</html>
Your PHP line:
if (isset($_POST['submit'])) {
Will never execute because you need an element with the name of submit, but you have:
<button class="submit" type="submit">Submit</button>
Which you have in the example you say works. The solution should be as simple as adding a name attribute like this:
<button class="submit" name="submit" type="submit">Submit</button>

how to redirect html page form php page?

Two files index.html and mail_handler.php
from this code that open mail_handler.php file but I don't want to open that file direct open HTML file index.html with a message.
submitted !!
After seeing a quick "email sent" message, I'd like the user to be directed back to the index.html page from mailhandler_auto.php.
mail_handler.php
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['subject'];
$msg=$_POST['msg'];
$to='xyz#yahoo.com'; // Receiver Email ID, Replace with your email ID
$subject='Form Submission';
$message="Name :".$name."\n"."Phone :".$phone."\n"."Subject :".$subject."\n"."Wrote the following :"."\n\n".$msg;
$headers="From: ".$email;
enter code here
if(mail($to, $subject, $message, $headers)){
echo '<script>alert("insert successfull");</script>';
}
else{
echo "Something went wrong!";
}
}
?>
This is index.html file i have used action method but in this code that move on mail_handler.php page but i want to go index.html page
index.html
<form action="mail_handler.php" method="post" name="form" class="">
<div class="form-group ">
<div class="">
<input class="form-control input-lg" type="text" name="name" placeholder="Name*" required >
</div><br>
<div class="">
<input class="form-control input-lg" type="email" name="email" placeholder="Email Address*" required >
</div><br>
<div class="">
<input class="form-control input-lg" type="subject" name="subject" placeholder="Subject*" required >
</div><br>
<div class="">
<textarea class="form-control" name="msg" placeholder="Message*" required rows="8" cols="80"></textarea>
</div><br>
<div class="">
<button class="btn btn-danger btn-lg" type="submit" value="send" name="submit">SUBMIT</button>
</div>
</div>
</form>
after sending the mail you can redirect the page in two ways
1) using JavaScript like your are using here to show the alert . Here you can simply add the script given below. It will redirect
window.open("index.html","_parent");
2) By using the PHP also you can do the same. Below is the example.Please add the script inside the if loop you can do
header('Location: index.html');
or
header('Location: ./');
Please try this.
Try this in the header tags of html:
`<meta http-equiv="refresh" content="5;url=http://www.example.com/page2.php">`
This will redirect it after 5 seconds. See here for more information.
As already suggested in others answers, you can redirect via javascript with window.open:
window.open("index.html","_self");
or via php with header()
header('Location: index.html');
Also you have the option to do everything in the same file:
$message=NULL;
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['subject'];
$msg=$_POST['msg'];
$to='xyz#yahoo.com'; // Receiver Email ID, Replace with your email ID
$subject='Form Submission';
$message="Name :".$name."\n"."Phone :".$phone."\n"."Subject :".$subject."\n"."Wrote the following :"."\n\n".$msg;
$headers="From: ".$email;
if(mail($to, $subject, $message, $headers)){
$message= '<script>alert("insert successfull");</script>';
}
else{
$message "Something went wrong!";
}
}
<form action="index.php" method="post" name="form" class="">
<div class="form-group ">
<div class="">
<input class="form-control input-lg" type="text" name="name" placeholder="Name*" required >
</div><br>
<div class="">
<input class="form-control input-lg" type="email" name="email" placeholder="Email Address*" required >
</div><br>
<div class="">
<input class="form-control input-lg" type="subject" name="subject" placeholder="Subject*" required >
</div><br>
<div class="">
<textarea class="form-control" name="msg" placeholder="Message*" required rows="8" cols="80"></textarea>
</div><br>
<div class="">
<button class="btn btn-danger btn-lg" type="submit" value="send" name="submit">SUBMIT</button>
</div>
</div>
Then you can create a condition
if ($message) {
echo $message;
} else {
//show the form
}
To show the form or the message
Note that using all the logic in one file could be easier and quicker but is prone to duplicate sending when refreshing the page.
Have a look at Post/Redirect/Get pattern to know more
Try
echo '<script>location.replace("index.html");</script>';

Why is message not appearing when using mail() function in 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'];

Categories