PHP Form Submission Sending Blank Emails - php

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?

Related

Newbie Needs To Know How To Fix A Broken PHP Email Form

I thought it would be easy (silly, I know) and so I followed the instructions I found at this site for a PHP email form: http://www.html-form-guide.com/email-form/dreamweaver-email-form.html I didn't need help with the Dreamweaver interface at all, I just needed the script, and I knew (or thought I knew) how to adapt their simple form into the form I needed, and adapt the script accordingly.
The form in question may be found at this URL: http://nineinchbride.com/SuitedForWar_BookTwo_PreOrderForm.php
The code as it presently exists on the page is as follows:
<form id="PreOrder_Book_2" name="PreOrder_Book_2" method="post" action="">
<p>Your Name:
<input type="text" name="CustomerName" id="CustomerName" />
</p>
<p>Your Email:
<input type="text" name="CustomerEmail" id="CustomerEmail" />
</p>
<p>
<input type="checkbox" name="NotifyPaperback" id="NotifyPaperback" />
Notify me when paperback is available.</p>
<p>
<input type="checkbox" name="Notify_eBook" id="Notify_eBook" />
Notify me when eBook is available.</p>
<p>Desired eBook Format:</p>
<p>
<label>
<input type="radio" name="eBookFormats" value=".mobi" id="eBookFormats_0" />
.mobi (Kindle)</label>
<br />
<label>
<input type="radio" name="eBookFormats" value=".epub" id="eBookFormats_1" />
.epub (Nook / Ipad / Sony / Kobo)</label>
<br />
<label>
<input type="radio" name="eBookFormats" value=".pdf" id="eBookFormats_2" />
.pdf (All readers)</label>
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
<br />
</p>
</form><script>
function validateForm()
{
var name=document.forms["PreOrder_Book_2"]["CustomerName"].value;
if (name==null || name=="")
{
alert("Name cannot be left blank.");
return false;
}
var z=document.forms["PreOrder_Book_2"]["CustomerEmail"].value;
if (z==null || z=="")
{
alert("Please enter your email.");
return false;
}
}
</script>
<script><?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['CustomerName'];
$visitor_email = $_POST['CustomerEmail'];
$message = $_POST['NotifyPaperback'];
$message = $_POST['Notify_eBook'];
$message = $_POST['eBookFormats'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
$email_from = 'webmanager#nineinchbride.com';//<== Put your email address here
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"email address: $visitor_email\n".
"Here is the message:\n $message".
$to = "webmanager#nineinchbride.com";//<== Put your email address here
$headers = "From: $email_from \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: http://nineinchbride.com');
?></script>
Please bear in mind that while I know my way around code a bit (enough to adapt things a little, adjust naming for consistency and the like), I am not a programmer per se, so please go easy on me.
Update 1:
Okay, I'm making progress here. I made the following changes to the PHP:
$name = $_POST['CustomerName'];
$visitor_email = $_POST['CustomerEmail'];
$message1 = $_POST['NotifyPaperback'];
$message2 = $_POST['Notify_eBook'];
$message3 = $_POST['eBookFormats'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
$email_from = 'webmanager#nineinchbride.com';//<== Put your email address here
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"email address: $visitor_email\n".
"Notify When Paperback Is Available: $message1\n".
"Notify When eBook Is Available: $message2\n".
"My eBook Format Is: $message3\n".
And, hurray, I'm getting all the form data. Figured it out for myself too ;-)
However, none of the validation is working. And the re-direct after successful submission is not working either. Any idea what's up with that?
Update 2:
Wow, validation problem solved, thank you Poria! I just added
<input type="button" name="button" id="button" value="Submit" onclick="return validateForm();"/>
to the form itself in place of the submit button I had before, and now the front end validation is working. Great!
But now the form itself does not submit any longer! What did I do wrong?
Your first mistake is
$message = $_POST['NotifyPaperback'];
$message = $_POST['Notify_eBook'];
$message = $_POST['eBookFormats'];
change it to
$message = $_POST['NotifyPaperback'];
$message .= $_POST['Notify_eBook'];
$message .= $_POST['eBookFormats'];
not the dot(.) for concatenation.
Secondly you never called the validation function
change the button like this
<input type="button" name="submit" id="button" value="Submit" onclick="return validateForm();"/>
Now form will be submit.
Hope it helps!
For further questions please post another question.

I am trying to send email from a FORM using PHP, but i receive the details without the From email Address

I am trying to send email from a FORM, but i receive the details without the FROM email Address, help to rectify the issue.
/* HTML FORM CODE */
<form action="contact.php" method="post">
Name:<br><input type="text" name="name"><br>
From:<br><input type="text" name"from"><br>
Request:<br><textarea type="text" name="request"></textarea><br>
<input type="submit" name="submit" value="send">
</form>
/* HTML FORM CODE */
/* PHP SEND EMAIL CODE */
<?php
$name=$_POST['name'];
$from=$_POST['from'];
$request=$_POST['request'];
$to="skks1981#gmail.com";
$subject=$request;
$body=<<<EMAIL
Hai My name is $name
My email is $from
$request
EMAIL;
$header:$from;
mail($to,$subject,$body,$header);
echo "Message Sent";
?>
/* PHP SEND EMAIL CODE */
Note: In From part i receive unknown or admin#khatamband.com email address.
Thank in advance.
You should use this $header:
$header = "From: {$from}";
and not
$header:$from;

Forms - PHP within HTML page, data not getting emailed

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

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