Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Send email in own cli plugin using the email plugin

Solved by Michael Hadorn View solution

Started by Michael Hadorn 9 years ago · 2 replies · 1004 views
9 years ago

Hi there

I would like to send an email via the email plugin (https://github.com/getgrav/grav-plugin-email) 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')
->setFrom($from)
->setTo($to);
$sent = $this->grav['Email']->send($message);

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

But I get this Error:
[Pimple\Exception\UnknownIdentifierException]
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!
Michael

last edited 11/09/17 by Michael Hadorn
9 years ago

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
9 years ago Solution

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

Used:

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

👍 1
last edited 11/09/17 by Michael Hadorn

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 44 1 week ago
Plugins · by Xavier, 4 weeks ago
2 54 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1181 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 48 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 73 2 months ago