WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] PHP Warning on Cron Run in wp_mail (8 posts)

  1. WhyChooseBob
    Member
    Posted 10 months ago #

    To test out the automatic update feature in trunk, I set up a cron job to run wp-cron.php twice a day. Since then, I've been receiving a warning email from the cron with the output:

    <br />
    <b>Notice</b>:  Undefined index: SERVER_NAME in <b>/.../path/to/wp-includes/pluggable.php</b> on line <b>331</b><br />

    Also, I noticed that the auto-update notification emails were being sent from "WordPress <wordpress@>".

    Line 331 in pluggable.php is found in wp_mail:

    if ( !isset( $from_email ) ) {
      // Get the site domain and get rid of www.
      $sitename = strtolower( $_SERVER['SERVER_NAME'] ); // line 331
      if ( substr( $sitename, 0, 4 ) == 'www.' ) {
        $sitename = substr( $sitename, 4 );
      }
    
      $from_email = 'wordpress@' . $sitename;
    }

    I would have thought this function would use get_option('siteurl') or similar, since SERVER_NAME isn't guaranteed to be set when php is run from the command line.
    Checking my production install (running WP 3.6.1), it seems the code is the same. I don't see the warning emails coming from my production site (I'm guessing WP_DEBUG may have something to do with that).
    Both my production and development sites are running on the same server.

  2. What's the domain like for the dev site?

    Does the account have access to mail? Like if you add a new user, do you get emailed?

  3. WhyChooseBob
    Member
    Posted 10 months ago #

    Both dev and prod have the same server and site config (it's a cPanel server - I have root access - and they're running under the same *nix account, and I made a direct copy of the prod database, then fixed the site options to point to the right place, when I set up the dev site).

    The account has email access - sending the email isn't the problem (it comes through just fine, aside from the 'From' header being "WordPress <wordpress@>"). I tested adding a new user, and received the email just fine (with the 'From' header set to "WordPress <wordpress@<DEV_BLOG_DOMAIN>>").

    The $_SERVER superglobal contains the following when run from a cron job:

    Array
    (
        [SHELL] => /bin/sh
        [MAILTO] => <EMAIL>
        [USER] => <USERNAME>
        [PATH] => /usr/bin:/bin
        [PWD] => /.../path/to/account/home
        [SHLVL] => 1
        [HOME] => /.../path/to/account/home
        [LOGNAME] => <USERNAME>
        [_] => /usr/bin/php
        [PHP_SELF] =>
        [REQUEST_TIME_FLOAT] => 1380934742
        [REQUEST_TIME] => 1380934741
        [argv] => Array
            (
                [0] => /.../path/to/account/home/test.php
            )
    
        [argc] => 1
    )
  4. That kind of error points to something wrong with THAT WP setup, y'know :/ I mean, if there's nothing wrong with the server and the account can still send email...

    Try manually copying the WP core files from 3.6.1 over that dev install and if the problem still happens?

  5. WhyChooseBob
    Member
    Posted 10 months ago #

    To me, it doesn't seem like a setup problem. I've never known PHP to set variables that it doesn't know about (SERVER_NAME is given to PHP by Apache, as far as I know). The cron job is not being run through Apache, so PHP isn't told about SERVER_NAME.

    (To make sure we're on the same page, when I say cron job I mean the unix crontab - my crontab line is 0 0,12 * * * php -q /.../path/to/dev/wp-cron.php).

    I ran my little print_r($_SERVER); script on another server I have access to (non-cPanel, different version of PHP, and not related to my main server in any way), and got:

    Array
    (
        [SHELL] => /bin/sh
        [USER] => <USER>
        [PATH] => /usr/bin:/bin
        [PWD] => /.../path/to/account/home
        [SHLVL] => 1
        [HOME] => /.../path/to/account/home
        [LOGNAME] => <USER>
        [OLDPWD] => /.../path/to/account/home
        [_] => /usr/bin/php
        [PHP_SELF] => test.php
        [SCRIPT_NAME] => test.php
        [SCRIPT_FILENAME] => test.php
        [PATH_TRANSLATED] => test.php
        [DOCUMENT_ROOT] =>
        [REQUEST_TIME] => 1381113241
        [argv] => Array
            (
                [0] => test.php
            )
    
        [argc] => 1
    )

    Again, no SERVER_NAME.

  6. The cron job is not being run through Apache, so PHP isn't told about SERVER_NAME.

    (To make sure we're on the same page, when I say cron job I mean the unix crontab - my crontab line is 0 0,12 * * * php -q /.../path/to/dev/wp-cron.php).

    Why not run curl http://example.com/wp-cron.php ?

    That's how you're supposed to, last I checked...

  7. WhyChooseBob
    Member
    Posted 10 months ago #

    That'd definitely fix it. I can't seem to find where it's documented that you're supposed to run wp-cron from crontab through curl/wget, though. The most I can see is the top comment within wp-cron.php itself, which doesn't even say to do so:

    /**
     * WordPress Cron Implementation for hosts, which do not offer CRON or for which
     * the user has not set up a CRON job pointing to this file.
     *
     * The HTTP request to this file will not slow down the visitor who happens to
     * visit when the cron job is needed to run.
     *
     * @package WordPress
     */

    (Since the HTTP sentence talks about 'the visitor', I assumed that was only meant for when wp-cron isn't set up to run from crontab).

    Doing a bit more searching, it seems I'm not the only one thinking that using SERVER_NAME is not the best option: http://core.trac.wordpress.org/ticket/25239.

  8. Honestly I don't remember where I learned to use curl/wget, but that's what I was always told when I asked a million years ago (okay 3 or so). I think it's because I wanted to 'act' like a real visitor.

    I'm marking this resolved since there is a trac ticket (it's how we do things in this subforum to stop going nuts).

Reply

You must log in to post.

About this Topic