PHP Contact Form Submitting Randomly - php

I hope I'm missing something pretty basic here but: An empty form is getting submitted randomly, sometimes 3-8 times a day, then none for a few days and so on.
The empty submits always email with the subject as "[Website Contact Form]." Even though there is no validation in my php, in the html code the subject is chosen from a drop-down menu with the default as "General Enquiry." Notice in the php code below, there is no way for a human to submit an empty form with the above subject line, that is, it would always be "[Website Contact Form]General Enquiry" if I press submit without entering anything.
I have contact.html call this contact.php file:
<?
$email = 'info#mail.com';
$mailadd = $_POST['email'];
$headers = 'From: ' . $_POST['email'] . "\r\n";
$name = $_POST['name'];
$subject = '[Website Contact Form] ' . $_POST['subject'];
$message = 'Message sent from: ' . $name . '. Email: ' . $mailadd . '. Organization: ' . $_POST['company'] . '. Phone: ' . $_POST['phone'] . '. ';
$message .= 'Message: ';
$message .= $_POST['message'];
if (mail($email,$subject,$message, $headers)) {
echo "<p>Thank You! We'll get back to you shortly.</p>";
}
else {
echo "<p>Error...</p>";
}
?>
I use this code for many websites, but have never encountered this issue. Is there something so obviously wrong with this code that I'm missing? Any help would be greatly appreciated!

I suspect that you may not be checking that these variables are set before you send the email. Someone requesting contact.php directly (without any form data) may produce the results you have described. If this is the case, the following code should work like a charm:
<?php
if (isset($_POST['submit']) {
// form code
}
else {
// The form was not submitted, do nothing
}
?>
Even if that's not that case, such a simple check is always good practice.
Furthermore, you should always validate any user input just as a good habit. You don't want your server flooding your inbox with emails. I suggest using regexs to validate the input provided and possibly use a captcha service (such as ReCaptcha).

If you've been using this code and it's been working fine then I'd check what variables you changed with this case for example your submit form.
Try out your form with all common possibilities and see if it works. And empty Subject will give your form the subject "[Website Contact Form]". Check that your script actually get's the post variables and your form submits the right variables. Your dropdown might have an option with value of "" and the innerHTML "General Enquiry". The value is what will get submitted.
It's good to check inputs server-side as well
<?php
if(isset($_POST['subject'],$_POST['email'])){
}
?>

Related

Php email form not sending email from web email form

I am trying to troubleshoot this form. It is not sending reservation requests from the form on the website. Despite showing a message that the form was sent.
I tried editing email and the headers.
<?
//print_r($_POST);
$to = “email#emaildomain.com, {$posting['email']}";
function msg($text){
echo "
<script type='text/javascript'>
alert('".$text."');
top.location.href = 'http://www.aribbq.com';
</script>
";
exit;
}
function error($text){
echo "
<script type='text/javascript'>
alert('".$text."');
history.go(-1);
</script>
";
exit;
}
if (!$_POST[date]) {error('Please, insert Date.');}
if (!$_POST[time]) {error('Please, insert Time.');}
if (!$_POST[party]) {error('Please, insert Party.');}
if (!$_POST[reservation_name]) {error('Please, insert Name.');}
if (!$_POST[reservation_email]) {error('Please, insert Email.');}
if (!$_POST[reservation_phone]) {error('Please, insert Phone.');}
if(isset($_POST['submit'])){
// then send the form to your email
//$from = ('Reservation from AriBBQ.com'); // sender
$mailheaders = "From: contact#aribbq.com" . "\r\n"; // . "CC:
design#youremail.com"
$mailheaders .= 'Reply-To: ' . $posting['Email'] . "\r\n";
$subject = "AriBBQ.com Online Reservation";
$body = "\n Contact Name: ".$_POST[reservation_name]." \r\n\n";
//
$body .= " Email: ".$_POST[reservation_email]." \r\n\n"; //
$body .= " =================================================== \r\n\n"; //
$body .= " Book a table \r\n\n
Date: ".$_POST[date]." \r\n\n
Time: ".$_POST[time]." \r\n\n
Party: ".$_POST[party]." \r\n\n
Contact Details \r\n\n
Name: ".$_POST[reservation_name]." \r\n\n
Email: ".$_POST[reservation_email]." \r\n\n
Phone: ".$_POST[reservation_phone]." \r\n\n
Message: ".$_POST[reservation_message]." \r\n\n"; //
$body .= " =================================================== \r\n\n"; //
$result = mail($to , $from , $subject , $body , $mailheaders);
if($result) {msg('Thank you, your reservation has been sent. We
will send you a confirmation text or call in person.');} //
else{error('Sending mail is failed. Please try again');} //
} else {
error('No submitted. Please try again');
}
?>
You see the form online at http://aribbq.com/. Click on reservations. Once the email is received, we want to be able to reply to the sender's email address.
Alright, essentially, you need to turn on error reporting because your script threw about 20 errors at me which you would see with error reporting on. As my comment above said, add error_reporting(E_ALL); to the top of your script while you debug.
The issues I came across are as follows:
Parse error: syntax error, unexpected '#' in /mail.php on line 4 caused by an incorrect double quote character, not " but “. Subtle, but problematic.
Next up, Multiple or malformed newlines found in additional_header in /mail.php because as of PHP 5.5.2, a bug was fixed to prevent mail header injection, so all of your \n\n within the $mailheaders should be removed, I recommend appending PHP_EOL to the end of each line instead.
You have your $from variable included in the mail() call, this presents 2 issues. One, the mail() function does not have a from parameter, you include it within the headers. Two - your variable is actually commented out.
As I mentioned in the comment above, again, your email address variable to send to is typed as $posting['email']', and $posting['Email'] within $mailheaders. The problem here is $posting doesn't exist. Secondly, your form, which you should include the HTML for in future questions for self-contained examples for people to more easily help you (see https://stackoverflow.com/help/how-to-ask), doesn't post email at all, it posts reservation_email.
Finally, the majority of your $_POST references do not include quotes so PHP doesn't know what to do with the words in between the square brackets. $_POST[date] should be $_POST['date'], for example.
I've made all the above changes and managed to successfully email myself with the script and email form provided, the only thing that I didn't look at was your msg() which didn't show me a success message. I did, however, put an echo statement before this function call which printed out fine.
I hope this helps you get your script up and running, good luck and remember, error_reporting(); is your friend!

How to make form fields required? [duplicate]

This question already has answers here:
Making email field required in php [closed]
(4 answers)
Closed 8 years ago.
I have this existing code and I am wondering how to make the name and email field required?
<?php
if(isset($_POST['submit'])){
$to = "xxx#email.com"; // this is your Email address
$from = $_POST['gift_email']; // this is the sender's Email address
$first_name = $_POST['gift_name'];
$subject = "Free Gift Request";
$msg = "A free gift has been requested from the following:"."\n";
$msg .= "Name: ".$_POST["gift_name"]."\n";
$msg .= "E-Mail: ".$_POST["gift_email"];
$headers = "From:" . $from;
mail($to,$subject,$msg,$headers);
//echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
header('Location:free_program_thankyou.php');
}
?>
For form
<input type="text" name="gift_email" required>
<input type="text" name="gift_name" required>
For Php
if(empty($_POST['gift_email']))
{
echo 'This field is required';
}else {
//Do what you want to do here
}
A two basic ways to do this:-
Within the php program check each required form field has been filled in send a new page with an error message back if it is not. Be sure to return the contents of any fields already filled in or your users will wish a plague of boils on your person.
Validate in javascript. Have a function triggered by the "onsubmit" condition which checks for all required forms fields are filled and highlights any that are not. see here
In practice a robust web site will do both. This seems like duplication however the javascript function is much more responsive and user friendly, BUT, the php server side validation cannot be gamed by turning JS off or spoofing responses.

How to post a collected value to a new page

I am still learning PHP and am now completely stuck - any help would be so much appreciated! Scenario: HTML form for user to complete, among other things, they have to select with a radio button how many tickets they want to buy. My PHP file compiles all the values into an email and sends it off - that part works perfectly - and then redirects the browser to my "thank you for completing the form" page. I would now like to display a value collected in the form on this thank you page: the amount of tickets the user selected with the radio button. How do i call the 'tickets' value to the thank you page?
Thank you ever so much!
Here is the HTML form:
http://menusolutions.co.za/maidens2014_booking_form.html
Here is my "sendmail3.php" file that sends my mail:
$webmaster_email = "carin#menusolutions.co.za";
$feedback_page = "maidens2014.html";
$error_page = "maidens_error_message.html";
$thankyou_page = "maidens_thank_you.php";
$name = $_POST['name'] ;
$telephone = $_POST['telephone'] ;
$cell = $_POST['cell'] ;
$email_address = $_POST['email_address'] ;
$address = $_POST['address'] ;
$tickets = $_POST['tickets'] ;
$mail_body = "Name: $name \n Telephone: $telephone \n Cell: $cell \n Email: email_address \n Address: $address \n Tickets: $tickets";
mail( "$webmaster_email", "Maidens Bowled Over 2014",
$mail_body, "From: $email_address" );
header( "Location: $thankyou_page" );
}
?>
And here is my thank you page, upon which I need to display the 'tickets' value so that people can be reminded how many tickets they bought and the amount they need to pay:
http://menusolutions.co.za/maidens_thank_you.php
Your help will be greatly appreciated! Thank you!
You can send ticket value inside URL or using session.
header( "Location:". $thankyou_page.'?ticket='.$tickets );
and on thanks you page add below code,
$tickets = $_GET['tickets'];
echo $tickets . ' tickets.';
The easiest way would be to redirect to the "thank you"-page and add the ticket value as a get-parameter
Link
maidens_thank_you.php?tickets=$tickets
You could use a GET parameter for this. For example,
header('Location: ' . $thankyou_page . '?tickets=' . $tickets);
And then in your thank you page (which I assume is a PHP script), you could retrieve that value using.
$tickets = $_GET['tickets'];
echo 'Thank you! You bought ' . $tickets . ' tickets.';
You can set a session or pass the values in the url by making your $thankyou page something like
$thankyoupage + "?numberoftickets=" + $numberoftickets;
header('Location:' + $thankyoupage);
and get the value back by
$_GET['numberoftickets']
in your actual thankyoupage.
only use the session method if you need the var in other pages than the thankyoupage too. sessions make this value available in your hole application.
more information about sessions can be found here: http://www.w3schools.com/php/php_sessions.asp
You can use sessions to accomplish that.
An other option is redirecting and using a GET value.
On your sendmail.php page you can use header to redirect the user :
header('location: thanks_page.php?ticketValue='.$ticketValue);
Be careful that : header() must be called before any actual output is sent. (see doc.)
And then get it back in your thanks_page in $_GET['ticketValue'] and do not forget to espace the value with htmlspecialchars or an equivalent for security !
Never use a varible in double quotes ("") it's will be string so use direct. you need to more learn php
for mail tuts :- http://php.net/manual/en/function.mail.php
mail( $webmaster_email, "Maidens Bowled Over 2014",
$mail_body, $email_address );
header( "Location:". $thankyou_page );

swiftmailer contact form required fields

I have built a contact form on a website which is handled by swiftmailer. At the moment it sends correctly with an image attachment and some input fields. How do i make some of the fields "required" and output an error message on those if left empty? Is this something that needs to happen before the swiftmailer library comes into it?
Apologies if this is simple stuff but im new to PHP and cant find a quick answer to this anywhere
<?php
$_SESSION["post"] = $_POST;
$name = $_POST["Name"]; $email = $_POST["Email"]; $phone = $_POST["Phone"]; $dob = $_POST['DOBDay'] ."\t" .$_POST['DOBMonth'] ."\t" .$_POST['DOBYear'];$address = $_POST['AddressLine1'] ."\n" .$_POST['AddressLine2'] ."\n" .$_POST['PostCode'];$experience = $_POST["Experience"];$height = $_POST["Height"]; $size = $_POST["DressSize"];$bra = $_POST["Bra"];$waist = $_POST["Waist"];$hipwidest = $_POST["HipWidest"];$bicep = $_POST["Bicep"];$thigh = $_POST["Thigh"];$shoe = $_POST["Shoe"];
require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('xxx#gmail.com')
->setPassword('xxx');
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Be A Model application: Girls')
// Set the From address with an associative array
->setFrom(array($email => $name))
// Set the To addresses with an associative array
->setTo(array('xxx#xxx.com', 'xxx#xxx.com' => 'contact test'))
// Give it a body
->setBody('Name: ' .$name ."\n"
.'Email: ' .$email ."\n"
.'Phone: ' .$phone ."\n"
.'Address: ' .$address ."\n"
.'DOB: ' .$dob ."\n"
.'Experience: ' .$experience ."\n"
.'Height: ' .$height ."\n"
.'Dress Size: ' .$size ."\n"
.'Bra: ' .$bra ."\n"
.'Waist: ' .$waist ."\n"
.'Hip at Widest: ' .$hipwidest ."\n"
.'Bicep: ' .$bicep ."\n"
.'Thigh: ' .$thigh ."\n"
.'Shoe Size: ' .$shoe ."\n" );
// And optionally an alternative body
//->addPart('<q>Here is the message itself</q>', 'text/html');
// Attachment
$message->attach(
Swift_Attachment::fromPath($_FILES['fileatt']['tmp_name'])->setFilename($_FILES['fileatt']['name'])
);
// Send the message
$result = $mailer->send($message);
if ($result)
{
header('Location: http://www.modelmeasures.co.uk/thankyou.html');
}
echo $result;
?>
There are two types of data validation for websites, Client Side and Server Side.
Client Side Validation - This type of validation is typically done using javascript, often as you complete the form. You've seen this on sites that show a 'X', or turn the field colour red, beside an invalid form field BEFORE you even submit it.
Client side validation is useful because it lets the user know that there is a problem before they even submit the form, giving them a chance to correct it.
Server Side Validation - This is where you check the form values when received by the server to make sure it is in the format you expect, doesn't contain invalid information, etc. You see this validation in use when you complete a form, submit it, and the page reloads and tells you there are errors.
You should be doing this type of validation regardless if you validate on the client side or not. It is easy to disable javascript, and if you are only using client side validation people could enter anything they want. This is a security risk.
What I usually do is set my pages up, and use server side validation. This ensures that there are no security issues, and that I am checking the data the user enters. Once that is working I also add client side javascript validation, to make the form more user-friendly. Doing it this way the javascript validation makes sure that the user is entering the correct information, but if something goes wrong, or javascript is disabled, my server validates the data anyways.
So, to answer your question, you really should be doing server side validation at the very least. It would be important to have the validation occur before Swiftmailer actually sends the email, so that emails are not send if invalid data has been entered.

How is this contact us script vulnerable / being manipulated?

A client recently got a spam warning from their host.
I think I have pin pointed the issue to an old contact us form. Simple html on the front end and a simple PHP script on the back end.
if ($_POST['submit'] == "Send"){
//START SEND MAIL SCRIPT
$mail = $_POST['email'];
$to = "me#gmail.com";
$subject = "Message from Website Contact Us Form";
$headers = "From: Contact us Form <webmaster#website.co.uk>";
$message = "Message from Contact Us Form\n\n";
$message .= "\nName: " . $_POST['contactname'];
$message .= "\nEmail: " . $_POST['contactemail'];
$message .= "\nTelephone: " . $_POST['contactphone'];
$message .= "\n\n\nMessage:\n" . $_POST['contactmessage'];
if(mail($to,$subject,$message,$headers)) {
header('Location: http://www.website.co.uk/contact-us/?action=success');
}else{
header('Location: http://www.webisite.co.uk/contact-us/?action=fail');
}//END IF MAIL
}//END SCRIPT
I know the remedies to fix it such as sanitizing post vars properly, using captchas, using a hidden 'honeypot' blank field, js tricks etc etc (I also like the look of this script too http://www.alt-php-faq.com/local/115/)
But to help me understand what was going on I want to know how this script is being manipulated. A foreign script posting vars to it but how do they send email to anyone apart from
'me#gmail.com' or if they are forcing cc / bcc fields somehow why do I not get all spam as well??
Thanks
Line like this $message .= "\nName: " . $_POST['contactname']; can be dangerous.
If $_POST['contactname']='MegaSteve4 \r\nCc: email1#mail.com, email2#mail.com'; are set, 2 uses will get spam mail.
See carefully. Its appending more headers. In this case Cc. I am not sure if Cc is a raw email header. But I hope you get the idea.
You're not doing any escaping of the post data. That means that this form is vulnerable to injection attacks.
I couldn't tell you how they did it, but that's probably what happened.

Categories