html form with php is not sending email - php

I'm writing to create a contact form on my website and then get the information sent to my inbox, but it is not working. Please take a look at my code below (PHP is not my thing) and let me know where I've gone wrong.
here is my html form
<form action="" method="post">
<div class="row">
<div class="col-md-6">
<input type="text" name="name" placeholder="Name"class="form-control" required>
</div>
<div class="col-md-6">
<input type="text" name="number" class="form-control" placeholder="Phone"required>
</div>
</div>
<input type="email" class="form-control" placeholder="Email" name="email" required>
<textarea name="message" class="form-control"style="resize:none;" placeholder="Message" rows="8" cols="30" required></textarea>
<input type="submit" name="submit" value="contact us">
</form>
This is my php code
<?php
$message ='';
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['message'];
$number = $_POST['number'];
$msg = $name ."<br>".$email."<br>".$comments."<br>".$number;
$to = "karunagoyal7#gmail.com";
$subject = "Email from Contact Us from";
if(mail( $to, $subject, $msg)){
$message = "<div class='alert alert-success'> your message has been send to saisoftlinktechnologies </div>";
}else{
$message = "<div class='alert alert-primary'> your message has not been send to saisoftlinktechnologies </div>";
}
}
?>
Both html from and php script is in same file.After submitting the form it is not even displaying the message of success or failure.

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

PHP code with google reCaptcha not sending email

My php webform was working until the point I added an if(isset) and also a reCaptcha.
Help?
Tried to debug the code - could not find an issue
html
<form class = "form-inline d-flex justify-content-center" action="contact.php" method = "post">
<div class="mc-field-group-1">
<input name="name" type="text" placeholder="Name" id="mce-LNAME"/>
</div>
<div class="mc-field-group-2">
<input name = "email" type="text" placeholder="Email address*" class="required email" id="mce-EMAIL" required/>
</div>
<div class="mc-field-group-3">
<input name = "subject" type="text" placeholder="Subject" class="required email" id="mce-EMAIL"/>
</div>
<div class="mc-field-group-4">
<textarea name="message" rows="10" cols="39" placeholder="Your message"></textarea>
</div>
<div class="g-recaptcha" data-sitekey="my_key(have the real key here in my code)"></div>
<div class="clear">
<input type="submit" name="submit" id="mc-embedded-subscribe" class="button" value="Send">
</div>
</form>
php
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "realemail#gmail.com";
$mailheader = "From: $email \r\n";
$secretKey = "my_key(have the real key here in my code)";
$responseKey = $_POST['g-recaptcha-response'];
$UserIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$UserIP";
$response = file_get_contents($url);
$response = json_decode($response);
if ($response ->success)
{
mail($recipient, $subject, $formcontent, $mailheader);
echo "Thank You!";
}
else
{
echo "Invalid Captcha, Please Try Again";
}
}
?>
When I click 'Send' in the form takes me to the contact.php page but email not sent to my gmail inbox. Has nothing to do with gmail as I said was working before I tweaked the code to add the reCaptcha.

php mail script form only returning headers

I have been having trouble getting my php mail script to work. I am fairly new at this so please stay with me.
My php mail form only seems to be returning the headers of the input fields, not the actual information that is typed in them..
Here is what my code is right now. Any help would be appreciated.
<form id="contact_form" class="cf" role="form" name="contact_form" method="post" action="mailscript.php">
<div id="left_form_container">
<div class="half_left_cf">
<input type="text" name="name" id="input_name" placeholder="Name">
<input type="email" name="email" id="input_email" placeholder="Email address">
<input type="text" name="subject" id="input_subject" placeholder="Subject">
</div>
</div>
<div id="right_form_container">
<div class="half_right_cf">
<textarea name="message" id="input_message" type="text" placeholder="Message"></textarea>
<div id="submit_container">
<input type="submit" name="submit" value="Submit" id="input_submit">
</div>
</div>
</div>
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "mail#website.com";
$body = "From: ".$name."\n E-Mail: ".$email."\n Message:\n" .$message;
if(empty($_POST['name'])){
$nameErr = "Name is required";}
if(empty($_POST['email'])){
$nameErr = "Email is required";}
if(empty($_POST['subject'])){
$nameErr = "Subject is required";}
if(mail ($to, $subject, $body)){
echo "<script>
alert('Thank you for sending your message!');
window.location.href='index.php'; </script";}
else {
echo "Mail was not sent!";}
?>
As I said, for some reason it just doesn't return any input from the boxes, only the headers..
Thanks for the help.

Can't seem to get PHP form to display

I am working on my version 2 of my portfolio site, I had a working mailer I created with a guide about a year ago, transferred it, and can't get my page to display now.
Here is the code I am using:
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST["My_Portfolio_Website"] ;
$message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email; "\n \nMessage: ".$message;
echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
}else{
//if "email" is not filled out, display the form
echo <form method="post" action="contact.php" class="connect">
<div>
<input id="name" name="name" type="text" required>
<label for="name">Your Name</label>
</div>
<div>
<input id="email" name="email" type="text" required>
<label for="email">Your Email</label>
</div>
<div>
<textarea id="message" name="message" required></textarea>
<label for="message">Your Message</label>
</div>
<div class="metro">
<div class="metro-button" type="submit">Click me</div>
</div>
</form>
}
?>
I ran it through a PHP syntax checker and it did not pull anything out, does anyone have any ideas?
For reference: Here is the original code I used on Version 1. I formatted it so it would display the email a little more cleaner, which probably royally screwed it up.
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST["My Portfolio Website"] ;
$message = $_REQUEST['message'].", Name: ".$name.", ".$phone.", Email: ".$email;
mail("TylerJStelmach#gmail.com", $subject, $message, "From:" . $email);
echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
}else{
//if "email" is not filled out, display the form
echo "<form method='post' action='index.php'>
<input type='text' input name='name' id='name' class='contacttext' placeholder=' Your Name' required>
<input type='text' input name='email' id='email' class='contacttext' placeholder=' Your Email Address' required>
<textarea input type='text' name='message' id='message' class='contacttext' placeholder=' Your Message' cols='55' rows='5' required></textarea>
<input type='submit' id='submit' class='submitcontacttext' value='Send Message'>
</form>";
}
?>
There are some syntax errors.
Also, your snipplet does not contain code to send mail.
Corrected code is as following:
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST["My_Portfolio_Website"] ;
$message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email; "\n \nMessage: ".$message;
echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
}else{
//if "email" is not filled out, display the form
echo '<form method="post" action="contact.php" class="connect">
<div>
<input id="name" name="name" type="text" required>
<label for="name">Your Name</label>
</div>
<div>
<input id="email" name="email" type="text" required>
<label for="email">Your Email</label>
</div>
<div>
<textarea id="message" name="message" required></textarea>
<label for="message">Your Message</label>
</div>
<div class="metro">
<div class="metro-button" type="submit">Click me</div>
</div>
</form>';
}
?>
as all are saying where is the line to send mail but as i have seen your site there is no sign of any code.
as i am just guessing, even if the if is false, the else should run as it is running in my localhost.
the problem must be something with your sever.
put a error_reporting(E_ALL); on top it will show errors if it there.
else try writing in a new file.
<?php
error_reporting(E_ALL);
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST["My_Portfolio_Website"] ;
$message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email; "\n \nMessage: ".$message;
echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
}else{
?>
<form method="post" action="contact.php" class="connect">
<div>
<input id="name" name="name" type="text" required>
<label for="name">Your Name</label>
</div>
<div>
<input id="email" name="email" type="text" required>
<label for="email">Your Email</label>
</div>
<div>
<textarea id="message" name="message" required></textarea>
<label for="message">Your Message</label>
</div>
<div class="metro">
<div class="metro-button" type="submit">Click me</div>
</div>
</form>
<?php
}
?>
I was able to figure this out by re-writing the original code I had based on the guide I used about a year ago, I came up with this for the answer which indeed works.
For all of the handling of where the information goes and how it was formated I included this chunk of php before I declared the doctype.
<?php
if($_POST["submit"]) {
$recipient="myemail#email.com";
$subject="Client Mail";
$name=$_POST["name"];
$email=$_POST["email"];
$email_from = $_POST['email'];
$message=$_POST["message"];
$mailBody="Name: $name\nEmail: $email\n\nMessage: $message";
mail($recipient, $subject, $mailBody, "From: $sender <$email>");
$thankYou="<p class='thank-you'>Thank you! Your message has been sent.</p>";
}
?>
From here, I figured out that the reason it wasn't submitting to it's own page was because my submit button was neither a button or input. I had it set as a div so with some tweaks here and there I rewrote the lower half to include an input as the submit. Which left me with this:
<?=$thankYou ?>
I included a small amount of text to display ( the $thankYou ) when you submit to the page and it reloads.
<form method="post" action="contact.php" class="connect">
<div>
<input id="name" name="name" type="text" required>
<label for="name">Your Name</label>
</div>
<div>
<input id="email" name="email" type="text" required>
<label for="email">Your Email</label>
</div>
<div>
<textarea id="message" name="message" required></textarea>
<label for="message">Your Message</label>
</div>
<div class="metro">
<input type="submit" name="submit" class="metro-button submit-me">
</div>
</form>
So now when submitted, the email comes in with:
The 'from' being set by $email
The 'subject' being set by $subject
and the message ($message) being formatted in this manner:
Name: John Doe
Email: JDoe#fake.com
Message: This is John Doe's message.
My apologies for the terrible question phrasing before hand, it was late at night and I was getting frustrated and losing my place, got a good nights rest and was able to solve it.
Here is the live version, I encourage you to test it, it works for me perfectly, hopefully this snippet can help someone else in the future!

PHP form submission returns blank

I'm a total novice so please bear with me :)…I've managed to create a form and used PHP to send the data to an email address. However, once I click submit; the screen goes blank instead of staying on the current page and displaying a message. I'm guessing i'm missing some sort of PHP code?
Also, i'd like to use the JQuery validator plugin on my form, how can I add it without basically screwing up the form?
MY HTML:
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>
MY PHP:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>
PHP script that you create will return an empty page, because that script just to send email. I think you need to combine PHP script and HTML script together with PHP script in top of script to get that you want and edit form action to empty like this sample:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>
<div>
<form id="form_id" name="form_name" action="" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>
Your form will take the user to scripts/index.php. You are echoing the '$feedback' var on the page with the HTML form. Redirect from scripts/index.php using
header("location: filelocation");
exit();
You can achieve this in two ways :
1. Have php and html code in one page.
2. Use ajax to submit your form.
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
</div>
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
echo '<p id="feedback">'.$feedback.'</p>'; <-- Notice this..
}
?>
You can also use ajax in jquery ($.ajax) or javascript.

Categories