php email from html form - php

So I've had some issues trying to get my php code to work. I found a form php handler which I want to send the email on the back end for the users.
<?php
if(isset($_POST['submit'])){
ob_start();
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];//Sender first name//
$last_name = $_POST['last_name'];//sender last name//
$subject = "Contact Request.";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$to = "-myemail#outlook.com-"; // this is your Email address
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
header('Location: http://-mysite-/thankyou.html');
exit();
}
?>
Here is the HTML form info:
<form action="php/email.php" method="post" name = "email form" enctype="multipart/form-data" class="container" align="center">
<label for="firstname"></label>
<strong>*</strong><input type="text" placeholder="Enter First Name" name="first_name" required>
<br>
<label for="lastname"></label>
<strong>*</strong><input type="text" placeholder="Enter Last Name" name="last_name" required>
<br>
<label for="email"></label>
<strong>*</strong><input type="text" placeholder="Enter Email" name="email" required>
<br>
<label for="phoneno"></label>
<input type="phoneno" placeholder="Enter Phone Number" name="phone_number">
<br>
<label for="interested"></label>
<t> Brief description of project:</t><br>
<textarea style="width:100% height:60%" align="center" name="message">
</textarea><br>
<button type="submit" class="btn" value="Send Form"><strong>Submit</strong></button>
</form>
I can't figure out why the page on submit does redirect to the email.php but doesn't do anything from there. Do I need to contact the site support team to restart my php services? I've been smacking my head against the keyboard for the last 2 days as to why this isn't working.

Because you haven't field in your form with name="submit" and trying to get its value in PHP: if(isset($_POST['submit'])).
Bonus tip: don't use name submit for any field of the form, cause some browsers getting fooled with this. Instead use formsubmit, submitted or anything else.
just add this field in your form
<input type="hidden" name="submitted" value="1">
andchange condition in PHP to
if(isset($_POST['submitted']) && intval($_POST['submitted']) == 1)...

Related

How to include default value with input data

So I built a php email form that allows users to submit inquiries which I got up and running, except there is one caveat. When the users enter their data and clicks submit, I get an email with the data they inputted but it ONLY shows the inputted data and not the value along with it. For example:
Now this is the email that comes over:
So again, my question is how do I get the input values to show in addition to the users inputted data in the email that's received? Example of what I want:
Company Name: David
Number of Employees: 3
Current Coverage: None
Telephone Number: 123-456-7890
Location: New Jersey
Email Address: test#gmail.com
I hope this makes sense. Here is my email form code below as well:
HTML FORM
<form action="index.php"
method="post"
enctype="multipart/form-data"
name="EmailForm">
<input type="text" name="subject" class="form-
control"
value="New Quote Request" required="required"
hidden>
<label for="name">Company Name</label>
<br>
<input type="text" name="name" class="form-control"
placeholder="Company Name" required="required">
<br>
<label for="employees">Number of Employees</label>
<br>
<input type="number" name="employees" class="form-
control"
placeholder="# of Employees" required="required">
<br>
<label for="coverage">Current Coverage</label>
<br>
<input type="text" name="coverage" class="form-
control"
placeholder="Current Coverage"
required="required">
<br>
<label for="telephone">Telephone Number</label>
<br>
<input type="text" name="telephone" class="form-
control"
placeholder="Telephone #" required="required">
<br>
<label for="email">Email Address</label>
<br>
<input type="email" name="email" class="form-
control"
placeholder="Email Address" required="required">
<br>
<label for="location">Location</label>
<br>
<input type="text" class="form-control"
name="location"
placeholder="Location" required="required">
<br>
<button type="submit" value="submit" class="btn
btn-primary">Submit</button>
<br><br>
</form>
PHP CODE
<?php
// get email from the config file
$config = require_once __DIR__ . '/../config/app.php';
$recipient_email = $config['mail']['to_email'];
// contact information
$company_name = $inputs['name'];
$employees = $inputs['employees'];
$coverage = $inputs['coverage'];
$telephone = $inputs['telephone'];
$location = $inputs['location'];
$contact_email = $inputs['email'];
$subject = $inputs['subject'];
// Email header
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=utf-8';
$headers[] = "To: $recipient_email";
$headers[] = "From: $contact_email";
$header = implode('\r\n', $headers);
$body = $message . "\n" . $company_name . "\n" . $employees . "\n" . $coverage . "\n" . $telephone . "\n" . $location . "\n" . $contact_email;
mail($recipient_email, $subject, $body, $header);
The label text isn't transferred to the server along with the form data, that's not how HTML forms work.
So whatever text you want in your email, you have to include it yourself in the php code which generates that email.

PHP file name added to URL but not called from html

When using a html form to call php to send an email with the details in the form to an email address specified in the php. The php file name is added to the URL but no email is sent and the page shown in blank.
The website is being hosted on my.ionos.co.uk (1&1) using php 7.3.
<form method="post" action="contact-form.php">
<input class="contact" type="text" name="Name" placeholder="Name">
<input class="contact" type="text" name="Email" placeholder="Your Email Address">
<input class="contact" type="text" name="Subject" placeholder="Subject">
<textarea class="contact" name="Message" rows="10" placeholder="Message"></textarea>
<input class="contact-submit" type="submit" value="Send Message">
</form>
<?php
if(isset($_POST['submit'])) {
$name = $_POST['Name'];
$mailFrom = $_POST['Email'];
$subject = $_POST['Subject'];
$message = $_POST['Message'];
$mailTo = "X#hotmail.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$Message;
if(mail($mailTo, $subject, $txt, $headers)){
echo "<h1>Mail send successfully</h1>";
} else {
echo "<h1>Mail failed to send<h1>";
}
header("Location: contact.html?mailsend");
}
?>
I'd appreciate it if someone could help, thanks in advance!
Try to change:
if(isset($_POST['submit'])) {
with
if(isset($_POST['Email'])) { //or another field in your form. If you want you can also add in your submit button: name="submit"
Checking out your code seems there is not an input with name submit. So you never enter in the if case.

Can't get email sending with HTML form using PHP

I am trying to make my own HTML / CSS form processed with PHP. The form has no errors that I can tell, just something going wrong with the PHP. I am not receiving the message from the form. I'm new with PHP and trying to learn about it. Any help is appreciated.
HTML form:
<form class="grn-background" action="processing.php" method="post">
<label for="fullname">Full Name</label>
<input id="fullname" name="fullname" type="text">
<label for="email">Email Address</label>
<input id="email" name="email" type="email">
<label for="message">Message</label>
<textarea id="message" name="message"></textarea>
<input type="submit" value="Send Message">
</form>
processing.php file
$full_name = $_POST['fullname'];
$email = $_POST['email'];
$message = $_POST{'message'};
$to = 'services#brentnicolet.com';
$subject = 'BNWS Contact Form Submission';
$form_message = "$message";
mail($to, $subject, 'From' . $email);
echo 'Thank you' . $full_name . '. We have received your message and will respond within the next 24 - 48 hours.';
?>
look at https://github.com/PHPMailer/PHPMailer for minimum requirement to run php-mailer
also note if you are using localhost - it will not work, as well as on github. It works when you have it on the hosted website.

PHP mail form - only one variable not submitting

I have set up a PHP mail form set up that only correctly outputs some of the variables entered in the form. It DOES mail the $name and $email variables, but not the $message variable.
The php to send the form is here:
<?php
if($_SERVER["REQUEST_METHOD"] == "POST" ){
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
//sending email
require_once("siteIncludes/class.phpmailer.php");
$mail = new PHPMailer();
$email_body = "";
$email_body = $email_body . "Name: " . $name . $message . "<br />";
$email_body = $email_body . "Email: " . $email . "<br />";
$email_body = $email_body . "Message: " . $message;
$mail->SetFrom("$email,$name");
$address = "foo#bar.com";
$mail->AddAddress($address);
$mail->Subject = "Form Submission | ".$name;
$mail->MsgHTML($email_body);
if(!$mail->Send() ){
echo 'There was a problem sending the email: '.$mail->ErrorInfo;
exit();
}
header("Location: myContact.php?status=thanks");
exit();
};
?>
And the HTML that sets up the form is here:
<div id="contactFormWrap" class="span6 offset3">
<form method="post" action="myContact.php" id="contactForm">
<div>
<label for="name">Please leave your name</label>
<input type="text" name="name" id="name" value="" class="required" />
</div>
<div>
<label for="email">and your email</label>
<input type="text" name="email" id="email" value="" class="required email" />
</div>
<div>
<label for="subject">What is your message about?</label>
<input type="text" name="subject" id="subject" value="" class="required" />
</div> -->
<div>
<label for="message">and your message</label>
<textarea name="message" id="message" value="" rows="10" class="required"></textarea>
</div>
<div id="messageButtons">
<input type="submit" value="Submit" name="contactSubmit" id="contactSubmit" class="sendEmail btn" />
</div>
</form>
</div>
I hope that was enough information. Does anyone know why the $message variable isn't being output to the submitted email?
thanks
I think this is your problem:
value=""
The <textarea> tag does not have a value attribute, however different browsers have different ways of handling invalid code, so whatever browser you are using must be using the value found in this invalid attribute instead of what you type in the actual text box.
Just do:
<textarea name="message" id="message" rows="10" class="required"></textarea>

unable to echo message sent in html page using php

I currently have this page I am working on:
http://www.webauthorsgroup.com/new/template/index3.html
In the lower right corner is a form (php), but I can't "echo" a text message after it has been submitted (for whatever reason).
My form is:
<form id="contact" method="post" action="">
<fieldset>
<label for="name">Name</label>
<input type="text" name="name" placeholder="Full Name" title="Enter your name" class="required">
<label for="email">E-mail</label>
<input name="hidden" class="required email" onblur="if(value=='<?php echo htmlentities($_POST['email']); ?>') value = 'Your Email'" onfocus="if(value=='Your Email') value = ''" type="email" value="Your Email" placeholder="yourname#domain.com">
<label for="phone">Phone</label>
<input name="phone" onblur="if(value=='<?php echo htmlentities($_POST['phone']); ?>') value = 'phone'" onfocus="if(value=='phone') value = ''" type="tel" value="phone" placeholder="ex. (555) 555-5555">
<input type="hidden" name="phone" placeholder="ex. (555) 555-5555">
<label for="message">Question/Comment</label>
<textarea name="message" onblur="if(value=='<?php echo htmlentities($_POST['message']); ?>') value = 'message'" onfocus="if(value=='message') value = ''" value="message" placeholder="Message"></textarea>
<input type="submit" name="submit" class="button" id="submit" value="Send Message" />
</fieldset>
</form>
=========================================================
the process.php is:
<?php
if(isset($_POST['submit']))
{
// Get Data
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$url = strip_tags($_POST['url']);
$message = strip_tags($_POST['message']);
echo "Thank You!";
}
// Send Message
mail( "bruce#webauthorsgroup.com", "Inquiry From WebAuthorsGroup",
"Name: $name\nEmail: $email\nPhone: $phone\nWebsite: $url\nMessage: $message\n",
"From: Forms <forms#example.net>" );
?>
=================================================
I need the "message sent" text echoed in the same div after submitting the form and I don't want to convert my index page to index.php
Any help would be great!
The PHP code on the page index3.html is not being parsed or executed by the PHP interpreter on your server because the file extension is .html. Go ahead and view source on that page in the browser. Note that you can see the PHP code. You should not be able to see PHP code in the HTML source. You should see only the HTML rendered by the PHP. Please change the file extension to .php so your server can execute it instead of outputting it as text.

Categories