Want to send email to a user account. Have the user account object, it of course has the account.mail.value which is the account email address.

However, the Rules interface complains that it's expecting a List data type and was given an Email data type when I try to assign account.mail.value as the Send To via a data selector. This is for the System Send Email action.

What is the proper way to send email to a user account?

The exact error message in the interface is:

Expected a list data type for context Send to but got a email data type instead.

The second is: why is message a text field??

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

john.oltman created an issue. See original summary.

john.oltman’s picture

Title: Cannot use account.mail.value as Send To data selector » Cannot use account.mail.value as Send To data selector for action SystemSendEmail
alex030’s picture

Same problem with node.uid.entity.mail.value on event "After updating content (rules_entity_update:node)".
I want to send an email to the author of a node after update.

With node.uid.entity.mail.value i become the same error message.
With node.uid.entity.mail i can save the rule with no error but if a node is updated no email is send.
So i checked out the logs and saw the message "Successfully sent email to" but i know that it should be for example "Successfully sent email to test@test.com".
The account email is missing in the log message and maybe if the rule action is processed?

alanpeart’s picture

Was this ever resolved? I am getting the same problem. I want to email a user after they save a node, and whichever value I choose has a problem. If I choose node.uid.entity.mail.value I cannot save the rule (error message as above) and if I choose node.uid.entity.mail I can save the rule but the email is not sent.

EAnushan’s picture

I'm experiencing the same issue. I've tried loading my rule via configuration, and the context_mapping doesn't accept lists, so I can't provide a list of data selectors for the to field in the SystemSendEmail action.

Seems like the patch in https://www.drupal.org/node/2827183 resolves the issue.

lindsay.wils’s picture

I have encountered this same issue acting on content moderation actions.

As a workaround, you can use the data selection string as a token in the direct input textarea.

Switch to data selection and get your string, in my case content_moderation_state.uid.entity.mail.value

Switch to the direct input mode and paste the string with double braces {{content_moderation_state.uid.entity.mail.value}} to use the token replacement API.

blacklabel_tom’s picture

Hi All,

Confirmed the fix in #6 worked for me as a workaround.

Cheers

Tom

yuseferi’s picture

The same error even testing #6 with {{comment.entity_id.entity.uid.entity.mail.value}}, but not working , my drupal version is 8.5.4

TR’s picture

Status: Active » Postponed (maintainer needs more info)

@zhilevan: Did you enter {{comment.entity_id.entity.uid.entity.mail.value}} in direct input mode? See #2919056: Comment Notify Node Author.

Please export your reaction rule configuration and post it here so we can see exactly what you are doing so we can try to reproduce it.

dureaghin’s picture

Hi All,

If someone is looking for a solution for Webform, this works: {{webform_submission.uid.entity.mail.value}}

You may need to apply this patch too: 2827183

Thanks,
Alex.

jmuzz’s picture

This does not seem to work for the flag module. I didn't expect the author entity here to be any different, but I tested posting comments on the same node using these suggestions and that does work for me.

For flagging what I tried was {{ flagging.flagged_entity.entity.uid.entity.mail.value }} and a few similar things. What that gave me was:

Recoverable fatal error: Object of class Drupal\Core\Field\FieldItemList could not be converted to string in Drupal\syslog\Logger\SysLog->log() .

Which I guess is a problem trying to record the error. On next page load I get what I think is the "real" error. To summarize, somehow what this sends to the first argument of Drupal\rules\Plugin\RulesAction\SystemSendEmail->doExecute() is not a string, not an array, but an object (FieldItemList).

Still trying to find a workaround.

TR’s picture

@jmuzz: You need to export your Rule and post it here.

jmuzz’s picture

@TR I mean these are just hacks for during the development of the typed data solution. All of this is going to be obsolete. I don't want to distract from the real development effort. I will post a solution if I find it.

Here's the export but to even add the condition in it you have to butcher the module with the hack I posted in the other thread (not recommended).

jmuzz’s picture

And here it is, in Drupal\rules\Plugin\RulesAction\SystemSendEmail.php .

-     protected function doExecute(Array $to, $subject, $message, $reply = NULL, LanguageInterface $language = NULL) {
+    protected function doExecute($to, $subject, $message, $reply = NULL, LanguageInterface $language = NULL) {
+    if (is_object($to)) {
+      $to = $to->getValue()[0]['value'];
+    }

This will allow messages to be sent to the owner of a flagged node with

{{ flagging.flagged_entity.entity.uid.entity.mail.value }}

Use only as a last resort, people. This isn't going to be committed.

TR’s picture

Thanks for the export - that clarified for me exactly what you're doing and which components you're using.

So #11 - #14 is unrelated to this issue, and is not a problem with Flag either. What you're seeing is the bug in #2927132: List of conditions includes Core conditions which do not work with Rules. Specifically, Rules conditions are not the same as core Drupal conditions, and you can't use core Drupal conditions in Rules. The CTools module appears to provide two core Drupal conditions - those can't be used by Rules. In general, anything that shows up in the "Other" category when you're looking at the conditions is a core Drupal condition and can't be used in Rules. Your export shows me you're trying to use the CTools "entity_bundle" condition. That's why you are getting errors. As you guessed in #2695149: Error when adding condition "Contact form bundle", if you use the Rules condition "Entity is of bundle" instead, then your rule should work properly.

berenddeboer’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Needs work

I think the later comments have obscured the fact that trying node.uid.entity.mail.value does not save.

See my rule where I used the above work around:

uuid: 31c68060-fc2a-41dd-9a76-6fce1ff97ca4
langcode: en
status: true
dependencies: {  }
id: notify_author_when_editor_comments
label: 'Notify author when editor comments'
events:
  -
    event_name: 'rules_entity_insert:node'
description: ''
tags:
  - ''
config_version: '3'
expression:
  id: rules_rule
  uuid: 6314c3e0-faa8-464d-8911-4e1f558fcff0
  conditions:
    id: rules_and
    uuid: 81d06213-2696-449b-90e2-2084b08cfe8a
    conditions: {  }
  actions:
    id: rules_action_set
    uuid: c2ae9649-87d1-4cdd-b7a6-6a7cbac8bc97
    actions:
      -
        id: rules_action
        uuid: 46609b74-42b0-40ca-b3f4-3980c07706a1
        context_values:
          to:
            - '{{node.uid.entity.mail.value}}'
          subject: 'New comment on your paper'
          message: 'Hello,  An editor has posted feedback on your paper submission'
          reply: ''
          language: ''
        context_mapping: {  }
        context_processors:
          to:
            rules_tokens: {  }
          subject:
            rules_tokens: {  }
          message:
            rules_tokens: {  }
          reply:
            rules_tokens: {  }
          language:
            rules_tokens: {  }
        provides_mapping: {  }
        action_id: rules_send_email
TR’s picture

It's not obscured, at least to me. The issue is still open and marked as a bug because it's still a problem. However, it is really a UI/UX bug more than anything.

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 UI bug would be that node.ui.entity.mail, which is an 'email' datatype (note, without .value at the end), SHOULD be allowed - the "To:" field should accept either an array or a single object of the 'email' datatype when in data selector mode. It does not currently - it requires an array, which we don't have.

The "workaround" 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, and text strings are the ONLY things allowed in direct input mode.

So the sendmail action works, but the UI is fragile and unhelpful and doesn't deal gracefully with errors. Completion of the UI is milestone 3 out of 3 tasks needed to be finished before Rules goes into beta. If getting this done is important to you, please help out with the development or support the developers!

imperator_99’s picture

Hi all,

I had a bit of trouble getting this to work too, but it turns out that I was putting spaces around the inside of my token, thinking it was like any old Twig token: {{ node.uid.entity.mail.value }}

But @TR is exactly correct, and the actual token is {{node.uid.entity.mail.value}}. Once I got rid of the spaces, it all worked perfectly.

Cheers,
Jesse.

TR’s picture

It should work just fine with spaces - there are automated test cases to ensure that spaces are ignored.

n4r3n’s picture

I hope this patch helps.

TR’s picture

VasyOK’s picture

After applyng patch #20 on latest dev of Rules its impossible to add action.

shradhaG’s picture

Hi,

I can confirmed the fix in #6 worked for me as a workaround. You need to switch to direct input mode and add your mail value in
{{node.field_mail.value}} in this format. And you are done. :)

Thanks.