3

SOLVED! Solution: adjust sendmail_path value in php_cli/php.ini as well.

I'm running PHP 7.4 on Ubuntu 20.x

I've installed msmtp in order to send emails with php.

Problem: running mailtest.php from browser does work. running mailtest.php by a cronjob doesn't work, I get this error in my logfile.

sh: 1: /usr/sbin/sendmail: not found
problem:

Here's the line from my /etc/crontab

00 23 * * * root /usr/bin/php /var/www/mailtest.php >> /var/log/mails.log 2>&1

mailtest.php

$header = 'From: '.$mail_from. "\r\n" . 'Reply-To: '.$mail_from."\r\n" ;

if ($mailstatus = mail($mail_to, $mail_subject, $mail_body, $header)){
        echo "sent";
} else {
        echo "problem: ".$mailstatus;
}

It's probably some kind of permission issue I guess. I've already tried to change root -> www-data but in this case nothing happens at all, my mails.log-file stays empty. No Mail sent as well.

This is driving me nuts, I spent hours of googeling and really don't understand what is going on.

Any ideas? Any further details/stuff I can check?

Thank you very much for your time!

jan
  • 31
  • 5
  • write the file mailtest.php. – Giacomo M Dec 03 '20 at 23:07
  • @GiacomoM I've edited my post. – jan Dec 03 '20 at 23:33
  • Post the response from `which sendmail` from your server CLI; this will return the path to your sendmail; it might not be `/usr/sbin/sendmail` in your case – rjbathgate Dec 03 '20 at 23:52
  • A quick method to fix the problem is --- change your cron so that you are using local browser to do the job (make sure you have installed lynx) command is: 00 23 * * * lynx -dump http://[your webite]/mailtest.php – Ken Lee Dec 03 '20 at 23:56
  • @rjbathgate sendmail is not installed but msmtp. The value of sendmail_path in my php.ini is set to /usr/bin/msmtp. I really don't understand why it's looking for /usr/sbin/sendmail when running as cron job. Again: it works fine if I just access the php-file from my browser. – jan Dec 04 '20 at 12:40
  • @KenLee thank you for your suggestion. Since i don't have lynx installed and would really like to understand and fix this bug, I won't be going this way right now. – jan Dec 04 '20 at 12:56
  • I believe you system configuration is that when in command mode it is using sendmail instead of msmtp for mail (yes sendmail is sometimes the defualt) . But since you have not installed sendmail so the system throws an error when PHP tries to send mail in a cronjob. – Ken Lee Dec 04 '20 at 23:02
  • @KenLee thank you very much for your thoughts! They helped me a lot finding a solution. After hours of googeling I've learned that php uses a different php.ini for cli. After adjusting sendmail_apth value to msmtp there it's now finally working as expected. :) – jan Dec 06 '20 at 00:02
  • good to know that you have fixed the problem. Have a nice weekend. – Ken Lee Dec 06 '20 at 01:22

0 Answers0