Unexpected $end in PHP file - php

I'm working on a PHP contact form, but I can't get it to work. I get the following error in the Apache server log, running on an Ubuntu Server VM:
PHP Parse error: syntax error, unexpected $end in /home/matthew/Sites/contactFormResponse.php on line 75, referer: http://192.168.1.4/contactForm.php
From googling this error, it sounds like it's normally caused by either using the short PHP tag when the server's not set up to recognise them, or by having a block of code that isn't closed correctly. But as far as I can see that isn't the case here - as far as I can see it's all closed correctly. The line it refers to is one line past the end of the file.
Here's the PHP code:
<?php
error_reporting(E_ALL);
// Define variables to hold the name, email address and message, and import the information into the variables
$name = $_POST['NameInput'];
$email = $_POST['EmailAddress'];
$telno = $_POST['ContactNumber'];
$querytype = $_POST['QueryType'];
$bookingstartdate = $_POST['BookingStartDay'] . $_POST['BookingStartMonth'] . $_POST['BookingStartYear'];
$bookingenddate = $_POST['BookingEndDay'] . $_POST['BookingEndMonth'] . $_POST['BookingEndYear'];
$message = $_POST['QueryText'];
// Validate the inputs - send it if it's OK
if(3 < strlen($name) && 3 < strlen($email))
{
$email_message = <<< EMAIL
Message from contact form at holidaychalet.co.uk
Name: $name
Email: $email
Contact Number: $telno
Query Type: $querytype
Booking Start Date: $bookingstartdate
Booking End Date: $bookingenddate
The message:
$message
EMAIL;
$headers = "cc:me#myemailaddress.com\r\n";
if(mail('matthew#localhost','Contact form email', $email_message, $headers))
{
echo "Thanks for completing the form! I'll be in touch shortly!";
}
else
{
echo "Something went wrong - please use the back button and try again";
}
}
else
{
echo "You didn't complete the form fully enough! Please use go back using your web browser's back button";
}
?>

The closing identifier for the here document syntax must be at the start of the line without any indentation:
It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system.
So in your case:
$email_message = <<< EMAIL
Message from contact form at holidaychalet.co.uk
Name: $name
Email: $email
Contact Number: $telno
Query Type: $querytype
Booking Start Date: $bookingstartdate
Booking End Date: $bookingenddate
The message:
$message
EMAIL;

EMAIL;
cannot be indented. Heredoc syntax requires that the closing identifier be at the start of the line, and that includes no leading whitespace.

You are filling $email_message with a string which is marked to end with EMAIL;
This one must be in a single line.
Change it to:
$email_message = <<< EMAIL
Message from contact form at holidaychalet.co.uk
Name: $name
Email: $email
Contact Number: $telno
Query Type: $querytype
Booking Start Date: $bookingstartdate
Booking End Date: $bookingenddate
The message:
$message
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!

HEREDOC text and variables formating issue

I created a PHP script that collects variables from HTML and converts them to PHP variables. Then takes those variables and inserts them into a HEREDOC string and finally sends an email to a predefined person. I'm having an issue getting the text to format with a carriage return after each variable. So that all the text in the email is left side formatted.
What I am getting is this:
SFARC Membership Application Date: February 19th. 2019 First Name: XXXXXX Last Name: XXXXXXX Nick Name: XXXXXXX
Here is portion of my code that handles the text string:
// Generate application in a message
$message = <<<EOT
SFARC Membership Application
Date: $App_Date
First Name: $First_Name
Last Name: $Last_Name
Nick Name: $Nick_Name
Address: $Address
City/Town: $City_town
Zip Code: $Zip_code
Email: $Email
Home Phone: $Home_phone
Cell Phone: $Cel_phone
Callsign: $Call_sign
ARRL Member: $Arrl_member
Membership Type: $Membership_type
Membership Term: $Membership_term year(s)
Payment Method: $Payment_method
Membership Dues: $Membership_dues
EOT;
// Sending email
if(mail($to, $subject, $message, $headers )){
echo 'Your membership application has been successfully submitted.';
} else{
echo 'Unable to submit your membership application. Please try
again.';
};
?>
Godaddy is who is hosting my website. Is that the issue? I watched several youtube videos and I have no idea what I am missing? Is there a better way to code to achieve the results I am looking for?
Thanks in advance.
I think your message is send by HTML reason. You can confirm it by content type in the header of email. Or add $message = nl2br($message); before send and try again.
You probably tried this, but I want to double check: did you try putting \n or \r at the end of lines?

Bootstrap JQuery posting wrong data elements to PHP mail

I am writing a wedding website with a basic form to get the guest email address, guest name(s), guest dinner option, the number of children they are bringing, and a requested song they would like played. I then want to take these values and send them via PHP mail function to an email address. Here is what I have so far:
Here is the snippet of my javascript. The variables emailAddr, g1_name, and g1_dinner all have the correct values in them when I log it to the console.
var data = {
email : emailAddr,
g1Name : g1_name,
g1Dinner : g1_dinner
};
$.post("email.php", data, function(){
alert("Email Success");
});
Here is the PHP:
<?php
$to = "test#email.com";
$from = $_POST['email'];
$guestName_1 = $_POST['g1Name'];
$guestDinner_1 = $_POST['g1Dinner'];
$message = "Email: "+$from +"\n\nGuest 1: "+$guestName_1+" - "+$guestDinner_1;
$message = wordwrap($message, 70);
mail($to,"Wedding RSVP",$message);
?>
Here is what is actually getting POST'd:
And here is the email I get from my web host:
So my main question is why is what is getting POST to my PHP file different than I am specifying?
And a follow up, why am I getting emailed a 0 and not the string I set?
And is this the right way to do something like this?
Any help is appreciated! Thanks!
PHP 101: String concatenation uses ., you're using +, which is mathematical addition.
'string' + 'string' => generally "0" or wonky integer as result
'string' . 'string' => 'stringstring'

my variable is not echoing <br> tag when it gets to the mail function

I am creating my own contact form and sending all the inputs through variable to the mail function
But there is one error in it......my code is like this in the below...
$from = $_POST["from"];
// sender
$name = $_POST["name"];
$subject = $_POST["subject"];
$message = $_POST["message"];
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
$phone = $_POST["phone"];
$mytour = $_POST["select1"];
$howmany = $_POST["select2"];
$arrival = $_POST["arrival"];
$departure = $_POST["departure"];
$accommodation = $_POST["select3"];
$company = $_POST["company"];
// send mail
$messagecontent = "Name:$name , " . "Email:$from ," . "Nature of Enquiry:$subject ," . "Message:$message ," . "Phone No:$phone, " . "My Tour Wish List: $mytour, " . "How many days do you have available:$howmany, " . "Arrival Date:$arrival ," . "Departure Date:$departure ," . "My Preferred Accommodation:$accommodation, " . "Company Name:$company ,";
// $messagewithbreak=str_replace(',',', <br/>;',$messagecontent); // code replaces all your commas with , and a <br> tag
mail("abc#gmail.com", " Contact form Filled", "$messagewithbreak", "From: $from\n");
echo "Thank you for sending us feedback";
}
But when I receive it in the my email...there is not break tag <br> tag...and it receives as....
Name: , Email:abc#gmail.com ,Nature of Enquiry:none ,Message:none ,Phone No:none,My Tour Wish List: Heritage Tour of Kells,How many days do you have available:Half a Day, Arrival Date:2014-09-10 ,Departure Date:2014-09-10 ,My Preferred Accommodation:Hostel,Company Name:none
But I want it to view proper in the email.... as that is not the professional way... to see the email moreover it is difficult to read the email like this.....
what should I do please help me on it....
I will really appreciate your help.
Please answer my question nothing is helping me out here.I have tried..the methods given to me by jenz and rakesh but still the form when receive in the email shows as the paragraph without line breaks....it is getting frustrating
why you concat message string again and again. Also remove double quotes from variables in mail()
$messagecontent="Name:$name , Email:$from , Nature of Enquiry:$subject , Message:$message , Phone No:$phone, My Tour Wish List: $mytour, How many days do you have available:$howmany, Arrival Date:$arrival , Departure Date:$departure , My Preferred Accommodation:$accommodation, Company Name:$company ,";
$messagewithbreak=str_replace(',',',<br>',$messagecontent);
mail("abc#gmail.com"," Contact form Filled", $messagewithbreak, $from);
Properly add formatting to your message. Any HTML tags placed inside double quotes will be converted to the tag when you echo it. So you just need to add <br> in proper places inside the message content. Also all PHP variables inside double quotes will get replaced by its value. So no need of appending each string with double quotes.
$messagecontent="Name:$name <br> Email:$from <br>Nature of Enquiry:$subject
<br>Message:$message<br>Phone No:$phone<br>
My Tour Wish List: $mytour<br>How many days do you have available:$howmany<br> Arrival Date:$arrival <br> Departure Date:$departure <br> My Preferred
Accommodation:$accommodation<br>Company Name:$company ";
To send HTML email you have to add headers which is optional by default where we pass Content-Type type declaration telling mail services that parse this email as HTML.
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
and pass $headers in mail() like,
mail("abc#gmail.com", " Contact form Filled", $messagecontent, $headers);
You are trying to use HTML in a plain text email. If all you want to do is have line breaks, then use \n instead of the br tag.
If you want to use HTML in an email you must add a content-type header. See the PHP mail docs for an example.
// send mail
$messagecontent = "Name:$name \n ";
$messagecontent.= "Email:$from \n";
$messagecontent.= "Nature of Enquiry:$subject \n";
............
...............
Set all the variables like that.

Assigning a variable a value from an recordset

I apologize but I'm very new to PHP and I am trying to create a very simple form that sends an email back to a user when they enter in their email address. I want the message to include some data from our database. I have been able to create a form that works perfectly as long as I enter in the message manually (like $message = "Hi. How you doing?") but I can't seem to figure out how to incorporate the recordset data. What I was hoping was to use something like...
<?php
$to = $_REQUEST['Email'] ;
$message = '<?php echo $row_rsPersonUser['bio']; ?>'; <<<<<<<<Line 63
$fields = array();
$fields{"Email"} = "Email";
$headers = "From: noreply#domain.ca";
$subject = "Thank you";
mail($to, $subject, $message, $headers);
?>
What I get from this is "Parse error: syntax error, unexpected T_STRING in.... on line 63". I know it's formatted wrong but I don't have a clue why. When I drop the into the body, the info I want does display on the webpage so I know that part is working. Any help would be welcomed.
Thanks
You don't have to use PHP start and end tags inside PHP code itself
$message = '<?php echo $row_rsPersonUser['bio']; ?>'; // this is wrong
^^^^^ ^^
Should be
$message = $row_rsPersonUser['bio'];
Just change the 63rd line like below..
you can't start one <?php block in another <?php block
$message = $row_rsPersonUser['bio'];
If you were to do that it would just print <?php echo… as literals since you can't send php code as a email only html/plan text
$message = '<?php echo $row_rsPersonUser['bio']; ?>';
should be:
$message = $row_rsPersonUser['bio'];
and (I tested the following and it appears the {}'s work but you might want to switch to only []'s for standardization and not sure if you might get in trouble later on?)
FROM: http://us1.php.net/manual/en/language.types.array.php
Note:
Both square brackets and curly braces can be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} will both do the same thing in the example above).
$fields{"Email"} = "Email";
should be:
$fields["Email"] = "Email";
You are already inside the php code, no need to add extra php start and end tags inside the variable name. Similar to how you have used the $to variable, you can use the $message variable.
So use
$message = $row_rsPersonUser['bio'];
and it would work fine.

Categories