2

What is the best way to define different sendto emails addresses for the ContactForm plugin, depending on the locale the site is currently viewed in?

I am setting up a multilanguage site, and the clients want to have different emails for each language version.

So far I figure a possible way would be to have separate, localized contactform templates, and in each of them define a dynamically-added recipient. But I wondered if there's a better option, e.g. locale-specific options in craft/config/contactform.php?

Thank you

1

You could write a plugin, which hooks in the contact form's onBeforeSend event, and modifies the recipients.

Here's an absolute bare-bones solution which does exactly that (not tested in any way whatsoever):

<?php namespace Craft;

class ExamplePlugin extends BasePlugin
{
    // Standard plugin methods omitted for brevity...

    public function init()
    {
        craft()->on('contactform.beforeSend', function (Event &$event) {
            $event->toEmail = 'john@doe.com';
        });
    }
}

Whether or not this is "officially" supported is unclear.

The fact it's an event (as opposed to a hook) might suggest not, but the contact form does check whether any listeners modified the event data, as evidenced by its check of the isValid property.

| improve this answer | |
0

I ended up with a simple piece of code in /craft/config/contactform.php

namespace Craft;

switch (CRAFT_LOCALE) {
    case 'en': $cf_toEmail = 'english@domain.co.uk'; break;
    case 'de': $cf_toEmail = 'german@domain.de'; break;
    // .....
    default:   $cf_toEmail = null;
}

return array(
    'toEmail' => $cf_toEmail,
    'prependSender' => '('.craft()->request->getPost('fromEmail').')',
);

prependSender in the return array was to troubleshoot another shortcoming that my clients do not like in the emails the CF plugin sends - that there is nowhere to see the fromEmail in the received email - it is only in the Reply-To header.

And I chose the config file over the plugin, as it's simpler and less code.

| improve this answer | |

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.