Kirby Uniform won't send from server (AJAX + Routes)

Hey there, I’m trying to send notifications from @mzur’s Kirby Uniform using a route and AJAX. Everything worked well from my local dev environment, however as soon as I move it to the server nothing has been getting through.

Any ideas?

'routes' => [
    [
      'pattern' => 'postman',
      'method' => 'POST',
      'action' => function () {
          $form = new \Uniform\Form([
            'email' => [],
            'fname' => [],
            'lname' => [],
            'phone' => [],
            'reasons' => [],
            'comments' => [],
            'subscribe' => []
          ]);

      // Perform validation and execute guards.
      $form->withoutFlashing()
          ->withoutRedirect()->guard();

      if (!$form->success()) {
        // Return validation errors.
        return Response::json($form->errors(), 400);
      }

      // If validation and guards passed, execute the action.
      $form->emailAction([
        'to' => 'info@website.com',
        'from' => 'info@website.com',
        'subject' => 'Web enquiry from {{fname}} {{lname}}',
      ])->logAction([
        'file' => kirby()->roots()->site().'/messages.log'
      ]);

    if (!$form->success()) {
      // This should not happen and is our fault.
      return Response::json($form->errors(), 500);
    }

    // Return code 200 on success.
    return Response::json([], 200);
    // return page('contact');

  }
]

JS

// Submit the form using AJAX.
var formData = $(this).serialize();
        $.ajax({
          type: 'post',
          url: $(this).attr('action') + '/postman',
          data: formData,
          success: function (response) {
            // You will get response from your PHP page (what you echo or print)
            console.log('success');
          },
          error: function(jqXHR, textStatus, errorThrown) {
            console.log(textStatus, errorThrown);
          }
        })

Does the log action do anything?

Have you checked if there are any errors in the console?

The log action is working successfully, which is great, it’s just the emails are not actually being sent.

No errors in the console either…

Can you send email on the server with Kirby’s email method (eg from a template)?

Hmm, no — I tried creating a new template, mail.php, and a content folder & text file with the simple Kirby sample code, but visiting the page still doesn’t seem to send the email. I also tried embedding it in a new route, but no luck.

But no error and the isSent() example returns true?

This did work! The page returned 1 and the email landed…so perhaps it has to do with Uniform’s emailAction?

<?php

$success = kirby()->email([
    'from'    => 'my@spfverifiedemail.com',
    'to'      => 'myotheraccount@gmail.com',
    'subject' => 'Welcome!',
    'body'    => 'We will never reply',
])->isSent();

?>

<div class="">
  <?= $success ?>
</div>

Hm, internally, the Uniform plugin also uses the email() method… Have you used the same sender and receiver as in your Uniform example?

If mails are sent but don’t get through, that is usually due to sender IPs being blocked or something like that. In search of a way for debugging I found this line

log stream --predicate  '(process == "smtpd") || (process == "smtp")' --info

which spits out smtp infos. Maybe you can run this from the command line on your server.

Sending mail is much more reliable if you use a provider to send your mail through.

Thanks so much for all of your help @texnixe, your debugging methods were extremely helpful. It turns out it was on the server side.

After discussing with my host, we finally managed to get it working by firstly adding the host’s server to the SPF records, but more importantly by updating the mail routing for the domain from ‘local’ to ‘remote’ — the domain’s nameservers are hosted elsewhere.

Hope that helps anyone else encountering a similar issue.