Uniform send custom reply

Hi, i am hsing uniform plugin and i need to send second email to the client. Not the copy but the thank you email.
Its tragic. Of course i know shit about php. So i ask for help of this cool comunity.

Uniform actions:

                array(
                    '_action' => 'email',
                    'to'      => (string)$site->formrecipient(),
                    'sender'  => (string)$site->formsender(),
                    'subject' => 'Client message',
                    'snippet' => 'uniform-email-contact'
                ),
                   array(
                    '_action' => 'email', 
                    'to'      => a::get($form, '_from'), 
                    'sender'  => (string)$site->formsender(),
                    'subject' => 'Thank you Client!',
                    'snippet' => 'uniform-email-thanks'
                ),

This is the problematic part

'to'      => a::get($form, '_from'),

I asume i cant get the value at initialisation. But how do ido that.

THANKS

That would be something like get('_from').
get() is a Kirby helper that returns the GET/POST value.

By the way: Please format your code blocks using three backticks at the start and end. I have done it for your post. You can click the pencil icon of your post to see how that syntax works.

Yes.

It works, thanks!

What was the final code used for the controller? I’m encountering the same problem and get('_'from') doesn’t seem to work for me. I have a list of allowed-recipients and that e-mail action works perfectly, but the user confirmation / thank you e-mail just below it does not work (invalid to: address).

Here’s my code:

            array(
               '_action' => 'email-select',
               'sender'  => 'example@example.com',
               'subject' => 'Web Contact from {name}',
               'snippet' => 'uniform-email-table',
               'replyTo' => isset($options['replyTo']) ? $options['replyTo'] : a::get($form, '_from'),
               'allowed-recipients' => array(
                  'example1'     => 'example1@example.com',
                  'example2' => 'example2@example.com',
                  'example3'  => 'example3@example.com',
                  'example4' => 'example4@example.com'
                  )
               ),
            array(
               '_action' => 'email', 
               'sender'  => 'example@example.com',
               'subject' => 'Thank You For Contacting Us, {name}!',
               'to' => 'get('_from')',
               'message' => 'Thank you for contacting us today. We will be in touch soon!'
            )
         )

Thanks for any help you can provide!

You’ve got quotes around get('_from'), try removing them.

Thanks, @texnixe- removing the quotes around get('_from') worked like a charm.

One more question- I’d like to customize the sender in the second e-mail message. Depending on what department the user chooses in the form, can I customize the sender e-mail address?

It would be something like:

'allowed-senders' => array(
                  'example1'     => 'example1@example.com',
                  'example2' => 'example2@example.com',
                  'example3'  => 'example3@example.com',
                  'example4' => 'example4@example.com'
                  )
'sender' => {allowed-senders},

How can I make this concept work? This would be super useful so the user has a direct e-mail address to reply to in his / her e-mail confirmation.

Thanks!

Is this what your are looking for: https://github.com/mzur/kirby-uniform/wiki/Action:-Email-select

In a sense, yes.
##First e-mail (To Allowed Recipients)
The first e-mail is directed to a recipient based on what the user selects in the form- allowed-recipients. Works perfectly as is in the first code snippet I posted above ('action' => 'email-select').
##Second e-mail (To user from Allowed Senders)
The second e-mail should be directed to the user (this works with get('_from')) but it should have a different sender depending on what department the user selects in the form- whatever e-mail address is the allowed-recipients should also be the sender of this e-mail- something like allowed-senders. That means that the allowed-recipients value is the same as allowed-senders and the user will get an email confirmation directly from the allowed-senders value and can reply to the right department without the e-mail going to a generic inbox.

What is the best way to go about this? I also set up another email-select action, but can’t get the allowed-senders concept to work.

Thanks so much for your help!

This is what I am thinking about adding to \plugins\uniform\lib\UniForm.php to make this concept work.

/*
 * Action Choose from multiple senders who should send the user confirmation by
 * email.
 */
uniform::$actions['email-select'] = function($form, $actionOptions) {
    $allowed = a::get($actionOptions, 'allowed-senders');

    if (!is_array($allowed))
    {
        throw new Exception('Uniform email select action: No allowed senders!');
    }

    $sender = a::get($form, '_sender');

    if (!array_key_exists($sender, $allowed))
    {
        return array(
                'success' => false,
                'message' => l::get('uniform-email-error').' '.l::get('uniform-email-select-error')
        );
    }

    unset($form['_sender']);
    unset($actionOptions['allowed-senders']);
    $actionOptions['_from'] = $allowed[$sender];

    return call_user_func(uniform::$actions['email'], $form, $actionOptions);
};

I will try it now and report back!

Got a fatal error when attempting to implement this change in UniForm.php:

/*
 * Action Choose from multiple senders who should send the user confirmation by
 * email.
 */

uniform::$actions['email-select'] = function($form, $actionOptions) {
    $allowed = a::get($actionOptions, 'allowed-senders');

    if (!is_array($allowed))
    {
        throw new Exception('Uniform email select action: No allowed senders!');
    }

    $sender = a::get($form, '_sender');

    if (!array_key_exists($sender, $allowed))
    {
        return array(
                'success' => false,
                'message' => l::get('uniform-email-error').' '.l::get('uniform-email-select-error')
        );
    }

    unset($form['_sender']);
    unset($actionOptions['allowed-senders']);
    $actionOptions['_from'] = $allowed[$sender];

    return call_user_func(uniform::$actions['email'], $form, $actionOptions);
};

Ideas on how to achieve the same thing for allowed-senders that @mzur set up for allowed-recipients? It’s the exact same concept, only the other way around.

What does the fatal error say? Note that _from must be set in the $form array and not the $actionOptions.

Hello, thank you for this thread to get the custom reply working. I now get two success-messages like “Das Formular wurde gesendet.” How do I skip the message the second time?

Could you post your code, please?

It’s just standard-code:

<?php

return function($site, $pages, $page) {
    $form = uniform('contact-form', [
        'required' => [
            'Vorname'   => '',
            'Nachname'  => '',
            '_from'     => 'email'
        ],
        'actions' => [
            [
                '_action' => 'email',
                'to'      => 'mai@mail.de',
                'sender'  => 'mail@mail.de',
                'subject' => 'Neue Anmeldung für {Kurs}',
                'snippet' => 'uniform-email-default'
            ],
             [
                '_action' => 'email',
                'to'      => get('_from'),
                'sender'  => 'mail@mail.de',
                'subject' => 'Deine Anmeldung für {Kurs}',
                'snippet' => 'uniform-email-default'
            ]
        ]
    ]);

    return compact('form');
};

Two messages are sent, like it should be - but the success-message ist displayed two times too: “Vielen Dank, das Formular wurde gesendet.Vielen Dank, das Formular wurde gesendet.”

You can pass an index number for the action that should output the message like this

<?php $form->echoMessage(0) ?>
2 Likes

To explain why that happens: Each form submission can fail, so there are two separate messages. You can however only display one as in Sonja’s code snippet above if both have succeeded. :slight_smile:

Here is the function with its comments:

/**
     * Echos the success/error feedback message directly as a HTML-safe string.
     * Either from one specified action or from all actions.
     *
     * @param mixed $action (optional) the index of the action to get the
     *                      feedback message from
     */
    public function echoMessage($action = false)
    {
        echo str::html($this->message($action));
    }

(UniForm.php)