Problems sending mail during register_shutdown_function

Hello,

I’m logging all errors and warnings of PHP/Yii2 by using an e-mail target:




        'log' => [

            'targets' => [

                'email' => [

                    'class' => 'yii\log\EmailTarget',

                    'mailer' => 'mailer',

                    'levels' => ['error', 'warning'],

                    'message' => [

                        'from' => ['from@domain.com'],

                        'to' => ['to@domain.com'],

                        'subject' => 'Log message',

                    ],

                ],

            ],

        ],

        'mailer' => [

            'class' => 'yii\swiftmailer\Mailer',

        ],



E-Mails are successfully send by Swiftmailer, but only during normal runtime. When sending log messages during "register_shutdown_function", PHP mail() fails. This is a restriction of my shared hosting environment, which resets the current working directory to the root (/) before shutdown functions are called.

In order to send mails during "register_shutdown_function", I have to manually change the working directory back to my www-root. How can I achieve this? Is it possible to extend Yii so that it automatically sets the working directory during register_shutdown_function back to the default Yii directory?

Thanks,

karl

Do you know how was such reset achieved? Would be interesting to reproduce it…

I don’t know how my hoster achieves this reset, but I think it is normal PHP/Apache behavior, see notes and comments at http://php.net/manual/en/function.register-shutdown-function.php

I confirmed such behavior of shutdown function but unable to reproduce any problems so far. Do you have any details about the problem? Any extra errors in logs etc. i.e. any idea what doesn’t work exactly?

Hi samdark,

I cannot give you an error log, because no errors are reported. It is a restriction of my hoster (http://www.hosteurope.de). You can find (a german) description here.

At the end of the page, you can find the statement, that I can only access /usr/sbin/sendmail or mail() if the working directory is /is/htdocs/wpXXXXX_YYYY/ or below (user specific root) but not from the server root /.

So my question is, is it possible to instruct Yii that it keeps the directory from which the start script was called when entering the shutdown procedure?

Kind regards,

karl

Yes, it should be possible with a custom email target class like the following:




class EmailTarget extends yii\log\EmailTarget

{

    public function export()

    {

        chdir('/is/htdocs/wpXXXXX_YYYY/');

        parent::export();

    }

}



Thank you for your response, I think this is a clean solution.