2

I am including a sprout form on my template like this:

...
{{ craft.sproutForms.displayForm('myForm') }}
...

I am submitting the form via ajax, and when the form is submitted I am trying to set the recipient in Sprout Email to the form's container author.

{author.email}

So it's not the author of the submitted form/entry, it's the author of the entry that the form is sitting on that I'm trying to access.

I'd really like to avoid passing in an email from a hidden input if at all possible. Thank you for any suggestions!

Update

For example, I am on the page a.html which has an author of damon@email.com. On a.html there is a contact form. When a user submits the contact form, I want a confirmation email sent tothem, and then an email sent to the author of the entry that the contact form is on. The same contact form can live on any page, and each page could have different authors.

a.html has an author of damon@email.com b.html has an author of foo@email.com

In the above case, a confirmation email needs to go whomever filled out the form, and then a notification email to each of the entries respectively.

Update 2

I am getting closer I think, but struggling to dynamically set the recipient.

Here is my form:

...
{% set form = craft.sproutForms.form('myForm') %}
<input type="hidden" name="action" value="sproutForms/entries/saveEntry">
<input type="hidden" name="handle" value="{{ form.handle }}">
<input type="hidden" name="entry" value="{{ entry.id }}">
<input type="hidden" name="author" value="{{ entry.author.id }}">

Then in my plugin I am doing this:

craft()->on('sproutForms.onBeforeSaveEntry', function (Event $event) {
    if (craft()->request->isSiteRequest()) {
        if (craft()->request->getPost('author') != '') {
            $owner = craft()->users->getUserById(craft()->request->getPost('author'));

            if ($owner) {
                if ($event->params['isNewEntry']) {
                    $entry = $event->params['entry'];

                    $entry->setContentFromPost(array(
                        'ownerEmail' => $owner->email, // damon@email.com
                    ));

                   // Set {ownerEmail} as recipient in notification?
                }
            }

In my ajax response I am seeing:

{"errors":[]}

In the sproutemail.log I am seeing:

[error] [plugin] No recipients found.

In the recipient field I have tried {ownerEmail} and {object.ownerEmail} both having no success.

I have also tried setting field values just in the off chance...

{% do craft.sproutForms.addFieldVariables({ entry: entry }) %}

The whole purpose of this is that I am trying to avoid putting a person's email in a hidden input. Just so that if a user of the site viewed the source the author's email wasn't right out in the open.

  • When you say "form's container author" are you referring to an author of a Channel Entry where the form is displaying on the same page? If that's the case, I can respond in more detail but the EntryModel is not going to be available to use as a dynamic recipient unless you explicitly make it available using a form field and something like Sprout Fields Invisible, Hidden, or Email Select fields. – Ben Parizek Mar 29 '17 at 12:43
  • Hi Ben - you are right. I have updated my question with a bit more detail. – Damon Mar 29 '17 at 13:55
1

This can be done using an Invisible Field and the addFieldVariables tag. Define the value you want as your email in your template:

{% do craft.sproutForms.addFieldVariables({
    'secretEmailAddress': entry.author.email
}) %}

Set the Invisible Field to target that value:

{secretEmailAddress}

And then customize the Notification Email Recipients field to target the handle of the Invisible Field:

{invisibleFieldHandle}

Another example of doing this dynamically from an Entry with a Form Relations field has been posted in response to another question here: https://craftcms.stackexchange.com/a/29553/115

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.