Character spacing in email sent by cron job

Have several php scripts that run on cron. Within those scripts are several print or echo commands so I know what the script actually did.
How do I get a single carriage return?
if I use \n, I get nothing.
If I use \n\n, I get double spacing
Any ideas?
Thanx

Try return then new-line \r\n

PHP has a built in constant, PHP_EOL, which will use the operating system’s newline character. EG:

echo "first line", PHP_EOL, "second line", PHP_EOL;

THANK YOU will try it
:slight_smile:

Doesn’t work. Get same results. \n\n gives me double spacing and PHP_EOL: gives nothing.
Output is “User Paul xxxxx(337) sent inactive email PHP_EOL;User Thomas xxx(4) sent inactive email PHP_EOL;” etc.

First, I said \r\n NOT \n\n…
( \r is RETURN and \n is NEWLINE )

Next, Skawid used commas. He meant to use periods for concatenation.

echo “your first text” . PHP_EOL . “some more text” . PHP_EOL;

I prefer the \r\n as it takes less typing and can be placed inside the quotes…

echo “your first text\r\nsome more text”;
( But, is harder to read, hence Skawid’s version might be better. )

@ErnieAlex you can actually use commas there! It’s equivalent to passing the echo statement multiple arguments.

@N00bie I’m guessing you mixed up which parts of the statement should be surrounded by quotes. The PHP_EOL constant should not be surrounded by quotes.

Actually you can use either. Comma’s makes PHP send four separate sections and periods makes it send one continuous section. Either way works well… And, yes, he probably just spelled it wrong…

Since echoing anything in a script being ran as a cron job will need ‘help’ (output buffering in the script or >> redirecting output in the cron command line) to be seen, what exactly is your code/command that’s producing and saving this output and what client/program are you using to view the output?

I suspect the problem is single vs double quotes around values, but it would take knowing the specific details of what you are doing to narrow down the possibilities.

Phdr, he is creating automated reports and needs to use them.

First, tried /r/n long ago. Only thing I have found thus far that works at all is \n\n for double spacing.
Am now trying
echo “$lname: $whendue” . PHP_EOL;
Running through broser it doesn’t work
Set cron it to run every hour - but gotta head out for several hours and bbl

What am I running? Very simple:
blah, blah, bla
extract($row);
$whendue = date(‘F d, Y’,$expiredate);
echo “$lname: $whendue” . PHP_EOL;
}

NOT /r/n !

Try \r\n

It works on every server I ever used…

Browser? New-line characters are not rendered and displayed by a browser unless you specifically output them inside of <pre>...</pre> tags or apply something like nl2br() to add <br> tags to them.

Thought you said it was a CRON job? For browser displays, you use
, for CRON reports, otherwise!

To display on BOTH, use \r\n< br> or something like that…

How we got from email to browser I do not know. Anyway, it is a cron job handled by cpanel with results sent by email.
\n does nothing

(br) does nothing
\r does nothing
\r\n does nothing
\n\r does nothing
\n\n gives double space
“xx”. PHP_EOL; does WORK

Thank you for your thoughtful input and assistance. Much appreciated

Well, as someone said before, you need to try it this way:

$string = ‘some text’ . “\r\n” ;
( Note that you must use double-quotes around the return-code and new-line code. Single quotes do not work correctly. )

By the way, if you use . PHP_EOL, that is end-of-line. So, it stops there. To move to the next line. You might need two of them .PHP_EOL . PHP_EOL to skip a line.

I think I am going to give up cause this is really not working. The PHP_EOL worked in my test script but not in the actual script ???

Here are some results:

echo “User $fname $lname ($id) sent dues reminded” . PHP_EOL;
RESULT: User Karren xxx(21) sent dues reminder User Jean-Philippe xx(66) sent dues
reminder User Miguel xxxx (69) sent dues reminder …
(funny it worked on my test script as: echo “$lname: $whendue” . PHP_EOL; )

echo “User $fname $lname ($id) sent inactive email” . “\r\n”;
RESULT: User Paul xxx(337) sent inactive email User Thomas xxx(4) sent inactive email User Robin xxx (6) sent inactive email User Kumi xxx (7) sent inactive email User

echo “User $fname $lname ($id) sent dues reminder” . PHP_EOL . PHP_EOL;
RESULT: Double spaced:
User Karren xx(21) sent dues reminder

User Jean-Philippe xxx(66) sent dues reminder

User Miguel Salgado xxx(69) sent dues reminder

Not sure there is anything else to even try. That why might just give up. That single space just not happening

Well, there are many many things that could interfere with this working.

First, how are you sending out emails? Normally you can’t use echo to do this.

Next, are you attempting to print to an email or a printed report? You have not really said what you are attempting to do inside a CRON job.

If you create a report in a CRON job, it is usually either a file such as a text file or WORD doc or PDF.
Then, you would view the results using a text editor, MS-Office or web page to view PDF’s. If it is a report that you want to read, where are you storing it and how are you reading it. All of these details will affect which way you create the text output.

So, we know you are creating it in a PHP CRON job which probably runs at a set time. Where are you saving the report and how do you read it once it is created. I am sure it is something simple you need to change. I have done many CRON scripts and never have any issues reading reports from them. Please give us further details. Thanks, Ernie

Oh, NOTE: It also can be WINDOWS vs MAC, etc. Here is a good site that explains the differences, it might help explain it better: EOL’s

The cron is being setup through cPanel. I have it set to run every hour just for testing, otherwise it would normally run once a month.
cPanel has cron email and that how being sent (cpanel gives little detail on this). Running Centros 7.9 on the server. Emails being retrieved and read with MS Outlook.
Script runs 3 queries on membership:

  1. Sends an email to all delinquent/inactive members. Echo members sent email.
    echo “User $fname $lname ($id) sent inactive email” . “PHP_EOL”;
  2. Inactivate expired members
    echo “User $fname $lname ($id) active status changed to n” . PHP_EOL;
  3. Send dues reminder (60 days from due date)
    echo “User $fname $lname ($id) sent dues reminder” ."\r\n";

Okay, so that sounds all normal. You are sending emails as reports. Therefore, you have a dozen ways to send them. Which way are you sending them? Using a library or thru PHP mailer?

If you are sending HTML email then use < BR> (or < BR />, or < /BR>) . If you use content-type: text/html you need to put a < br> because your message will be threated like an html file. But if you change your content-type to text/plain instead of text/html you will be able to use \r\n characters

Sponsor our Newsletter | Privacy Policy | Terms of Service