3

I want to sent email on a specific language regarding of the web language.

For example when the user register he has the possibility to select a language for example English - en, Italian - it, German - de and french - fr.

The website is multilingual, so what i wanna do is that when a user fills a form for example the contact form, and after he submits the form an e-mail is sent to him.

So lets assume that he has selected Italian as language of the website, but when he was registered he had selected English. So the E-mail should be sent on English even though the site is in Italian.

Emails are translated through __() function of cakephp using .pot files.

Email Template is this:

contact_us_user

<h2 style="color: #ee2424;">
    <?php 
        echo __('SITENAME');
    ?>
</h2>
<?php
    echo "<h2 style='text-align: left;'>";
    if (isset($firstname) && isset($lastname) && isset($title)) {
        echo __('Hello <span style="color: #ee2424;"> %s %s</span>.', $firstname, $lastname);
    } else {
        echo __('Hello');
    }
    echo "</h2>";
    echo __('Thank you for contacting us!');
    echo "<br />";
    echo __('We will take a look to your <strong>reservation enquiry</strong> and we will get back to you with a booking quote');
    echo "<br />";
    echo "<hr />";
    echo __('<p>Thanks and Regards.</p>');
?>

And the function that send the e-mail is this:

/*  SEND MESSAGE TO THE USER    */
            $layout = 'default';
            $template = 'contact_us_user';
            $subject  = __('TEST');
            $title_for_layout = __('Contact US');
            $viewVars = array(
                "firstname"  => $this->request->data['Contact']['name'],
                "lastname"  => $this->request->data['Contact']['surname'],
            );
            if(isset($this->request->data['Contact']['email']) && !empty($this->request->data['Contact']['email']) && trim($this->request->data['Contact']['email'])!='') {
                $this->__sendEmail($this->request->data['Contact']['email'], $subject, $template, $viewVars, $layout, $title_for_layout);
            }

And the method is as bellow:

/**
 * send E-mail method
 *
 * @return boolean
 */
public function __sendEmail($emailTo, $subject = 'Email', $template = 'default', $viewVars, $layout = 'default', $title_for_layout = 'test') {
    $this->set('title_for_layout', $title_for_layout);
    App::uses('CakeEmail', 'Network/Email');
    $Email = new CakeEmail();
    $Email->template($template, $layout)
            ->emailFormat('html')
            ->viewVars($viewVars) 
            ->from(array('info@sitename.com'=>'sitename.com'))
            ->to($emailTo)
            ->subject($subject);
    return $Email->send();
}

So what I am asking is that is there a way or parameter to pass to email to indicate in which language I want email to be sent. Something like: $language = 'en';

My Cakephp version is: 2.5.6

Thanks in advance.

peterh
  • 11,875
  • 18
  • 85
  • 108
Johnny
  • 175
  • 1
  • 9
  • 1
    Are the emails not already being translated into the correct language when you send them? If you're already using Cake's i18n features to translate the View content it should be doing the same when the email is sent in the language the user has selected. I have done something very similar on a couple of sites without issue. – drmonkeyninja May 12 '15 at 09:49
  • @drmonkeyninja The emails are being translated but the emails are sent on on the language of the website as i explained above the email should be sent on the language of the user not the language of the site (For example you are navigating website in German but you registered and selected English as your own default language the email should be sent in English even though you are seeing website in German.) – Johnny May 12 '15 at 09:53

2 Answers2

4

I modified a bit of @drmonkeyninja code. This way im also telling the site to store the value on session to access the correct locale:

public function __sendEmailWithLanguage($emailTo, $viewVars, $lang, $subject = 'Email', $template = 'default', $layout = 'default') {
    // Store site language
    $siteLanguage = Configure::read('Config.language');

    if (isset($lang) && !empty($lang) && trim($lang)!='') {
        // Switch to preferred email language
        $this->Session->write('Config.language', $lang);
        Configure::write('Config.language', $lang);
    }

    // Send email
    App::uses('CakeEmail', 'Network/Email');
    $Email = new CakeEmail();
    $Email->template($template, $layout)
        ->emailFormat('html')
        ->viewVars($viewVars) 
        ->from(array('info@biriola.com'=>'Biriola.com'))
        ->to($emailTo)
        ->subject($subject);
    $result = $Email->send();

    // Restore to site language
    $this->Session->write('Config.language', $siteLanguage);
    Configure::write('Config.language', $siteLanguage);

    return $result;
}

So this way you change even the session value of language and the you change them back again to previows value.

Hope this helps!.

erlandmuchasaj
  • 232
  • 4
  • 16
1

You could perhaps try switching the configured language at the time of sending the email and then restore the site language after sending the email:-

public function __sendEmail($emailTo, $subject = 'Email', $template = 'default', $viewVars, $layout = 'default', $title_for_layout = 'test', $lang = 'eng') {
    $this->set('title_for_layout', $title_for_layout);
    App::uses('CakeEmail', 'Network/Email');
    $Email = new CakeEmail();
    // Store site language
    $siteLanguage = Configure::read('Config.language');
    // Switch to preferred email language
    Configure::write('Config.language', $lang);
    // Send email
    $Email->template($template, $layout)
            ->emailFormat('html')
            ->viewVars($viewVars) 
            ->from(array('info@sitename.com'=>'sitename.com'))
            ->to($emailTo)
            ->subject($subject);
    $result = $Email->send();
    // Restore to site language
    Configure::write('Config.language', $siteLanguage);
    return $result;
}

Not sure why you're setting the title_for_layout here. Apart from the fact that title_for_layout was deprecated in CakePHP 2.5 it would be better if this method was defined in a model; even better would be to have the email called as an Event.

drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59
  • title_for_layout has no effect, it was just for testing purposes, anyway thanx for your answer i will give it a try. – Johnny May 12 '15 at 10:15
  • Still the email is sent in the browser language ( or the current language of the website.) – Johnny May 12 '15 at 13:10