Send email in own cli plugin using the email plugin

Hi there

I would like to send an email via the email plugin ( inside a cli in my own plugin.

My problem is that the email plugin is not loaded in my cli context. I know that I could add and configure an own mailer but this is ugly.

Similar code is working in my plugin:

$message = $this->grav[‘Email’]->message($subject, $content, ‘text/html’)
$sent = $this->grav[‘Email’]->send($message);

In the cli context I use:
$email = Grav::instance()['Email'];

But I get this Error:
Identifier “Email” is not defined.

So, how can I load the email plugin with his configuration (I’m using a SMTP server).

Best thanks for any reply!

That email object doesn’t get initialized until the plugins are initialized. By default Grav CLI commands do not do this. Probably the easiest way would be to simply include the Email class from the email plugin, instantiate it, then call the send method.

1 Like

Yes, get it. Thx @rhuk for the motivation.


/** @var EmailPlugin $emailPlugin /
// load plugin & init (init will add the email property to the grav array entry)
$emailPlugin = new EmailPlugin(‘Email’, Grav::instance());
* @var \Swift_Message $message */
$message = Grav::instance()[‘Email’]->message($subject, $content, ‘text/html’);
$message->setFrom($fromAddr, $fromName)
->setTo($toAddr, $toName)
$sent = Grav::instance()[‘Email’]->send($message);

1 Like