Error: Could not instantiate mail function with $kirby->email

Hi all,

any idea why this error appears while sending mails with the contact form demo code?

thx for help.

Check this:

works! thanks alot!

I also have this damn error message “Could not instantiate mail function.”. Form ist hosted with Host Europe and they need this parameter ‘-f Adding it does not help and now I wonder, if I might add it at the wrong position?

I added it to the controller of the contact form. Oh … btw, I am using the mail contact form in the Cookbook.

                'template' => 'email',
                'from'     => '',
                'replyTo'  => $data['contactform-email'],
                'to'       => '',
                'subject'  => esc($data['contactform-name']) . ' hat eine Nachricht über die Website gesendet',
                'data'     => [
                    'text'   => esc($data['contactform-text']),
                    'sender' => esc($data['contactform-name']),

I also made sure that the standard email address needed by Host Europe also is set in their KIS. Any hint?

Thanks in advance and have a lovely day,


Hmm … I just read, that I actually don’t need the -f parameter, if I added a standard email:

Es ist möglich ohne den Parameter “-f” E-Mails aus Skripten zu versenden. Dazu ist es nötig eine Standard-E-Mail-Adresse im KIS einzurichten.

Strange. Why the heck to I get this error then?

Hey Marc! Is there anything in the PHP error logs or server error logs?

Hey. I just activated loggin errors. He did not have them turned on. I need to wait 15 minutes and try again to see if an error is recorded.

Hmm somehow it does not log anything. Neither on the website – as I set it to “Logging Output” to see if I get more info on the website. No chance to see any other info sadly. :frowning:

Hm, is sendmail enabled or are you sending mail via smtp transport?

Sendmail should be enabled. At least I used it with the form I built for the K2 version of this site. I thought, I do the shortcut and use the pre-built form, but I guess, I was wrong :joy:

Is there any other pre-built contact form around? Otherwise, I simply look into building my own again, as I have no idea why this is not sending.

OK. I found some more time today to check this and just wanted to notify you that it was a problem – as expected – with Host Europe and their way of dealing with stuff like this. All things (like their “Standard Email Address” and/or the -f parameter) were correctly set. But what my mistake was and what I did not know is, that you need to use an email address in the “from” field that is part of the web package you use the script in. You can’t use any email where the URL is not part of the package you run this script in. Even though you own the domain and run it on the same KIS or Server, it needs to be part of the Domain(s) you use in this package. This is strict, but makes sense in a way.

So now it is working perfectly.


Hi all! I would like to reopen this, as I have the same problem, too, and I am absolutely unable to solve it after struggling 2 days :flushed:

I am testing the site on a subdomain (without ssl encryption), might that be a problem? Does anyone have a hint about how to eventually debug this error more specifically, to get a clue about what is

php info says

sendmail_path /usr/sbin/sendmail -t -i
SMTP localhost
smtp_port 25

if that could help.

I did not change anything within the config.php file, so I guess SMTP is not configured within kirby and I am trying to send a mail from and to Anyhow, I tested the same system on another server and it worked! I am getting crazy …

The site is hostet at, by the way, maybe anyone experienced a similiar error with that hoster? A million thanks in advance!

If you call the mail() function manually in a template/controller, like so

mail ('', 'Test', 'works!', '', '-f');

does this work?

1 Like

I have tried this, and this works:

     $to = "";
     $subject = "This is subject";
     $message = "<b>This is HTML message.</b>";
     $message .= "<h1>This is headline.</h1>";
     $header = " \r\n";
     $header .= " \r\n";
     $header .= "MIME-Version: 1.0\r\n";
     $header .= "Content-type: text/html\r\n";
     $retval = mail ($to,$subject,$message,$header);
     if( $retval == true ) {
        echo "Message sent successfully...";
     }else {
        echo "Message could not be sent...";

Hi all!

It seems, that at least the error cause is found: after talking to the support, the reason seems to be, that Domainfactory changed their general server configuration and now they are forcing a ReturnPath with the parameter -f within the sendmail_path in the php.ini (which I cannot access). Kirby seems to force a ReturnPatch with phpmailer by its´ own, which causes an error. The suggested solution should be to add the parameter 0 to phpmailer.

 $mail->setFrom('absender@domain.tld', 'Mailer' ,0);

But I absolutely do not know where to integrate that in the system and I found no documentation. Alternatively, I tried to integrate this within my script of the control contact.php, but it does not work:

    'beforeSend' => function ($mailer) {
        $mailer->setFrom('', esc($data['name']) ,0);
        return $mailer;

I am not sure, where to integrate that correctly, here is my complete code, whichs throws the error " Undefined variable: data". I think after 2 days staring at this piece of code, I am getting blind, a coffe and a cake for the one who solves this:

return function($kirby, $pages, $page) {

    $alert = null;

    if($kirby->request()->is('POST') && get('submit')) {

        // check the honeypot
        if(empty(get('website')) === false) {

        $data = [
            'name'  => get('name'),
            'email' => get('email'),
            'text'  => get('text'),
			'optin' => get('optin')

        $rules = [
            'name'  => ['required', 'minLength' => 1],
            'email' => ['required', 'email'],
            'text'  => ['required', 'minLength' => 1, 'maxLength' => 3000],
			'optin' => array('required')

        $messages = [
            'name'  => 'Please enter a valid name',
            'email' => 'Please enter a valid email address',
            'text'  => 'Please enter a text between 3 and 3000 characters',
			'optin'  => 'Bitte markieren Sie die Checkbox, um uns zu erlauben Sie zu kontaktieren.'

        // some of the data is invalid
        if($invalid = invalid($data, $rules, $messages)) {
            $alert = $invalid;

            // the data is fine, let's send the email
        } else {
            try {
                    'beforeSend' => function ($mailer) {
                    $mailer->setFrom('', esc($data['name']) ,0);
                    return $mailer;
                    'template' => 'email',
                    'from'     => '',
                    'replyTo'  => $data['email'],
                    'to'       => '',
                    'subject'  => esc($data['name']) . ' hat Ihnen eine Nachricht über Ihr Kontaktformular geschickt',
                    'data'     => [
                        'text'   => esc($data['text']),
                        'sender' => esc($data['name']),
						'email' => esc($data['email'])						

            } catch (Exception $error) {
                    $alert['error'] = 'Ihre Nachricht konnte leider nicht verschickt werden <strong>' . $error->getMessage() . '</strong>';
                    $alert['error'] = 'Da ist leider etwas schiefgelaufen!';

            // no exception occurred, let's send a success message
            if (empty($alert) === true) {
                $success = 'Ihre Nachricht wurde erfolgreich versandt! Wir melden uns so schnell wie möglich bei Ihnen.';
                $data = [];

    return [
        'alert'   => $alert,
        'data'    => $data ?? false,
        'success' => $success ?? false

Thanks a million!!

When using a variable defined outside the scope of the closure, you have to pass it via a use statement:

    'beforeSend' => function ($mailer) use($data) {
        $mailer->setFrom('', esc($data['name']) ,0);
        return $mailer;
1 Like

Thank you for that hint! But with that it throws the “Could not instantiate mail function.” again.

Do you know another way to keep phpmailer from forcing the ReturnPath like

 $mail->setFrom('', 'Mailer' ,0);

or do you have an idea, why the code above does not work?

Thank you so much!

Hi there! I have solved the problem in the meantime and just wanted to let you know, in case anyone is experiencing similar issues. The following code uses the direct access to the phpmailer function within Kirby to let the server define the ReturnPath instead of phpmailer, by removing the “Sender” value:

                    'beforeSend' => function ($mailer) {
                        $mailer->Sender = '';
                        return $mailer;

Cheers and thank you for participating!

1 Like