V8-archive: V8.2.0 hooks MailService send function get error when create item from public

Created on 12 Dec 2019  路  5Comments  路  Source: directus/v8-archive

Bug Report

Steps to Reproduce

  1. Add hooks setting in project php config file.
'hooks' => [
    'actions' => [
        'item.create.messages' => function ($data) {
            $container = Application::getInstance()->getContainer();
            $mailService = new MailService($container);
            $mailService->send([
                'type' => 'html',
                'subject' => "subject",
                'body' => 'new message created'',
                'data' => [],
                'to' => [
                    [ 'name' => 'Gavin Luo', 'email' => '[email protected]' ]
                ],
            ]);
        },
    ],
],
  1. Create message item from public post request, and error
{
  "error": {
    "code": null,
    "message": "Address in mailbox given [] does not comply with RFC 2822, 3.6.2.",
    "class": "Swift_RfcComplianceException",
    "file": "\/var\/www\/vendor\/swiftmailer\/swiftmailer\/lib\/classes\/Swift\/Mime\/Headers\/MailboxHeader.php",
    "line": 345
  }
}
  1. Login to Admin APP, and create a new message item, email sending hook worked.

Expected Behavior

Also send email when on public api request to create message item.

Actual Behavior

Address in mailbox given [] does not comply with RFC 2822, 3.6.2. error

Other Context & Screenshots

Public role create permission has been setted and the message item can create normally, just email sending now working.

Technical Details

  • Device: Desktop
  • OS: Windows 10 with Docker Desktop
  • Web Server: Caddy 1.8
  • PHP Version: 7.4
  • Database: MySQL 8
  • Install Method: cloned v8.2.0 tagged branch, manual build and install
bug

All 5 comments

@bjgajjar I found the problem is that the send function setFrom is false.

Why we send false to setFrom and use default from from setting must have a Admin permission?

Can we change it to use a from key from data to set mail from and if it not exist, we use the default mail from and without admin permission?

Or is there an official recommended best practice for sending emails for extension?

reference commit https://github.com/directus/api/commit/ef40463a88ca8379188b596439da5bcfaadf5e9f

https://github.com/directus/api/blob/a3883473cfcbc1dae2fa015c9c808df60599f660/src/core/Directus/Services/MailService.php#L32-L43

looks like https://github.com/symfony/mailer is great for me, maybe I can use it and wrapped as an extension.

@everyx - We really appreciate you taking the time to debug an issue. As you mentioned;

Can we change it to use a from key from data to set mail from and if it not exist, we use the default mail from and without admin permission?

It is already doing that. from is set with getFrom method. And the getFrom method is doing exactly the same which you have mentioned. Please check the code here

maybe I can use it and wrapped it as an extension.

Yeah. Please do it and feel free to send the PR also :)

Just close this issue and it's not a bug. :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maettyhawk picture maettyhawk  路  3Comments

ondronix picture ondronix  路  3Comments

vuhrmeister picture vuhrmeister  路  3Comments

gitlabisbetterthangithub picture gitlabisbetterthangithub  路  3Comments

andgar2010 picture andgar2010  路  3Comments