Sending email with data from form - php

I have problem with simple contact box. I want to send informations from form to my email.
Already tried with register globals on and off (.httacess), tried with simple $formname and $_POST['formname'] but still getting email without form info.
<?php
if ($action == "send"){//isset wyslij
//if (!$name || !$email || !$phone || !$enquiry) {
//$problem = TRUE;
//echo("<p>You have to fill all form.</p>");
//}
if (!$problem){
$name = $_POST['name'];
$data = date("d.m.y");
$message = "
<p>Name: $name</p>
<p>Phone: $phone</p>
<p>Email: $email</p>
<br>
<p>Enquiry: $enquiry</p>";
$od = "blabla#email.com";
$content = $message;
$header = "From: $od \r\n";
$header .= 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
(mail('myemail#gmail.com', 'New message from website Angelzena', $content, $header));
echo("<br><p>Message has been sent.</p>");
}else{
echo("<p>Try <a href=contact.php>again</a></p>");
}
}
?>
<form action="contact.php?action=send" method="post" enctype="text/plain">
<label for="name">Name</label><input type="text" name="name" /></br></br>
<label for="email">Email</label><input type="text" name="email" /></br></br>
<label for="phone">Phone</label><input type="text" name="phone" /></br></br>
<label for="enquiry">Enquiry</label><textarea name="enquiry" cols="20" rows="10"></textarea></br></br>
<input type="submit" id="contact_button" value="Send" />
</form>

<form action="contact.php?action=send" method="post" enctype="text/plain">
When POSTing forms the enctype attribute should be application/x-www-form-urlencoded or multipart/form-data.
PHP does not handle forms POSTed as text/plain.

Related

I have created a form, but posts are not coming

I have created a form, but posts are not coming. Form Code
<form method="post" action="contact.php">
<form class="quote">
<div>
<label>Name</label><br>
<input type="text" placeholder="Name">
</div>
<div>
<label>Email</label><br>
<input type="email" placeholder="Emial Address">
</div>
<div>
<label>Message</label><br>
<textarea placeholder="Message"></textarea>
</div>
<button class="button_1" type="submit">Send</button>
</form>
contact.php code
<?php $to = 'demo#spondonit.com'; $firstname = $_POST["fname"]; $email= $_POST["email"]; $text= $_POST["message"]; $headers = 'MIME-Version: 1.0' . "rn"; $headers .= "From: " . $email . "rn"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn"; $message ='<table style="width:100%"> <tr> <td>'.$firstname.' '.$laststname.'</td> </tr> <tr><td>Email: '.$email.'</td></tr> <tr><td>Email: '.$text.'</td></tr> </table>'; if (#mail($to, $email, $message, $headers)) { echo 'The message has been sent.'; }else{ echo 'failed'; } ?>
Maybe the route of the page is wrong, try:
action="/contact.php">
if it's on the same folder
You really need to provide more detail - what does it mean that posts are not coming? Do you see the "failed" message or nothing at all?
But, most importantly: you use identifiers such as $_POST['fname'] but there is nothing in your HTML indicating that a text input value should be sent as "fname". You need to add a name attribute to all of your <input>s, for example:
<input type="text" name="fname" placeholder="Name">
Neither placeholder= or <label> suffice - they only affect how the form is displayed on the webpage and not how it is sent to the server.

Unable to replace form with message on submission

I've created an HTML5 form, which incorporates reCAPTCHA, and I've also written a PHP script that sends an email when the form is submitted. At the moment, the script redirects the user to an error or thankyou page, but I'm trying to adjust it to dynamically replace the form within a message within the same page.
I've tried the following script, but it displays the message as soon as the page loads, before any user interaction.
PHP/HTML:
<?php
if ($_POST) {
// Load reCAPTCHA library
include_once ("autoload.php");
$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
$emailFrom = $email;
$emailTo = "my#email.com";
$subject = "Contact Request";
// Prepare email body text
$body = "<strong>Name:</strong> $name <br /> <strong>Email:</strong> $email <br /> <strong>Message:</strong> $message";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $name <$emailFrom>" . "\r\n";
$secret = 'XXX';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'],$_SERVER['REMOTE_ADDR']);
echo 'Your message was submitted!';
} else {
?>
<div class="contact-form">
<form role="form" method="post" action="index.php">
<label for="name"><span>Name</span><input type="text" class="input-field" name="name" required data-errormessage-value-missing="Please enter your name." /></label>
<label for="email"><span>Email</span><input type="email" class="input-field" name="email" required data-errormessage-value-missing="Please enter your email address." /></label>
<label for="message"><span>Message</span><textarea name="message" class="textarea-field" required data-errormessage-value-missing="Please enter your message."></textarea></label>
<label><span> </span><div id="recaptcha"><div class="g-recaptcha" data-sitekey="6LcBawsTAAAAAKBPfGs1jApXNRLvR2MIPng0Fxol"></div></div></label>
<label><span> </span><input type="submit" value="" class="submit-button" /></label>
</form>
</div>
<?php
}
?>
I'm new to PHP, so I'm not sure if it's a syntax or semantics issue. Any help would be greatly appreciated!
Here's one way of doing it.
Check to see if the form has been submitted with if(isset($_POST['submit'])). You can also use if($_SERVER['REQUEST_METHOD'] == 'POST') to see if the form has been submitted.
Then we check if the email has been successfully sent, and if it has we set the $success_message variable.
We then check to see if the $success_message variable is set, and if it isn't, we show the form.
Also, note that I added name="submit" to the submit button element. This is how we're checking to see if the form has been submitted.
I also changed stripslashes() to strip_tags() to prevent any malicious code from getting through.
<?php
// Load reCAPTCHA library
include_once ("autoload.php");
if(isset($_POST['submit'])) {
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = trim(strip_tags($_POST['message']));
$emailFrom = $email;
$emailTo = "my#email.com";
$subject = "Contact Request";
// Prepare email body text
$body = "<strong>Name:</strong> $name <br /> <strong>Email:</strong> $email <br /> <strong>Message:</strong> $message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $name <$emailFrom>" . "\r\n";
$secret = 'XXX';
$lang = 'en';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'],$_SERVER['REMOTE_ADDR']);
// EDIT: repositioned recaptcha from OP's PasteBin script, as requested and adjusted messaging
// changed $success var to $message and added error message
// Original if statement, which redirected the user
if($resp->isSuccess()){
// send the email
if(mail($emailFrom, $subject, $body, $headers)) {
// set the success message
$success_message = 'The form was sent! Yay!';
} else {
// error message
$error_message = 'Could not send email';
}
} else {
$error_message = 'Prove you are a human!';
}
}
?>
<div>
<!-- quick and dirty way to print messages -->
<?php if(isset($success_message)) { echo $success_message; } ?>
<?php if(isset($error_message)) { echo $error_message; } ?>
</div>
<?php if(!isset($success_message)): ?>
<div class="contact-form">
<form role="form" method="post" action="index.php">
<label for="name"><span>Name</span><input type="text" class="input-field" name="name" required data-errormessage-value-missing="Please enter your name." /></label>
<label for="email"><span>Email</span><input type="email" class="input-field" name="email" required data-errormessage-value-missing="Please enter your email address." /></label>
<label for="message"><span>Message</span><textarea name="message" class="textarea-field" required data-errormessage-value-missing="Please enter your message."></textarea></label>
<div class="g-recaptcha" data-sitekey="6LcBawsTAAAAAKBPfGs1jApXNRLvR2MIPng0Fxol"></div>
<script type="text/javascript"
src="https://www.google.com/recaptcha/api.js?hl=<?php echo $lang; ?>">
</script>
<label><span> </span><input type="submit" name="submit" value="" class="submit-button" /></label>
</form>
</div>
<?php endif; ?>

$_POST doesnt work

I have a simple contact me script, which fires an email on submit.
The email is sent, but there are no form values in the email (name: empty, ...).
Why doesn't my contact script work?
<?php
if ($action == "send") //isset wyslij
{
if (!$_POST[name] || !$_POST[email] || !$_POST[phone] || !$_POST[enquiry])
{
$problem = TRUE;
echo("<p>You have to fill all form.</p>");
}
if (! $problem)
{
$data = date("d.m.y");
$message = "
<p>Name: $_POST[name]</p>
<p>Phone: $_POST[phone]</p>
<p>Email: $_POST[email]</p>
<br>
<p>Enquiry: $_POST[enquiry]</p>";
$od = "contactmail#asdasdas.com";
$content = $message;
$header = "From: $od \r\n";
$header .= 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
(mail('email#example.com', 'New message from website', $content, $header));
echo("<br><p>Message has been sent.</p>");
}
else
{
echo("<p>Try <a href=contact.php>again</a></p>");
}
}
?>
<form action="contact.php?action=send" method="post" enctype="text/plain">
<label for="name">Name</label><input type="text" name="name" /></br></br>
<label for="email">Email</label><input type="text" name="email" /></br></br>
<label for="phone">Phone</label><input type="text" name="phone" /></br></br>
<label for="enquiry">Enquiry</label><textarea name="enquiry" cols="20" rows="10"></textarea></br></br>
<input type="submit" id="contact_button" value="Send" />
</form>
Valid values for enctype in tag are:
application/x-www-form-urlencoded
multipart/form-data
you have text/plain and thats why it's not working.
simply remove the "enctype" argument from the HTML form code and try again?

Add input type=file element in my PHP

I have a form. With the following:
HTML:
<form name="feedback_form" method="post" action="" class="feedback_form">
<input type="text" name="field-name" value="İsim" title="İsim" class="field-name form_field">
<input type="text" name="field-email" value="Email" title="Email" class="field-email form_field">
<input type="text" name="field-subject" value="Başlık" title="Başlık" class="field-subject form_field">
<textarea name="field-message" cols="45" rows="5" title="Mesajınız..." class="field-message form_field">Mesajınız...</textarea>
<label for='uploaded_file'>Fotoğrafınızı Yükleyin:</label>
<input type="file" name="field-file" value="File">
<br>
<input type="reset" name="reset" id="reset2" value="Temizle" class="feedback_reset">
<input type="button" name="submit" class="feedback_go" id="submit2" value="Gönder">
</form>
PHP:
<?php
header('Content-Type: text/html; charset=utf-8');
function sendFeedback($feedback_email, $feedback_msg, $feedback_name, $feedback_subject, $feedback_file) {
/* EDIT THIS */
$admin_email = "mymail#gmail.com";
if ($feedback_subject == "Subject" || empty($feedback_subject) ) {
$subj = "Email from your site";
} else {
$subj = $feedback_subject;
}
/* //EDIT THIS */
$message = "
<html>
<head>
<title>Websitenizin emaili</title>
</head>
<body>
<p><a href='mailto:".$feedback_email."'>".$feedback_name."</a> send this message:</p>
<p>".$feedback_msg."</p>
<p>".$subject."</p>
</body>
</html>
";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
if ($feedback_name!=="Name" && $feedback_email!=="Email" && !empty($feedback_email) && !empty($feedback_msg) && !empty($feedback_name) ) {
if ($feedback_email == "mail_error") {
echo "<span class='ajaxok'>Geçersiz email adresi.</span>";
} else {
mail($admin_email, $subj, $message, $headers);
echo "<span class='ajaxok'>Teşekkürler. Mesajınız gönderilmiştir.</span>";
}
} else {
echo "<span class='ajaxalert'>Lütfen zorunlu alanları doldurunuz.</span>";
}
}
sendFeedback($_POST['email'], $_POST['message'], $_POST['name'], $_POST['subject'], $_POST['file']);
?>
When send a message on this form, send email, working. But only subject, message, name and email. I want add image upload in this php code. But i don't know how can i do it? Please help me.
In the form you have to put enctype="multipart/form-data".
In your php file you can access the file via $_FILES['file'];
Then take a look at this tutorial

Form for sending mail not sending

I have a "tell a friend" pop up email form that allows users to share my page with an email address that they enter. It pops up fine, but I can't get the form to send the email.
html:
<div id="tellfriend">
Close
<form id='tellafriend_form' method="post" action="#sendMessage" name="tellafriend_form">
<label for="name">Your Name:</label>
<input type="text" id="name" name="name" />
<label for="to">Friend's email:</label>
<input type="text" id="to" name="to" />
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject" />
<label for="message">Message:</label>
<textarea id="message" name="message"></textarea>
<input type="submit" name="submit" value="Submit">
</form>
</div><!-- #tellfriend -->
javascript that handles the "pop up":
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script>
$(function() {
$('#tellfriend').hide();
$('#sendMessage').click(function(e) {
$("#tellfriend").fadeToggle('fast');
});
});
</script>
php that's supposed to send the mail:
<?
if (isset($_POST['Submit'])) {
// This will check to see if the form has been submitted
$senders_name = $_POST['name'];
// The person who is submitting the form
$recipient_friend = $_POST['to'];
// The forms recipient
$subject = $_POST['subject'];
// The subject line
$message = $_POST['message'];
// The message being sent
mail($recipient_friend, "From $senders_name", $subject, $message);
if (isset($_POST['your_email'])) {
echo "<br>Your friend has been contacted <br><br>Thank you $senders_name";
}}
?>
Disclaimer: PHP newbie, hoping to learn. Thanks!
The order of your parameters in mail function is not correct. see this
it should be
mail($recipient_friend, $subject, $message);
if you want to use headers then do this
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: '.$recipient_friend.' <'.$recipient_friend.'>' . "\r\n";
$headers .= 'From: '.$sender.' <'.$senderEM.'>' . "\r\n";
Then call mail like this
mail($recipient_friend, $subject, $message, $headers);
You have one error in your PHP code:
if (isset($_POST['Submit'])) {
should be:
if (isset($_POST['submit'])) {
with a lowercase "s".
Indeed the name of you submit button is "submit" but the value is "Submit".
You could eventually do that:
if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') {
And your mail parameters are not correct like boug said.
You have 2 errors
first:
if (isset($_POST['submit']))
// $_POST is case sensitive
second:
if (isset($_POST['your_email']))
// you dont have an inout named 'your_email'

Categories