How to debug sending mail?

Hi,

I am using Uniform v3 to create a contact form. The input from the form is both logged to a file and send by mail (with PHP mail).

On my local server (MAMP) it works fine. The message is logged and a mail is send.
But on my remote server no mail is send (or at least I receive nothing). No error is displayed. The form just goes to the success message.

Since there are no error messages and the messages do get logged, I have no idea to start with debugging.

Any ideas where I could look to fix this? Thanks!

I wouldn’t rely on PHP mail function. Many hosting providers disable it to prevent their server from being used to send spam and emails are easily treated as junk by email clients.

I’ve integrated the PHP Mailer library (SMTP) and also used Mailgun Kirby driver with great success in my projects. Check out all the drivers available.

Yes, but if you are only using it to send mails to yourself from a contact form and have made sure that it works, then the PHP Mailer library is probably overkill.

But I agree when it comes to sending mails to others from your server.

Thanks.

So I have tried Mailgun. I already had an account from a previous iteration of the site so that was quick to do. I have used the sample code to send something from the command line using Curl. That works. Both on my local machine as well as on the server.

The form does not work however. When clicking the send button, the page reloads and the form appears again with the same contents. Again both on the local and the remote server. And again the information does get written to the log file.

But if I swap the EmailActions around so that the email goes first, nothing gets written to the log file. So something gets stuck there.

Nothing shows up in my PHP error log.

This is what I have in the controller (obviously I have replaced some info)

if (r::is('POST')) {
        $form->emailAction(['to' => 'me@mydomain.com',
                            'from' => 'mailgun@mydomain.com',
                            'subject' => 'Site suggestion',
                            'service' => 'mailgun',
                            'options' => array(
                              'key'    => 'my-mailgun-key',
                              'domain' => 'mydomain.com')
                          ])
              ->logAction(['file' => kirby()->roots()->site().'/messages.log']);
    }

The form and logic are based on the basic example provided in the Uniform documentation.

Any ideas are welcome. In particular on how to log what is happening here. Since there are no (error) messages anywhere I have no idea what is going wrong.

Try to put this piece of code into a template for testing:

<?php $email = email(array(
  'to'      => 'me@example.com',
  'from' => 'postmaster@example.com',
  'subject' => 'Hallo',
  'body'    => 'Hey, this is a test email!',
  'service' => 'mailgun',
  'options' => array(
    'key'    => 'key-xxx',
    'domain' => 'my@example.com'
  )
));

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

This should output some sort of error.

I got it working!

I stripped everything down to the basic example. Once that was working I added Mailgun.

It turns out that I made some modifications to the form that suppressed certain errors. Including a ‘missing API key’ error and then I found that I was supplying the options incorrectly.

Once that was fixed it worked both locally and on the server.

As a reference this is the correct code

if (r::is('POST')) {
        $form->emailAction(['to' => 'me@example.com',
                            'from' => 'mailgun@mydomain.com',
                            'subject' => 'Site suggestion',
                            'service' => 'mailgun',
                            'service-options' => ['key' => 'my-mailgun-key',
                                                  'domain' => 'mydomain.com']
                          ])
              ->logAction(['file' => kirby()->roots()->site().'/messages.log']);
    } 

@texnixe: Thanks for your sample code. I just missed it while I was typing my message.