0

I'm trying to use the php mail function from cmd,with following command:

php -r "\$from = \$to = '[email protected]'; \$x = mail(\$to, 'subject'.time(), 'Hello World', 'From: '. \$from); var_dump(\$x);"

I get this error: Parse error: syntax error, unexpected token "", expecting end of file in Command line code on line 1 could it be because of wrong directory ? or what am I missing in the code ?

7
  • 1
    It would be easier if you inverted the quotes, and used single quotes to wrap PHP code. This way you wouldn't have to escape variable sigils. Commented Mar 27, 2021 at 19:52
  • @weirdan I tried and it didn;t work but I;m not sure if I did it correctly,because I didn;t uderstand well what you ment. is this right ? '(php -r '$from ="hello" $to = "[email protected]"; $x = mail($to, "subject".time(), "Hello World", "From: ". $from); var_dump($x);')' Commented Mar 27, 2021 at 20:27
  • Pasted that line into my ubuntu terminal. Works. Tested with bash and fish. Did you do that from windows command prompt? Commented Mar 27, 2021 at 20:34
  • yes I'm using windows Commented Mar 27, 2021 at 20:46
  • 2
    Windows CMD doesn't use backslashes for escapes, nor does it use dollar-signs (used by PHP) for interpolated variables. Commented Mar 30, 2021 at 7:20

1 Answer 1

2

The \ character doesn't play any role in CMD—just get rid of it. Neither does $, so it doesn't need to be escaped:

C:\>php -r "$from = $to = '[email protected]'; $x = mail($to, 'subject'.time(), 'Hello World', 'From: '. $from); var_dump($x);"
PHP Warning:  mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in Command line code on line 1

Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in Command line code on line 1
bool(false)

Whether it's a good idea is left as exercise for readers ;-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.