I am developing a simple PHP script that will send emails to users. The script is located outside of WP directory and its run through a cronjob. Example cron job call:

php /PATH/TO/SCIRPT/script.php >> /PATH/TO/SCIRPT/history.log

and here’s part of the code:


require_once "/PATH/TO/WP/wp-load.php";

$res = wp_mail('email', 'title', 'body');

When I use PHP CLI to run the script $res is always false. But if I run the same script from the web it works just fine.

Below are the server details: ubuntu 16.04lts up-to-date with php 7.0.28-0ubuntu0.16.04.1 (cli) ( NTS )

Any help will be much appreciated.

  • not sure your server config, if you're on a lamp stack php-cli uses a different php.ini for apache2 or fpm, and cli perhaps verify email settings are right in /etc/php/7.x/cli/php.ini – David Sword Apr 9 '18 at 16:39
  • Dev server is running Ubuntu 16.04LTS (vagrant box). The production server has the exact same setup. I will check cli php.ini settings right now. – mr.d Apr 9 '18 at 16:43
  • Any particular reason why you are using wp_mail rather than then PHP mail function? – Rick Hellewell Apr 9 '18 at 18:31
  • @RickHellewell Yes. I am using WordPress SMTP plugin to send emails using my Mailgun account. Using wp_mail() I will be able to send emails without entire PHP library (PHPMailer) just for 20 lines of code. – mr.d Apr 10 '18 at 3:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.