If you create a send mail action where the $to address is a string the following error is generated:
Warning: implode(): Invalid arguments passed in Drupal\rules\Plugin\RulesAction\SystemSendEmail->doExecute() (line 121 of rules/src/Plugin/RulesAction/SystemSendEmail.php).

The $to variable is assumed to be an array, but if you use the context_mapping to retrieve a node author's e-mail address it will be passed as a string

Comments

intrafusion created an issue. See original summary.

intrafusion’s picture

Status: Active » Needs review
StatusFileSize
new760 bytes

Patch to check is_array()

Anonymous’s picture

I tried your patch and it solved also my email problem. Thx for this patch

hsponner’s picture

Thanks, #2 patch working for me (sending mail to author/sender of Contact message)

eanushan’s picture

I'm running into the same scenario with context_mapping. The patch in #2 works for me as well. Can we mark this as RTBC?

admirernepali’s picture

Great, patch works. Would love to see if rules team applies the patches too.

intrafusion’s picture

Status: Needs review » Reviewed & tested by the community

Surely, 4 approvals for a one line change can be RTBC

jonathan1055’s picture

Hi intrafusion,
Yes it may seem that this should be committed, but as I understand it, the patch in #2 is is a specific fix which is a 'sticking plaster' and does not tackle the cause of the problem. We need to test that the patch in #2824348: Warnings when using token replacements in multiple string context parameters solves this error, as that is the more likely one to get committed.
Jonathan

fago’s picture

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)

Exactly - the passed context does not match the requirement. So the problem is in our context mapping code allowing us to do that or not taking care of the situation. We should work on this, so it will make the band-aid unnecessary.

With #2824348: Warnings when using token replacements in multiple string context parameters being committed, it should be possible to use the token replacement now instead?

gsquirrel’s picture

Am adding this here in case it helps anyone, i finally got an email to go to the author using twig token in direct input mode rather than using the data selector.
This is the token that worked {{ node.uid.entity.mail }}

tdnshah’s picture

How do we apply this patch?

jonathan1055’s picture

Hi tdnshah,
Here is the Drupal page on how to apply a patch for you general information. However, the proper solution to this issue will not actually involve this patch fix.
Jonathan

tr’s picture

I just tested this by creating a send mail action in response to an event. I used one e-mail address in the to: field, on a single line. I am using the current up-to-date Rules 8.x-3.x.

I did not see this error.

@tdnshah Are you seeing this error in the current 8.x-3.x-dev release?

brtamas’s picture

StatusFileSize
new990.74 KB

For 8.3.0-alpha4

tr’s picture

Thank you, but there are a lot of problems with that patch.

The patch 'adds' many files that already exist, for example .gitignore, .travis.yml, composer.lock, README.md, rules.module, etc. I didn't check in detail, but perhaps you have added the entire rules module to this patch?

There are some changes not related to this issue, for example in composer.json.

The patch also adds some files which should NOT be added, like LICENSE.txt, which doesn't belong in the repository.

It's hard for me to tell what code is part of your fix and what code is not.

tr’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)
jonathan1055’s picture

Just for clarity, the patch in #15 should be ignored. The patch in #2 can be used if you need an immediate fix before #2723259: Allow single-valued data selector input to be passed as an array for 'multiple' context fields is completed.

tr’s picture

@jonathan1055: Correct, #15 should not be used. Thanks for hiding that.

I would also suggest #2 should not be used. That patch "fixes" bad input just for the SystemSendEmail action, and doesn't do anything to avoid the same issue where it occurs elsewhere, notably in the "Node is of type" action. The patch in #2 just changes the SystemSendEmail action to accept either an array or a string. That sort of type fluidity is what we are trying hard to avoid - Rules works when we can impose strong typing on top of PHP, so that we always know what type the passed data is, that way we always know how to use it and don't need to scatter isset(), is_array() etc. everywhere throughout the code when we try to use the data.

Instead of the patch in #2, this issue can be avoided by just passing the correct data type. To quote myself from #2799569-17: Cannot use account.mail.value as Send To data selector for action SystemSendEmail (with some minor changes to make it relate to the above discussion):

Specifically, node.uid.entity.mail.value is a string object, so Rules is correct in not allowing that to be used in the DATA SELECTOR for this action as the Sendmail "To:" address, because "To:" requires an array ('list' datatype) as input. (Multiple e-mail "To:" addresses are allowed!). When using the data selector, strict typing is mandated to ensure that the data may be used.

The "workaround" of using {{node.uid.entity.mail.value}} in DIRECT INPUT mode isn't really a workaround, it's the correct way to do things in this case. If you don't have an object that contains a 'list' of 'email' objects, then you can't use the data selector but you can/should use a token in direct input mode. Using direct input mode, the token {{node.uid.entity.mail.value}} resolves to a text string, the Sendmail "To:" address is a textarea which accepts one text string per line. So entering this token in direct input mode is the correct and proper way to configure the SystemSendEmail action in this case.

I consolidated a number of issues into #2723259: Allow single-valued data selector input to be passed as an array for 'multiple' context fields because they all revolve around this same problem of assigning a single-valued variable chosen in the data selector to a context parameter that accepts only an array of values. The actions where this issue show up are different, but they all have the same cause and they all have the same solution or "workaround" described in the previous paragraph above.

jonathan1055’s picture

Thanks TR for taking the time to explain the background reasoning, that is really helpful.