php syntax for new line is not working - php

I am having a rather simple question
can anyone tell me why this is not displaying each variable on a new line, well except for
the <br>.
$curtime = gmdate("d/m/Y H:i:s");
//capture the PayPal returned information as order remarks
$oremarks =
"##$curtime##<br>".
"PayPal Transaction Information...\n".
"Txn Id: ".$ppInfo["txn_id"]."\n".
"Txn Type: ".$ppInfo["txn_type"]."\n".
"Item Number: ".$ppInfo["item_number"]."\n".
"Payment Date: ".$ppInfo["payment_date"]."\n".
"Payment Type: ".$ppInfo["payment_type"]."\n".
"Payment Status: ".$ppInfo["payment_status"]."\n".
"Currency: ".$ppInfo["mc_currency"]."\n".
"Payment Gross: ".$ppInfo["payment_gross"]."\n".
"Payment Fee: ".$ppInfo["payment_fee"]."\n".
"Payer Email: ".$ppInfo["payer_email"]."\n".
"Payer Id: ".$ppInfo["payer_id"]."\n".
"Payer Name: ".$ppInfo["first_name"]." ".$ppInfo["last_name"]."\n".
"Payer Status: ".$ppInfo["payer_status"]."\n".
"Country: ".$ppInfo["residence_country"]."\n".
"Business: ".$ppInfo["business"]."\n".
"Receiver Email: ".$ppInfo["receiver_email"]."\n".
"Receiver Id: ".$ppInfo["receiver_id"]."\n";
//Update database using $orderno, set status to Paid
//Send confirmation email to buyer and notification email to merchant
//Redirect to thankyou page
echo $oremarks;
thanks Richard

Carriage returns have no effect if you're viewing this output as HTML, so try turning them into <br> tags with nl2br...
echo nl2br($oremarks);

Because you are outputting the result in the browser window, try "<br />" instead of "\n".

Presumably because you are generating HTML source code from PHP, and not plain text.
In HTML, a new line is treated like any other whitespace. You need a <br> element or something that is display: block (or similar) to trigger a line break.

Try alternating the Double quotes for your array values, use single quotes instead
$curtime = gmdate("d/m/Y H:i:s");
//capture the PayPal returned information as order remarks
$oremarks =
"##$curtime##<br>".
"PayPal Transaction Information...\n".
"Txn Id: ".$ppInfo['txn_id']."\n".
"Txn Type: ".$ppInfo['txn_type']."\n".
"Item Number: ".$ppInfo['item_number']."\n".
"Payment Date: ".$ppInfo['payment_date']."\n".
"Payment Type: ".$ppInfo['payment_type']."\n".
"Payment Status: ".$ppInfo['payment_status']."\n".
"Currency: ".$ppInfo['mc_currency']."\n".
"Payment Gross: ".$ppInfo['payment_gross']."\n".
"Payment Fee: ".$ppInfo['payment_fee']."\n".
"Payer Email: ".$ppInfo['payer_email']."\n".
"Payer Id: ".$ppInfo['payer_id']."\n".
"Payer Name: ".$ppInfo['first_name']." ".$ppInfo['last_name']."\n".
"Payer Status: ".$ppInfo['payer_status']."\n".
"Country: ".$ppInfo['residence_country']."\n".
"Business: ".$ppInfo['business']."\n".
"Receiver Email: ".$ppInfo['receiver_email']."\n".
"Receiver Id: ".$ppInfo['receiver_id']."\n";
//Update database using $orderno, set status to Paid
//Send confirmation email to buyer and notification email to merchant
//Redirect to thankyou page
echo $oremarks;
But I would recommend using a HEREDOC instead of concatenating a string
$curtime = gmdate("d/m/Y H:i:s");
//capture the PayPal returned information as order remarks
$oremarks =<<<OREMARKS
##$curtime##
PayPal Transaction Information...
Txn Id: $ppInfo['txn_id']
Txn Type: $ppInfo['txn_type']
Item Number: $ppInfo['item_number']
Payment Date: $ppInfo['payment_date']
Payment Type: $ppInfo['payment_type']
Payment Status: $ppInfo['payment_status']
Currency: $ppInfo['mc_currency']
Payment Gross: $ppInfo['payment_gross']
Payment Fee: $ppInfo['payment_fee']
Payer Email: $ppInfo['payer_email']
Payer Id: $ppInfo['payer_id']
Payer Name: $ppInfo['first_name'] $ppInfo['last_name']
Payer Status: $ppInfo['payer_status']
Country: $ppInfo['residence_country']
Business: $ppInfo['business']
Receiver Email: $ppInfo['receiver_email']
Receiver Id: $ppInfo['receiver_id']
OREMARKS;
//Update database using $orderno, set status to Paid
//Send confirmation email to buyer and notification email to merchant
//Redirect to thankyou page
echo $oremarks;

In html, newlines never go to line. You have to put <br> in your source.
Notice that php can also works independently from http server as a command line utility and does not necessarily generate html.
Thus if you set your content type in web server to plain/text instead of html using header("Content-type: plain/text"); at the beginning of your file your text would go to line as you expected.

\n only shows a newline in the source. is the "newline" character for HTML.

The '\n' is just creating a new line in the html code, it is not creating a new line that is visible. You need to use html to get the new line to be visible. You can use the the html break <br> or you can make each line a paragraph <p> your text... </p> or you can use a list:
<ul>
<li> your text... </li>
<li> next item... </li>
<li> more stuff.. </li>
</ul>

Try putting in the following:
<?php
echo "<pre>";
.
.
.
?>

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?

add html url tag in joomla php function

i have installed joomla 3 and i have installed leave management component in my joomla. Its working perfectly. Now i want to customize the mail function. While changing the code i have some problem. i want to add HTML URL in message body. Here below the code.
//email function message body
$body = "Dear Mr. ".$manager_name.", \n\n You have received a leave request:\n\n From Name: " .$fromusers_name."\n\n Leave Type: " .$subject."\n\n From Date: " .$fromdate."\n\n To Date: " .$todate."\n\n Message: " .$message."\n\n ". ?>Click Here <?php ." " ; // email message body
If i run this code means my joomla is not running, simply blank page coming.
Try using the following:
$body = "Dear Mr. ".$manager_name. ", \n\n
You have received a leave request:\n\n
From Name: " .$fromusers_name. "\n\n
Leave Type: " .$subject. "\n\n
From Date: " .$fromdate. "\n\n
To Date: " .$todate. "\n\n
Message: " .$message. "\n\n <a href='http://intranet/intranet'>Click Here</a>";

Sending Plain Text Emails

I am trying to send plain text emails to a company who will be processing them for me. I am using the Swift Mailer library and as far as I can tell - I have no problems. But the company that I am sending the messages to says that they are coming into them with all of the data on a single line. When I send myself an email using this script it looks perfect - each piece of data is on a separate line.
Here is the script I am using. It is very simple.
<?php
$to="";
$bcc="";
$subject = "Quote" ;
$domain = $_SERVER['HTTP_HOST'];
//Message
$message = "Domestic Quote Request\n";
$message .= "Full Name: $_POST[name]\n";
$message .= "Email: $_POST[email]\n";
$message .= "Phone: $_POST[phone]\n";
$message .= "Pickup Date: $_POST[shipdate]\n";
$message .= "Pickup City or Zip: $_POST[pickupzip]\n";
$message .= "Drop Off City or Zip: $_POST[dropoffzip]\n";
$message .= "Year: $_POST[vehicleyear]\n";
$message .= "Make: $_POST[vehiclemake]\n";
$message .= "Model: $_POST[vehiclemodel]\n";
$message .= "Carrier Type: $_POST[carriertype]\n";
$message .= "This Quote is from $domain";
require_once 'lib/swift_required.php';
// Create the Transport
// Mail
$transport = Swift_MailTransport::newInstance();
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
$the_message = Swift_Message::newInstance()
// Give the message a subject
->setSubject($subject)
// Set the From address with an associative array
->setFrom($_POST['email'])
// Set the To addresses with an associative array
->setTo(array($to))
->setEncoder(Swift_Encoding::get8BitEncoding())
// Give it a body
->setBody($message, 'text/plain', 'us-ascii');
//Check is BCC Field is emtpy.
if ( !empty($bcc) )
{ //The BCC Field is not empty so set it.
$the_message->setBcc(explode(',', $bcc));
}
// Send the message
$result = $mailer->send($the_message);
}
header("location:index.php?s=1");
?>
Here is an example of what the company is saying they are receiving:
Domestic Quote Request Full Name: Test Email: someone#somewhere.com Phone: 555-555-5555 Pickup Date: 07/02/2012 Pickup City or Zip: 12938 Drop Off City or Zip: 23981 Year: 2009 Make: Audi Model: A4 Carrier Type: enclosed carrier This Quote is from awebsite.com
And this is what I receive when I email myself.
Domestic Quote Request
Full Name: Test
Email: someone#somewhere.com
Phone: 555-555-5555
Pickup Date: 07/02/2012
Pickup City or Zip: 12938
Drop Off City or Zip: 23981
Year: 2009
Make: Audi
Model: M4
Carrier Type: enclosed carrier
This Quote is from awebsite.com
The company said that they believed that the lead was being sent as free text instead of plain text. Is there such a thing? I have never heard of free text.
Thank you in advance for the help.
The only thing I can think of is that the web client they are viewing the email in parses emails in HTML. So instead of \n try <br /> maybe.
Email body requires Microsoft \r\n line endings.
The headers require Linux \n line endings.

Unexpected $end in PHP file

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;

Categories