Sending an email fails, phpmail.log?

Hallo!

I am trying to send an email using the built-in Kirby function, but it fails.
The E-Mail will be send, though as in it arrives in my mailbox, but the validation still fails…?

exception 'Whoops\Exception\ErrorException' with message 'mail(/var/log/phpmail.log): failed to open stream: Permission denied' in /home/www-data/seed.eyekon.ch/docs/kirby/vendor/getkirby/toolkit/lib/email.php:159
Stack trace:
#0 [internal function]: Whoops\Run->handleError(2, 'mail(/var/log/p...', '/home/www-data/...', 159, Array)
#1 /home/www-data/seed.eyekon.ch/docs/kirby/vendor/getkirby/toolkit/lib/email.php(159): mail('to@example.com', 'Sending emails ...', 'Hey! This was r...', 'From: my@email....')
#2 [internal function]: {closure}(Object(Email))
#3 /home/www-data/seed.eyekon.ch/docs/kirby/vendor/getkirby/toolkit/helpers.php(270): call_user_func_array(Object(Closure), Array)
#4 /home/www-data/seed.eyekon.ch/docs/kirby/vendor/getkirby/toolkit/lib/email.php(127): call(Object(Closure), Object(Email))
#5 /home/www-data/seed.eyekon.ch/docs/site/snippets/newsletter.php(28): Email->send()
#6 /home/www-data/seed.eyekon.ch/docs/kirby/vendor/getkirby/toolkit/lib/tpl.php(22): require('/home/www-data/...')
#7 /home/www-data/seed.eyekon.ch/docs/kirby/kirby/component/snippet.php(38): Tpl::load('/home/www-data/...', Array, false)
#8 /home/www-data/seed.eyekon.ch/docs/kirby/helpers.php(12): Kirby\Component\Snippet->render('newsletter', Array, false)
#9 /home/www-data/seed.eyekon.ch/docs/site/templates/news_overview.php(17): snippet('newsletter')
#10 /home/www-data/seed.eyekon.ch/docs/kirby/vendor/getkirby/toolkit/lib/tpl.php(22): require('/home/www-data/...')
#11 /home/www-data/seed.eyekon.ch/docs/kirby/kirby/component/template.php(103): Tpl::load('/home/www-data/...', NULL, true)
#12 /home/www-data/seed.eyekon.ch/docs/kirby/kirby.php(635): Kirby\Component\Template->render(Object(Page), Array)
#13 /home/www-data/seed.eyekon.ch/docs/kirby/kirby.php(623): Kirby->template(Object(Page), Array)
#14 /home/www-data/seed.eyekon.ch/docs/kirby/kirby/component/response.php(29): Kirby->render(Object(Page))
#15 /home/www-data/seed.eyekon.ch/docs/kirby/kirby.php(705): Kirby\Component\Response->make(Object(Page))
#16 /home/www-data/seed.eyekon.ch/docs/index.php(16): Kirby->launch()
#17 {main}

I am using the demo snippet (https://getkirby.com/docs/cheatsheet/helpers/email)

$email = email(array(
  'to'      => 'bastian@getkirby.com',
  'from'    => 'my@email.com',
  'subject' => 'Sending emails with Kirby is easy',
  'body'    => 'Hey! This was really easy!'
));

if($email->send()) {
  echo 'The email has been sent';
} else {
  echo $email->error();
}

On the server I am not allowed to access /var/log/ (managed server) and I can’t find any references to phpmail.log in all of Kirby’s files :frowning:

Any idea what I am doing wrong?

Thanks!

I found this on SO, maybe it helps.

Hi! No not really, I can’t access /var/log/, as I mentioned in my post; but thank you!!

I understand that. But the point is that one suggestion is to change the location of the log file in php.ini (and create that file location somewhere, where you have access and permissions to).

Anyhow, this is nothing Kirby can fix, because it is a permission problem. If you can’t find a solution, you may want to contact your provider. Or use PHPMailer, or an external service.

I just tried something along your input:
when I create a single file (not within Kirby), it works without problems; the mail is send, no error appears.
I am just using plain PHP mail() function.
Using the same within Kirby, fails with

Whoops \ Exception \ ErrorException (E_WARNING)
mail(/var/log/phpmail.log): failed to open stream: Permission denied

(this time, in the Kirby Debugger)

Kirby uses the plain PHP function as well, as you can see in the source code:

email::$services['mail'] = function($email) {

  $headers = array(
    'From: ' . $email->from,
    'Reply-To: ' . $email->replyTo,
    'Return-Path: ' . $email->replyTo,
    'Message-ID: <' . time() . '-' . $email->from . '>',
    'X-Mailer: PHP v' . phpversion(),
    'Content-Type: text/plain; charset=utf-8',
    'Content-Transfer-Encoding: 8bit',
  );

  ini_set('sendmail_from', $email->from);
  $send = mail($email->to, str::utf8($email->subject), str::utf8($email->body), implode(PHP_EOL, $headers));
  ini_restore('sendmail_from');

  if(!$send) {
    throw new Error('The email could not be sent');
  }

};

And the mail is obviously being sent.

Could you just disable Whoops for the moment?

c::set('whoops', false);

but leave debugging on, and check what happens?

I saw that Kirby uses the normal PHP function, that is why I am confused as to why this happens.

I altered the code to be the following. Mail is send, but error is also echoed:

$email = email(array(
  'to'      => 'to@example.com',
  'from'    => 'my@email.com',
  'subject' => 'Sending emails with Kirby is easy',
  'body'    => 'Hey! This was really easy!'
));

if($email->send()) {
  echo 'The email has been sent';
} else {
  echo $email->error();
}

Output is:

Warning: mail(/var/log/phpmail.log): failed to open stream: Permission denied in /home/www-data/example.com/docs/kirby/vendor/getkirby/toolkit/lib/email.php on line 159
The email has been sent

So to me it is not a permissions problem, but a Kirby problem? - even though I can’t find any references to phpmail.log or why exactly the log has to be written (or what is even in it!)
Backtracking within the Whoops Vendor did not help - or at least I don’t know where to look at

I don’t think that Kirby tries to write to the log file… Can you find anything regarding sendmail logs in your php.ini?

ah damn! That was stupid of me!!
Yes, there is an entry in the php.ini!
I will contact the provider :slight_smile:

Thank you so much!

Let me know how you finally solved the issue.

So the admin changed the php.ini mail.log path to be a valid file.

Now the E-Mail can be send, but the file gets a new entry for every E-Mail that was send:

[02-May-2017 09:37:43 UTC] mail() on [/home/www-data/example.cmo/docs/kirby/vendor/getkirby/toolkit/lib/email.php:159]: To: to@example.com -- Headers: From: my@email.com Reply-To: my@email.com Return-Path: my@email.com Message-ID: <1493717863-my@email.com> X-Mailer: PHP v5.5.9-1ubuntu4.21 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit

Is there any way to change this?

I guess you mean “file” not “fail”?

As I said above, the mail log is nothing that is generated by Kirby. I think if you uncomment the log line in php.ini, no log should be created at all. This seems to be a PHP configuration thing.

yes sorry, typo :slight_smile:
Mhh ok I see - thank you for all your support!

The issue is therefore resolved :slight_smile:

Great! Your are welcome :slight_smile:.

1 Like