So I've been trying to get a PHP script to send an email to myself when a page form is filled out. An example of the page is found here:
http://tonybenwhite.byethost8.com/WSAWebpage/Action.html
The hosting service DOES allow PHP, and my web dev instructor said the PHP looks correct, but the email won't send. Here's the PHP script and the HTML form:
<?php
if(isset($_POST['send'])){
$to_address="my#email.com";
$subject="WSA Day of Action Entry";
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$street1=$_POST['street1'];
$street2=$_POST['street2'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$initials=$_POST['initials'];
$message="Name: " .$firstname." ".$lastname."\n";
$message .="Email: " .$email."\n";
$message .="Street: " .$street1."\n";
$message .="Street: " .$street2."\n";
$message .="City: " .$city."\n";
$message .="State: " .$state."\n";
$message .="Zip Code: " .$zip."\n";
$message .="Initials: " .$initials."\n";
$headers = 'From: '.$email."\r\n".
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
'X-Mailer: PHP/' .phpversion();
mail($to_address, $subject, $message, $headers);
}
?>
<form name="infoForm" method="post" action="email.php">
<fieldset class="formfield">
<p class="form">
Are you a registered voter in the state of Washington?<br>
<input type="checkbox" id="yesBox"/> Yes<br><br>
First Name: <input type="text" id="firstName" name="firstName"><br>
Last Name: <input type="text" id="lastName" name="lastName"><br><br>
Email: <input type="text" id="email" name="email" size="30"><br><br>
Street Address: <input type="text" id="street1" name="street1" size="30"><br>
<input type="text" id="street2" name="street2" size="30"><br>
City: <input type="text" id="city" name="city" size="30"><br>
State: <input type="text" id="state" name="state" size="30"><br>
Zip Code: <input type="text" id="zip" name="zip" size="30"><br><br>
Initials: <input type="text" id="initials" name="initials" size="3"><br><br>
<input type="submit" name="send" class="inputButton" id="send" value="Submit" disabled=true/> <input type="reset" name="resetFields" class="inputButton" id="reset" value="Reset"/><label id="disableLabel"> <i><font size="2px">Please confirm you are a WA State Voter!</font></i></label>
</p>
</fieldset>
</form>
Any ideas what I'm doing wrong?
Edit: The link you provided does have JS to work in conjunction with your submit button.
Disregard what was posted originally as strikethroughs, but continue reading passed that.
I don't know if this is your full and actual code, whether you have JS to check if all fields are filled, yet this disabled=true in:
<input type="submit" name="send" class="inputButton" id="send" value="Submit" disabled=true/>
The submit button will not be clickable since the submit is disabled / set to true.
This won't prevent mail from doing its job, however some of your form elements do not match your POST variables.
For instance:
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
along with name="firstName" and name="lastName"
those are case-sensitive, therefore they should be changed to name="firstname" and name="lastname" respectively.
Using the following in two seperate files was sent and received successfully. I'm wondering if you are using your entire code inside the same file.
If so, use action="" instead of email.php IF your file isn't named email.php
PHP
<?php
if(isset($_POST['send'])){
$to_address="my#email.com";
$subject="WSA Day of Action Entry";
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$street1=$_POST['street1'];
$street2=$_POST['street2'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$initials=$_POST['initials'];
$message="Name: " .$firstname." ".$lastname."\n";
$message .="Email: " .$email."\n";
$message .="Street: " .$street1."\n";
$message .="Street: " .$street2."\n";
$message .="City: " .$city."\n";
$message .="State: " .$state."\n";
$message .="Zip Code: " .$zip."\n";
$message .="Initials: " .$initials."\n";
$headers = 'From: '.$email."\r\n".
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
'X-Mailer: PHP/' .phpversion();
mail($to_address, $subject, $message, $headers);
}
?>
HTML form:
<form name="infoForm" method="post" action="email.php">
<fieldset class="formfield">
<p class="form">
Are you a registered voter in the state of Washington?<br>
<input type="checkbox" id="yesBox"/> Yes<br><br>
First Name: <input type="text" id="firstname" name="firstname"><br>
Last Name: <input type="text" id="lastname" name="lastname"><br><br>
Email: <input type="text" id="email" name="email" size="30"><br><br>
Street Address: <input type="text" id="street1" name="street1" size="30"><br>
<input type="text" id="street2" name="street2" size="30"><br>
City: <input type="text" id="city" name="city" size="30"><br>
State: <input type="text" id="state" name="state" size="30"><br>
Zip Code: <input type="text" id="zip" name="zip" size="30"><br><br>
Initials: <input type="text" id="initials" name="initials" size="3"><br><br>
<input type="submit" name="send" class="inputButton" id="send" value="Submit" />
<input type="reset" name="resetFields" class="inputButton" id="reset" value="Reset"/>
<label id="disableLabel"> <i><font size="2px">Please confirm you are a WA State Voter!</font></i></label>
</p>
</fieldset>
</form>
Related
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.
I am a PHP noob, so i don't know what to write in my message variable. I need my data from the form to be sent to the email. I got a textarea, 2 inputs (name, email). Want the text from input to be sent to my email. Here code i have:
<?
if((isset($_POST['name'])&&$_POST['name']!="") && (isset($_POST['phone'])&&$_POST['phone']!="")){
$to = 'rayetzkiillya#gmail.com';
$subject = 'Обратный звонок';
$message;
$headers = "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: Отправитель <from#example.com>\r\n";
mail($to, $subject, $message, $headers);
}
?>
<form action="send.php" class="postcard" method="post">
<span>Your message:</span>
<textarea type="text" value="" required></textarea>
<div id="stripe1"></div>
<div id="stripe2"></div>
<img src="./images/Seal_of_the_United_States_Department_of_the_Post_Office.svg. png" alt="Oops" id="seal" />
<img src="./images/stamp.jpg" alt="Stamp" id="stamp" />
<div class="inputs">
<div class="inputs" id="input1"><label for="to" type="text" id="to">to: </label> <input type="text" value=" Me" readonly><div id="stripe3"></div></div>
<div class="inputs" id="input2"><label for="from" type="text" id="from">from: </label> <input type="text" id="input2"><div id="stripe3"></div></div>
<div class="inputs" id="input3"><label for="email" type="text" id="email">email: </label> <input type="text" id="input3"><div id="stripe3"></div></div>
<button type="button" class="btn btn-primary" id="send_button">Send</button>
</form>
You can make your message an HTML string, or just plain text.
For plain text, you can do something like
$message .= "name: ".$_POST['name']."\r\n";
$message .= "Message: ".$_POST["theMessage"];
However you need to name your inputs in the HTML as well,
so
<input type="text" id="input2" name="name">
<textarea type="text" value="" required name="theMessage"></textarea>
Or similar.
For the from address, you have to use change your header line
change
$headers .= "From: Отправитель <from#example.com>\r\n";
to
$headers .= "From: ".$_POST["name"]." <".$_POST["from"].">\r\n";
And obviously name your input accordingly:
<input type="text" id="input3" name="from">
Also, as suggested by others, you should be sanitizing/validating these values before using them.
I have a form that requests execution of php file
Here is the form code:
<form name="First Form" method="post" action="email_script.php">
<input type="text" name="name" placeholder="Ваше Имя">
<input type="text" name="telephone" placeholder="Ваш Телефон">
<input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
</form>
Here is the php code:
<?php
// the message
$message .= $_POST["name"];
$message .= $_POST["telephone"];
// use wordwrap() if lines are longer than 70 characters
$message = wordwrap($message,70);
// send email
mail("nsaann#gmail.com","Заказ",$message);
?>
How can I add the form name to be a part of the message I email?
How can I make values of name, telephone, form name to be in differrent rows of the email body?
(Rigt now what I receive as email is this:
Name+1234567890
but I rather want it to be like this:
Name
+1234567890)
The reason I ask is because I want to create multiple html files with multiple forms that all requests execution of the same email_script.php and I want to know how each individual page performs in collecting orders. Thanks
So I edited the code and here it is:
<form name="First_Name" method="post" action="email_script.php">
<input type="hidden" name="First Name">
<input type="text" name="name" placeholder="Ваше Имя">
<input type="text" name="telephone" placeholder="Ваш Телефон">
<input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
</form>
php code:
<?php
if(isset($_POST["name"]) and isset($_POST["telephone"])){
$message = $_POST["name"]."<br>"; //use <br> and set the email headers to: Content-type: text/html
$message .= $_POST["telephone"]."<br>";
$message .= key($_POST); //form name
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail("nsaann#gmail.com", "Заказ", $message, $headers);
}
?>
And here is what I got as a result: http://postimg.org/image/4gj5gh22p/
Unfortunately I still got "name" instead of "First_Name"
Getting the name of a form is not possible as it's not sent as part of the POST data.
An alternative from this post suggests adding a hidden input element inside the form with the same name as the form:
<form name="First Form" method="post" action="email_script.php">
<input type="hidden" name="First Form">
<input type="text" name="name" placeholder="Ваше Имя">
<input type="text" name="telephone" placeholder="Ваш Телефон">
<input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
</form>
You can then use $_POST['First Form']; to get the name.
<form name="First Form" method="post" action="emailscript.php">
<input type="hidden" name="form" value="First Form">
<input type="text" name="name" placeholder="Ваше Имя">
<input type="text" name="telephone" placeholder="Ваш Телефон">
<input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
</form>
and use it using
$message .= $POST['form']
So finally I have the solution (My brother helped me with this).
here is my html code:
<form name="First_Name" method="post" action="email_script.php">
<input type="hidden" name="form_name" value="First Name">
<input type="text" name="name" placeholder="Ваше Имя">
<input type="text" name="telephone" placeholder="Ваш Телефон">
<input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
</form>
here is my php code:
<?php
if(isset($_POST["name"]) and isset($_POST["telephone"])){
$message = $_POST["name"]."<br>"; //use <br> and set the email headers to: Content-type: text/html
$message .= $_POST["telephone"]."<br>";
$message .= $_POST["form_name"]."<br>";
//$message .= key($_POST); //form name
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail("nsaann#gmail.com", "Заказ Парня", $message, $headers);
}
?>
It works via hidden input and the resulted email looks like this:
Nazar
+380953648591
First Name
can anybody help me fix my php code so that it will send the form data to the email address? i am using MAMP Server to check wether it works the website works as planned however once i fill the form out and press submit it takes me to the PHP page however no email is sent.
PHP CODE
<?php
if(isset($_POST['submit'])){
$emailSubject = 'Customer Has a Question!';
$webMaster = 'r1bos#hotmail.com';
$nameField = $_POST ['Full_Name'];
$emailField = $_POST['E-Mail'];
$phoneNumber = $_POST['Phone_Number'];
$questionField = $_POST ['Query'];
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Questions: $question <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
#mail($webMaster, $emailSubject, $body, $headers);
exit;
}
?>
HTML FORM
<form action="Php/form.php" method="post" name="Contact_Form" >
<label for="fullname">Full Name: </label>
<input type="text" name="Full_Name" id="name"><br>
<label for="E-Mail">E-Mail: </label>
<input type="text" name="E-Mail" id="email" ><br>
<label for="PhoneNumber">Phone Number: </label>
<input type="text" name="Phone_Number" id="phonenumber" ><br>
<label for="Query">Query: </label>
<textarea name="Query" rows="4" cols="21" id="query" ></textarea><br>
<input type="submit" name="submit" value="Send" >
</form>
I'm new to WordPress. I create a static page contact.php. In contact page, I have a contact form, so users would get in touch with me. When the user would click the submit button it should go to index.php and prompting the success of email delivery otherwise display not successful..
this is the code of contact.php
<form class="form" method="post" action="/send-email.php">
<p class="name">
<input type="text" name="name" id="name" placeholder="Enter your name" size="40" />
<label for="name">Name</label>
</p>
<p class="email">
<input type="text" name="email" id="email" placeholder="mail#example.com" size="40" />
<label for="email">Email</label>
</p>
<p class="web">
<input type="text" name="telephone" id="telephone" placeholder="000-000-000" size="40" />
<label for="telephone">Telephone</label>
</p>
<p class="text">
<label for="message">Message</label>
<textarea name="message" placeholder="Write something to us" cols="40" rows="5" /></textarea>
</p>
<p class="submit">
<input type="submit" value="Send" />
<input type="reset" value="Cancel" />
</p>
</form>
send-email.php
<?php
$ToEmail = 'sample#sample.com';
$EmailSubject = 'Contact Form';
$mailheader = "From: " . $_POST["email"] . "\r\n";
$mailheader .= "Reply-To: " . $_POST["email"] . "\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = " <strong>Name:</strong> " . $_POST["name"] . "";
$MESSAGE_BODY .= "<br> <strong>Email:</strong> " . $_POST["email"] . "";
$MESSAGE_BODY .= "<br> <strong>Telephone:</strong> " . $_POST["telephone"] . "";
$MESSAGE_BODY .= "<br><br> <strong>Message:</strong> " . nl2br($_POST["message"]) . "";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die("Failure");
/* echo "<pre>";
print_r($MESSAGE_BODY);
echo "<pre>"; */
header("Location: /index");
But it's not sending. And the URL looks just like www.site.com/send-email.php
What am I missing in here? Any ideas? I would really appreciate your help. Thanks.
Sounds like die("Failure") might be kicking in and logging the "Failure" message instead of printing it on screen. Or the mail module isn't installed, so it is running into an error where it doesn't know what the mail() function even is—admittedly unlikely. Not too sure.
My guess is that your server is not even set up to send mail properly yet. That can be a complicated setup if you are running your own server. If you are on a shared server, it really should be set up already... unless your host is not very good.
In any case, if you are running in WordPress I'd recommend installing the Jetpack plugin by the folks who made WordPress itself. It includes a feature that adds a contact form to a page with a few clicks of your cursor.
If that form won't send an email, there is something wrong that is beyond the scope of the information you provided in your question.
<form class="form" method="post" action="index.php">
<p class="name">
<label for="name">Name</label>
<input type="text" name="name" id="name" placeholder="Enter your name" size="40" />
</p>
<p class="email">
<label for="email">Email</label>
<input type="text" name="email" id="email" placeholder="mail#example.com" size="40" />
</p>
<p class="web">
<label for="telephone">Telephone</label>
<input type="text" name="telephone" id="telephone" placeholder="000-000-000" size="40" />
</p>
<p class="text">
<label for="message">Message</label>
<textarea name="message" placeholder="Write something to us" cols="40" rows="5" /></textarea>
</p>
<p class="submit">
<input type="submit" name="submit" value="Send" />
<input type="reset" value="Cancel" />
</p>
</form>
AND YOUR index.php
<?php
if(isset($_POST['submit'] == 'Send')) {
$ToEmail = 'sample#sample.com';
$EmailSubject = 'Contact Form';
$mailheader = "From: " . $_POST["email"] . "\r\n";
$mailheader .= "Reply-To: " . $_POST["email"] . "\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = " <strong>Name:</strong> " . $_POST["name"] . "";
$MESSAGE_BODY .= "<br> <strong>Email:</strong> " . $_POST["email"] . "";
$MESSAGE_BODY .= "<br> <strong>Telephone:</strong> " . $_POST["telephone"] . "";
$MESSAGE_BODY .= "<br><br> <strong>Message:</strong> " . nl2br($_POST["message"]) . "";
$status = mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die("Failure");
}
if($status){
echo "your success message";
}
?>
try this
ONE MORE THING
Since you are using this on wordpress you should better use wp_mail funciton of wordpress