Email provider mailgun

Hello,

I did not understand how to manage mailgun on this page?

I created a plugin in a mailgun folder and copied/pasted the example (I put the correct information)

I also tested with Sendgrid.

Is there something to do in the config/config.php?

Error class not found

I think you need to install the Mailgun library in you project via composer as in the instructions here.

most importantly this bit above the example code in the kirby docs:

require 'vendor/autoload.php';
use Mailgun\Mailgun;

I think its saying class not found because the example in the kirby documentation has lines like

$mg = Mailgun\Mailgun::create('key-example');

which means its trying to use the class in the mailgun library from mailgun but your code currently doesnt have that library loaded into it (this is what the use statement does).

It will be a smiliar story for Sendgrid.

The kirby docs could probably be made a bit clearer as i think they assume you already have the respective libraries installed.

Thanks, I’ll give it a try as I haven’t done that yet.

I have to modify the index.php file at the root?

<?php

require 'vendor/autoload.php';
use Mailgun\Mailgun;

require __DIR__ . '/kirby/bootstrap.php';

echo (new Kirby)->render();

No, this would go in your plugins index.php, not the main Kirby PHP.

Similar to the pluginkit example with composer GitHub - getkirby/pluginkit at 3-composer

so the class in the example in the documenation would go in the seperate file in the src folder, and this part:


Kirby::plugin('my/sendgrid', [
    'components' => [
        'email' => function ($kirby, $props, $debug) {
            return new SendGridProvider($props, $debug);
        }
    ]
]);

goes in the plugins index.php

Ok, so I should install composer to create the vendor folder and call the mailgun repository?

yes you need composer installed and use the the official pluginkit from my previous post as a basis of the plugin. you need install the mailgun library in your plugin folder, not the main Kirby folder. this will put the mailgun library in a Vendor folder INSIDE your plugin.

From there you can load the mailgun library into the file with the class in it, and go from there.

To be honest i have never put the class in the pluigns main index.php, i use a serperate file for that but i guess it should work. its just tidier to put the class in a seperate file.

Wait a minute, what do you want to do? If you want to use Mailgun as your SMTP email provider, you don’t need the SDK. But include your credentials in the /site/config/config.php as described in the docs you linked above.

But how does:

Mailgun\Mailgun::create

work without the SDK? im confused :slight_smile:

Ok I’m waiting.

Before kirby 3.6.0 I had put this in site/config/config.php

    'email' => [
        'transport' => [
            'type' => 'smtp',
            'host' => 'smtp.eu.mailgun.org',
            'port' => 587,
            'security' => 'tls',
            'auth' => true,
          
            'username' => 'postmaster@mg.domain.com', 
            'password' => 'KEY', // provided in the domain information
            ],
        ],

It was working fine I just want it to work again with the new method? Is it obligatory?

Ca envoi les mails avec PHP MAILER ?

That’s still the same.

It doesn’t.

But that’s a different story. The SDK is for using the API.

Which new method? Transport configuration is still the same: Emails | Kirby CMS

ok the mailgun example in the Kirby docs does this:

class MailgunProvider extends Kirby\Email\Email
{
    public function send(): bool
    {
        $mg = Mailgun\Mailgun::create('key-example'); // For US servers
        $mg = Mailgun\Mailgun::create('key-example', 'https://api.eu.mailgun.net'); // For EU servers

        return $mg->messages()->send('example.com', [
          'from'    => $this->from(),
          'to'      => current(array_keys($this->to())),
          'subject' => $this->subject(),
          'text'    => $this->body()->text()
        ]);
    }
}

I dont understand where Mailgun\Mailgun is coming from - is that something built into Kirby or reliant on the Mailgun php library being installed?

Well, that’s about creating an Email component. But that’s not needed if you just want to use PHPMailer with those providers.

No, that’s not built into Kirby.

That’s why I was asking about the use case.

ah ok i see… just using Mailgun for SMTP doesnt need all that jazz. :trumpet:

I guess it’s better if the emails go through mailgun directly?

When I received messages before it displayed this:

Sent by: mg.domain.fr
signed by: mg.domain.fr
security: Standard encryption (TLS) Learn more

So I assumed that mailgun was no longer taken into account? Because in my mailgun account I no longer have a log.

OK but lets step back a second. What are you using mailgun / sendgrid for? These are designed for high volume email sending, beyond what most web hosting companies will allow from a standard SMTP mail account on your hosting.

If this is low traffic site and your just using this for like a contact form or something you can probably do away with mailgun / sendgrid. Check the terms of your hosting account. Most will say something like “up to 500 emails a day per email account” in the small print.

More than that, regularly, will likely get you flagged as a spammer. If your volumes are higher than that then yes, you do need service like mailgun/sendgrid, but other other wise you can just use a normal SMTP account on your webhosting to send mail from kirby.

I only use it for transactional email (I don’t exceed 50 emails / month I think).

The problem is that the sites are on shared hosting with thousands of customers.

Theoretically these companies have reliable ip addresses which limit the arrival in spam because their ip are known to mail servers (gmail, yahoo, hotmail …)?

It is for this use. But maybe you are right. Any good advice is worth taking.

I tested again with the documentation and it seems to work again (I see the mailgun signature and the encryption again) Emails | Kirby CMS

before

    'email' => [
        'transport' => [
            'host' => 'smtp.eu.mailgun.org',
            'port' => 587,
            'security' => 'tls',
            'auth' => true,
            'username' => 'postmaster@mg.domain.com',
            'password' => 'KEY',
            ],
        ],

After (correct)

    'email' => [
        'transport' => [
            'type' => 'smtp',
            'host' => 'smtp.eu.mailgun.org',
	    'port' => 587,
            'security' => 'true',
            'auth' => true,
            'username' => 'postmaster@mg.domain.fr',
            'password' => 'KEY',
            ],      
        ],

add 'type' => 'smtp', and change 'security' => 'true',

Thanks @texnixe It was my fault. I had already changed several times in more than a year the smtp config because sometimes it didn’t work anymore.

1 Like

But the shared hosting and thousands of customers doesnt matter. What does matter is what they have contractually aggreed to provide you with. From experience, ~50 emails a month is million miles from any limit ive seen imposed but a hosting company, shared or not.

1 Like