Uniform + Mailgun

Hi,

I’m trying to set up the Uniform plugin with Mailgun as the external service. Although configuring Mailgun doesn’t seem to work. In my config.php I added the following lines.

c::set( 'email.use', 'mailgun' );
c::set( 'email.mailgun.key', 'my-very-secret-key' );

Later I added:

c::set( 'email.service', 'mailgun' );

But none of them seems to have some effect. References to the Uniform plugin docs on this forum are outdated. Also the uniform, docs states something about creating a plugin with email settings, but I can’t find an example of the structure. Can anyone help me out?

Have you set the service and service options in your controller? http://kirby-uniform.readthedocs.io/en/v2.3.0/actions/email/

I’m not performing the email action in a controller but in a custom route. Something along the lines of:

c::set( 'routes', [[
  'pattern' => 'some/(:num)/(:any)'
, 'method'  => 'GET|POST'
, 'action'  => function( $foo, $bar ) {
    
    // ... fetching data ...
                                   
    $form = new Form([
      'email' => [
        'rules'   => [ 'required', 'email' ]
      , 'message' => 'Email is required'
      ]
    , 'message' => []
    ]);

    if ( r::is( 'POST' ) ) {
      $form->emailAction([
        'to'   => 'to@domain.com'
      , 'from' => 'from@domain.com'
      ]);
    }

    return [ 'foobar', [
      'uri'  => $_SERVER['REQUEST_URI']
    , 'page' => $page
    , 'form' => $form
    , 'csrf_field' => csrf_field()
    , 'honeypot_field' => honeypot_field()
    ]];
  }
]]);

Well, I’m not familiar with the new version of the plugin yet and can’t find how to set up the former service options in the docs, @mzur?

You configure the email service and the service options in the email action like so (similar to v2.3):

$form->emailAction([
   'to' => 'to@domain.com',
   'from' => 'from@domain.com',
   'service' => 'mailgun',
   'service-options' => ['key' => 'my-very-secret-key'],
]);

Where do you got that method of selecting an email service through the config from? I’ve never seen that before.

Can you show me the outdated links to the Uniform docs in this forum? I’ll try to make them work again.

Thanks, that makes sense. Just tried to configure the service that way but without luck. Still getting the “The email could not be sent” message. I tried debugging from the Kirby source and from the plugin source, but that didn’t tell me a lot. Also, I’m running PHP’s built-in server so all error info is displayed in the terminal and there is none.

The forum post is this one (second link):

Which works but once you get to the docs you’ll have to search again for the right topic.

As for the config, I found it here:
https://getkirby.com/docs/cookbook/deprecated/email-plugin

Would be great to be able to define those things in the config rather that at the action, and have the plugin listend to the global config. Those things are generally application-wide.

The last link has already been deprecated, where did you find that? Guess we should remove it completely.

The Mailgun service requires the domain option, too, did you specify that?

There is nothing I can do about the link to the docs. Although the GitHub wiki now links to the documentation at Read the Docs, the content is exactly the same and you can still find the documentation on the service and service-options action options there. The link points to the documentation of Uniform v2.3, though, and you are using v3 (but the email action options are the same in this case).

@texnixe
I found it through a Google search I suppose, looking for Kirby email configuration.

@mzur
Yes, I tried domain, host and hostname, all without luck. My main issue now is the rather ambiguous error message (The email could not be sent). Any pointers as to why would be very welcome and make it a lot easier to debug. Or even an exception telling me where the error originated. I already tried locating the message using a text search throughout the code to debug it in the source, but nothing there either. I’ll have to dig a little deeper I guess.

This is the line of code you are looking for (in kirby/vendor/getkirby/toolkit/lib/email.php). You can try and debug the content of the response object.

@mzur
Thanks, found that already, though :slight_smile:
In the end, I decided to write something small myself because I have the required code available in other projects.

@texnixe
Here is more of that email config code in the current docs:
https://getkirby.com/docs/cheatsheet/helpers/email

Is email configuration in the config.php indeed deprecated?
If not, how can I configure mailgun globally, so I don’t need to provide the API key and domain every time I send an email?

Write something yourself to replace the email service or to replace Uniform? I’m asking because I want to eliminate all possible bugs in Uniform v3 beta.

Finally found it! Sending emails from localhost using Mailgun is blocked because a valid SSL certificate is required. On the production server with an SSL certificate, it works as expected. There is probably a workaround to use it without the certificate, but for me, it’s ok if it works in production.

Thanks for your help anyway.

1 Like