PHP mail from HTML Form [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'm trying to link my HTMl form to my PHP contact code but it doesn't work, no mail is sending. I know all the PHP settings on the server are ok, I've done a test with a single-page-form and I received a mail.
Do you know what could be wrong ?
Thank you very much!
HTML CODE :
<section id="contact-full">
<div class="container">
<h2 class="title">Message</h2>
<form action="scripts/contact.php" role="form" id="contact_form" novalidate>
<div class="form-group">
<input type="text" class="form-control" id="contact_name" placeholder="Full name" name="name">
</div>
<div class="form-group">
<input type="email" class="form-control" id="contact_email" placeholder="Email Address" name="email">
</div>
<div class="form-group">
<textarea class="form-control" rows="4" placeholder="Your message or question" id="contact_message" name="message"></textarea>
</div>
<button type="submit" id="contact_submit" data-loading-text="•••" class="btn btn-lg btn-block btn-primary"><i class="icon icon-paper-plane"></i>Send</button>
</form>
</div>
</section>
PHP CODE :
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if(!empty($_POST['contactname']) && !empty($_POST['contactemail']) && !empty($_POST['contactmessage'])) {
$to = 'your#email.com'; // Your e-mail address here.
$body = "\nName: {$_POST['contactname']}\nEmail: {$_POST['contactemail']}\n\n\n{$_POST['contactmessage']}\n\n";
mail($to, "Message from yoursite.com", $body, "From: {$_POST['contactemail']}"); // E-Mail subject here.
}
}
?>

You're missing a few things;
First off in your HTML code you need to add method = "POST" to your <form> tag.
Secondly in your PHP code you grab all the incorrect names with $_POST. Change the following:
if(!empty($_POST['contactname']) && !empty($_POST['contactemail']) && !empty($_POST['contactmessage']))
to
if(!empty($_POST['contact_name']) && !empty($_POST['contact_email']) && !empty($_POST['contact_message']))
And all other parts of the code that call those objects.

Related

my php for sending email is not working [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
im working on a html form where i need to send mail using php. i need to upload it in my hosting server. i uploaded these two files in the root directory. still im not able to send mail.
Here is my html code
<!-- form -->
<form action="http://shankiyan.in/sendEmail.php" method="post" name="form" id="contactForm" enctype="application/x-www-form-urlencoded" accept-charset="UTF-8" >
<fieldset>
<div class="form-field">
<input name="name" type="text" id="contactName" placeholder="Name" value="" minlength="2" required="">
</div>
<div class="form-field">
<input name="email" type="email" id="contactEmail" placeholder="Email" value="" required="">
</div>
<div class="form-field">
<input name="subject" type="text" id="contactSubject" placeholder="Subject" value="">
</div>
<div class="form-field">
<textarea name="message" id="contactMessage" placeholder="message" rows="10" cols="50" required=""></textarea>
</div>
<div class="form-field">
<button class="submitform"type="submit" name="submit" id="submitform">Submit</button>
</fieldset>
</form>
<!-- Form End -->
and my php code is
<?php
require('sendEmail.php');
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$formsubject=$_POST['subject'];
$formmessage=$_POST['message'];
$to="shmediyosel#gmail.com";
$subject="subject:". $formsubject;
$message="Name".$name."\n"."wrote the following".$formmessage;
$headers="from".$email;
if(mail($to,$subject,$message,$headers))
{
echo"<h1>message sent successfully</h1>";
}
else
{
echo"<h2> something went wrongg!</h2>";
}
?>
i cant send mail from my html form .
please help me out what mistake i have done ?
Try to put this code on the top of your php file and check for any errors:
error_reporting(E_ALL);
ini_set('display_errors', 1);
Also, is the sendEmail.php requiring itself? Because this will not work.
If not, can you provide us with the sendEmail.php so we can understand easier the problem?

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

Email sending using PHP and HTML without any database [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I want to send emails without using any database that's why I was trying to send an email using PHP and HTML, but the problem is that after pressing the submit button it does not sending any emails. Here are my codes:
<form role="form" name="contactform" id="contactform" method="POST" action="send.php" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="input-text form-group">
<input type="text" name="contact_name" class="input-name form-control" placeholder="Full Name" />
</div>
<div class="input-text form-group">
<input type="email" name="email" class="input-name form-control" placeholder="Enter Email-ID" />
</div>
<div class="input-text form-group">
<input type="text" name="contact_phone" class="input-name form-control" placeholder="Phone Number" />
</div>
</div>
<div class="col-md-6">
<div class="input-text form-group">
<input type="text" name="contact_sub" class="input-name form-control" placeholder="Subject" />
</div>
<div class="textarea-message form-group">
<textarea name="contact_message" class="textarea-message hight-82 form-control" placeholder="Message" rows="2"></textarea>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-default top-margin-10 hblack" name="Submit" value="Submit" />
</div>
</div>
</form>
and the PHP code are:
<?php
$strTo = $_POST["contact_name"];
$strSubject = $_POST["email"];
$strMessage = nl2br($_POST["contact_phone"]);
$strSub = nl2br($_POST["contact_sub"]);
$strmsg = nl2br($_POST["contact_message"]);
$body="Job-title:$title \r\n
Name:$strTo \r\n
Email:$strSubject \r\n
Phone:$strMessage \r\n
Subject:$strSub \r\n
Message:$strmsg";
$from=$strSubject;
$to='abc#abc.com'; // removed for spam protection
mail($to,$from,$body);
echo "<script> alert('Your Form has been Submitted successfully'); window.location.href='index.php'; </script>";
?>
Thanks in advance.
If you use the mail function then you will have to make some server side changes to your php settings. Most commonly these changes are found in a file called "php.ini". I encourage looking up changes needed for the mail function to work on your specific server and including details about your server and php version when searching

Email PHP form doesn't work [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I haven't written code in years, so I am going off old code from a project that is over 5 years old, so I am not surprised that it doesn't work; I would like some pointers on how to make it work, please.
Here is what I have in my HTML email form --
<form action="fydcontact.php" method="post">
<div class="form-group">
<!--<label for="contact_name">Name:</label>-->
<input type="text" id="contact_name" class="form-control" placeholder="Name" />
</div>
<div class="form-group">
<!--<label for="contact_email">Email:</label>-->
<input type="text" id="contact_email" class="form-control" placeholder="Email Address" />
</div>
<div class="form-group">
<!--<label for="contact_message">Message:</label>-->
<textarea id="contact_message" class="form-control" rows="9" placeholder="Write a message"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
</form>
and here is what my PHP looks like --
<?php
if(isset($_POST['send'])) {
// Prepare the email
$to = ''foryourdayformals#gmail.com ';
$name = $_POST['contact_name'];
$mail_from = $_POST['contact_email'];
$subject = 'Message sent from website';
$message = $_POST['contact_message'];
$header = "From: $name <$mail_from>";
// Send it
$sent = mail($to, $subject, $message, $header);
if($sent) {
echo 'Your message has been sent successfully! Return to For Your Day, LLC Website';
} else {
echo 'Sorry, your message could not send. Please use direct email link on Contact Us page (below the map). Return to For Your Day, LLC Website';
}
}
?>
Any help is GREATLY appreciated! Thanks!
update
$to = ''foryourdayformals#gmail.com ';
to
$to = 'foryourdayformals#gmail.com';
and need to add name attribute for form . This update your form
<form action="fydcontact.php" method="post">
<div class="form-group">
<!--<label for="contact_name">Name:</label>-->
<input type="text" id="contact_name" name="contact_name" class="form-control" placeholder="Name" />
</div>
<div class="form-group">
<!--<label for="contact_email">Email:</label>-->
<input type="text" id="contact_email" name="contact_email" class="form-control" placeholder="Email Address" />
</div>
<div class="form-group">
<!--<label for="contact_message">Message:</label>-->
<textarea id="contact_message" name="contact_message" class="form-control" rows="9" placeholder="Write a message"></textarea>
</div>
<button type="submit" class="btn btn-primary" name ="send">Send</button>
</form>

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