directus / v8-archive

Directus Database API — Wraps Custom SQL Databases with a REST/GraphQL API
https://docs.directus.io/api/reference.html
507 stars 203 forks source link

How do you send an email from a custom hook? #2130

Open bskimball opened 4 years ago

bskimball commented 4 years ago

The example shows using a notify function when an item is created. How should that notify function look if it were to include the environment variables provided?

I'm trying this

<?php

return [
    'actions' => [
        'item.create.contact_requests' => function ($data) {
            $container = \Directus\Application\Application::getInstance()->getContainer();
            $mailer = new \Directus\Services\MailService($container);
            $mailer->send([
                'type' => 'html',
                'subject' => 'New Contact Request',
                'to' => 'brian.kimball@bdkinc.com',
                'body' => implode(" ", $data)
            ]);
        }
    ]
];

but I get a validation error [2020-04-05 01:40:35] api[_].ERROR: Swift_RfcComplianceException: Address in mailbox given [] does not comply with RFC 2822, 3.6.2. in /var/directus/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php:345

bskimball commented 4 years ago

FWIW - I could not get the MailService working correctly. I also tried the sendWithLayout functions provided by Mailer.php, but got the same error.

I ended up just using the Swift Mailer library directly. I did not need the twig templates, so this was not an issue.