Forms - PHP within HTML page, data not getting emailed - php

I am using PHP to send data to an email address from a HTML form. It worked fine while the PHP file was a pure PHP file, displaying the confirmation text upon submitting the form. However, I needed the confirmation text to appear within our usual templates so I added the same PHP into the body of a page and set the form action to go to that page. When someone now submits the form, an email does get sent but it contains none of the information from the form. Can you help?
HTML:
<form method="post" action="thank-you-page.html">
Email: <input name="email" type="text"><br />
Name: <input name="name" type="text"><br />
<h3>Your message</h3>
Subject: <input name="subject" type="text"><br />
Message:<br /> <textarea name="message" rows="15" cols="40"></textarea><br />
<input type="submit" />
</form>
PHP within body of thank-you-page.html:
<?php
$to = "myemail#email.com";
$subject = 'Feedback from online form';
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print 'Your mail was sent successfully. Thank you for your feedback.'; }
else
{print 'We encountered an error sending your mail.'; }
?>
Thank you!

Your thank you page needs to be a PHP page, not just an HTML page.
Change it to be thank-you-page.php

Related

How to fix this php file for sending email

i have created this php form for enquiry purpose but when i clicked on the submit button it's not working. can you tell me what have i done wrong in this code.
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type ="text" name="subject" placeholder="subject">
<input type ="email" name="email" placeholder="email">
<textarea rows="5" name="message" cols="30"></textarea>
<input class="btn" type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
$to = "rizwandesigns19#gmail.com";
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = $_POST['email'];
$headers = "From : $from";
mail($to, $subject, $message, $headers);
}
echo "Mail Sent";
?>
or can you give me valid php script for this purpose.
To get through google's and other's spam filters you need an address which the mail is sent from. For this you could create a new Gmail-address and use the phpmailer-library.
Edit: Setup with gmail is actually pretty complicated and it will disable your access once in a while which renders it almost unuseable. I started to do it by installing a real mail server on the host machine (Install a Complete Mail Server with Postfix and Webmail in Debian 9) and use the php mail() function (phpmailer can still be used tho).

PHP Form Submission Sending Blank Emails

After manually submitting a form on my website, an email with the subject appears in my inbox. The email's subject is correctly sent as "Feedback Form Submission", but the email itself is blank. There is no body nor input from the email box on my form.
Form:
<form action="feedback-form.php" method="post" enctype="text/plain">
E-mail:<br>
<input type="text" class="textForm" name="email_address" size="35"><br>
Comment:<br>
<textarea name="feedback" class="textForm" rows="6" cols="35"></textarea><br><br>
<input type="submit" id="submit" class="button" value="Send">
</form>
PHP:
<?php
#Receive user input
$email_address = $_POST['email_address'];
$feedback = $_POST['feedback'];
#Filter user input for invalid characters
function filter_email_header($form_field)
{
return preg_replace('/[nr|!/<>^$%*&]+/', '', $form_field);
}
$email_address = filter_email_header($email_address);
#Send email
$headers = "From: $email_address";
$sent = mail('me#website.com', 'Feedback Form Submission', $feedback, $headers);
I believe my mail function is set up properly. My variables in the form match the variables my PHP script is using. My PHP and HTML are in the same file. I have read in other questions that having the two pieces of code in the same file can lead to issues when a blank form is submitted, but I am having issues while inputting valid information into my form. What am I doing incorrectly here?

Javascript Form Submit with Email Action?

I need make this form send me a email like a contact form:
Script code:
<script type="text/javascript">
$(document).ready(function(){
$("#contactLink").click(function(){
if ($("#contactForm").is(":hidden")){
$("#contactForm").slideDown("slow");
}
else{
$("#contactForm").slideUp("slow");
}
});
});
function closeForm(){
$("#messageSent").show("slow");
setTimeout('$("#messageSent").hide();$("#contactForm").slideUp("slow")', 2000);
}
</script>
HTML CODE:
<div class="box">
<div id="contactFormContainer">
<div id="contactForm">
<fieldset>
<label for="Name">Nome: </label>
<input id="name" type="text" />
<label for="Telefone">Telefone Fixo: </label>
<input type="text" id="phone" maxlength="15" onkeypress="Mascara(this);" />
<label for="Message">Assunto:</label>
<textarea id="Message" rows="3" cols="20"></textarea>
<input id="sendMail" type="submit" name="submit" onclick="closeForm()" />
<span id="messageSent">Sua solicitação foi enviada com sucesso, por favor, aguarde...</span>
</fieldset>
</div>
<div id="contactLink"></div>
</div>
When click and close the form i need send me a email with the content of form, how to?
Some idea? thanks!
Firstly i can't see the form tags in your code. According to me you're doing this wrong and i'm sure many of our friends on stack will agree too.
Your question suggests that you basically want to receive an email with the data submitted through the form. Why don't you try the below method.
HTML
<form action="mail.php" method="POST">
<input type="text" name="fname"></input>
<input type="text" name="lname"></input>
<button>SUBMIT</button>
</form>
PHP
<?php
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$to = "someone#example.com";
$subject = "Hello World";
$message = "Firstname: $firstname \n\n Lastname: $lastname";
$from = "sender#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
The above example is the most simplest method of sending an email. You can go advance by adding more header information and graphically formatting the email.
Go through these tutorials if you get confused.
http://www.w3schools.com/php/php_mail.asp
http://www.phpeasystep.com/phptu/8.html
And since you mentioned that you want to perform the task via javascript you can try submitting the form via ajax, refer the below tutorials
http://teachingyou.net/php/simple-php-contact-form-using-ajax/
http://www.sitepoint.com/forums/showthread.php?1055068-Send-PHP-email-using-jQuery-AJAX
Since you've tagged the question php, have a look at php's mail function. http://php.net/manual/en/function.mail.php
$to = 'you#domain.com';
$subject = 'Contact Form';
$message = '...' //concatenate the $_POST (or $_GET) variables to create this message
mail($to, $subject, wordwrap($message, 70, "\r\n");
This function requires that your server has a properly configured to send mail - see the php documentation for requirements: http://www.php.net/manual/en/mail.requirements.php

Source URL embedded in message for a contact form

I've got a very simple PHP contact form, containing Email and Message.
I would like to add a functionality so that every time the contact form is sent I know the URL that it is being sent from, and I'd like to include it into the body of the message that gets sent to my email.
Here's the PHP code that runs the Contact form.
<?php
$to = "email#email.com" ;
$from = "Something Broke!" ;
$subject = "Something Broke!";
$fields = array();
$fields{"emailOptional"} = "Email:";
$fields{"message"} = "Message:";
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%s: %s\n",$b,$_REQUEST[$a]); }
if(mail($to, $subject, $body)){
echo 'sent';// we are sending this text to the ajax request telling it that the mail is sent..
}else{
echo 'failed';// ... or to tell it that it wasn't sent
}
?>
And here's the markup:
<form method="post" action="widgetScript.php" id="contactForm">
<input type="text" name="emailOptional" placeholder="Your Email (optional)" />
<textarea rows="5" type="text" name="message" id="message"></textarea><br />
<input type="submit" name="send" id="Submit" value="Send">
</form>
I've found that you can use [_post_url] to get the current URL if I understand correctly - but I'm unsure what to do with it. Would appreciate all the help I could get
In your php code, use $ _SERVER ['HTTP_REFERER'] to get the url the form was submitted from.
For more information on $_SERVER:
http://php.net/manual/en/reserved.variables.server.php

code won't populate email

created a simple little php code to populate email with email address and info from a textbox on the form. it originally worked when I was calling to the script from a html form, but once I converted my site to PHP it stopped working. Eventually I would like to put this same info into a Database table but right now I would be content just getting the email to work.
When the form is submitted I get the email address from the customer, but I don't get the info from the textbox. here is my code.
<?php
$email = $_POST['email'];
$message = $_POST['message'];
mail( "sales#sixtoed-design.com", "Service Request", "From: $email",
$message );
header( "Location: http://www.sixtoed-design.com/thankyou.php" );
?>
Like I said it is a very simple code and worked fine before I converted my site completely to PHP.
Below is my code for the form if you need it.
<form method="POST" action="sendmail.php" enctype="multipart/form-data">
Email: <input name="email" type="text" /><br />
Message:<br />
<textarea name="message" rows="15" cols="40">
</textarea><br />
<input name="" type="submit" value="Send Email">
</form>
You're using the mail function wrong. See the docs.
The 3rd parameter should be the message and you send from as a header in the 4th parameter, so:
mail( "sales#sixtoed-design.com", "Service Request", $message, "From: $email" );
See example 2 in the docs

Categories