Newlines are not interpreted when passed to php via command line
I have a PHP script that I call from another shell script that sends an automatic email with a message generated from the shell script. The problem is when I post a message, all newlines are printed in the message. How do you get them to be interpreted?
sendmail.sh:
/path/to/phpscript/sendmail.php "Some Message With Newlines\nHello World.\n"
sendmail.php:
$message = $argv[1] . "\nNewline";
$smtp->send($to, $from, $message);
The odd is \ n after the $ argv variable is interpreted and actually prints Newline on a new line, but the newlines in the $ argv variable don't work, I tried to wrap the variable in double quotes, among other things, but so far to no avail.
a source to share
How about calling the script with real newlines:
$ php temp.php "Some Message With Newlines
> Hello World.
> "
With temp.php
containing this:
var_dump($argv[1]);
Gets the following result:
string(40) "Some Message With Newlines
Hello World.
"
Edit: Another solution might be to use something like this to call your PHP script:
$ echo -e "Some Message With Newlines\nHello World.\n" | php temp.php
And change your PHP script to read from stdin
, instead of $argv
:
var_dump(file_get_contents('php://stdin'));
a source to share