Script to test email sending functionality

Hi guys,

I’m wondering, is there a simple PHP script that I could use via CLI / PuTTY on my shared hosting server to test Nextcloud’s email sending function? I know that Nextcloud sends out emails when I push the button “Test email settings - Send email”, and AJAX cron seems to be working okay, but I’d like to switch to a cron job that uses an internal server path on my hosting provider.

The problem is that I discovered today in connection with a different system running on the same server that I am not able to use the PEAR library Mail.php at all from the CLI or with a cron job that uses an internal server path to send emails (although it works fine when called via a browser). Apparently my hosting provider has completely blocked the functionality in the CLI that the PEAR library uses to send email.

I’m concerned that if Nextcloud email sending function uses a similar library, it may also be affected. Any information would be greatly appreciated!

Thanks!

I just activated cron via server path, and after a few minutes I received a notification. Is it really safe to assume that AJAX cron is now completely disabled and Nextcloud is now successfully relying only on the cron job? I know it sounds paranoid, but stuff is really screwed up on my server and I need to make sure. :smiley:

I have downloaded and modified the PHP SMTP test script from this website to use my own server address and my credentials, and I can confirm that it is not possible to open a connection from the CLI on my server (PuTTY) to ports 25, 465 or 587 – and I am pretty sure this also affects cron jobs that run via internal server path. It works when called through the browser, but not through CLI. How, then, is Nextcloud able to send out Email notifications? Does Nextcloud use an entirely different approach?

Hello @RayCulp,

Nextcloud is a web application and runs in the context of the webserver.
That means NC is not relying on CLI access of any mail library.

If you call cron.php from system cron you have to make sure call is started by webserver user.
See docs at:

https://docs.nextcloud.com/server/18/admin_manual/configuration_server/background_jobs_configuration.html?highlight=cron#cron

Hope that explains it.

Just go to system settings as admin and set cron to system cron and there you can see when I it get triggered. That way I would check that cron is correctly working.

Thank you very much for your input, @rakekniven.

I temporarily added the following command to the top of cron.php:

file_put_contents('/var/www/myusername/html/debug.txt', date('Y-m-d H:i:s').' Running Nextcloud cron'."\r\n", FILE_APPEND);

This way I can see an entry each time cron.php is called.

When I inspect the file debug.txt, I can see that cron.php is being called correctly every five minutes by the cron job. The entry is there.

However, and here is the twist, it only actually does anything when called via URL using exec ('wget https://nextcloud.mydomain.com/cron.php');. It does nothing when called via server path using include ('./nextcloud/cron.php'); or exec('php /var/www/myusername/html/nextcloud/cron.php'); I see the log entry in debug.txt, but Basic settings, Background jobs says the cron hasn’t run for XX amount of time, something seems to be wrong.

Could this be because my hosting provider is blocking outgoing connections for the user context that is running the cron job?

Like I mentioned before, I am having trouble connecting to my SMTP server via CLI as well as via cron using an internal server path (such as /var/www/web486/html/nextcloud/).

Why not calling your cron by a wget command.
Result should be the same.

For many drupal sites I have one raspberry calling something like:

wget -q http://www.host.domain/cron.php -O /dev/null

Use the support of your Hoster. Ask them about the best way to call up cron. Nextcloud is not so unpopular :wink: So hopefully the can help you in a satisfying way.

Well, if I know that I can only run cron jobs via URL or AJAX, then I can adapt accordingly. But my hosting provider explicitly gives me the option to run via server path, and as far as I can tell, it’s not working. I have a very long, complex support ticket open with my hosting provider, so we’ll see that they come up with. So far, they seem pretty clueless. :slight_smile:

Even though things are working now via web cron, it would still be good to see if we could narrow down the list of possible reasons that cron.php can be called via server cron or CLI but still doesn’t do anything. When I do SMTP tests with the PEAR libary, I am seeing that it is not possible to connect to SMTP from the CLI. I’m getting time-out errors trying to connect to the socket. When I try the test script via browser, it works fine. Would blocked ports in a firewall, for example, explain why web cron and AJAX work fine in Nextcloud, but cron via server path or CLI does not?

Hello @RayCulp,

to be honest I think you miss some basic understanding of php regarding webserver module and CLI. You can have a total different setup for both of them on the same machine.
In your case you do not know the settings of your hoster in detail.
That make it even more difficult to find the cause.
One recommendation from my site would be to change your hosting partner to one who is understanding, offering and supporting Nextcloud.

Only your hoster can tell you why.

So basically, my hoster told me a few days ago that there’s something wrong with the server, they’re not exactly sure what it is, and they’ll move me to one of their newer server environments to test things out, and if it solves the problems, then I can stay for free. Case closed. :slight_smile:

1 Like