Cakephp发送电子邮件在不同的用户选择的语言

7lrncoxx  于 2022-11-12  发布在  PHP
关注(0)|答案(2)|浏览(156)

我想发送一封关于网络语言的特定语言的电子邮件。
例如,当用户注册时,他可以选择语言,例如英语-en、意大利语-it、德语-de和法语-fr
这个网站是多语言的,所以我想做的是,当用户填写一个表格,例如联系人表格,并在他提交了表格后,一封电子邮件被发送给他。
假设他选择了意大利语作为网站的语言,但是当他注册的时候他选择了英语。所以即使网站是意大利语的,电子邮件也应该用英语发送。
电子邮件通过cakephp的__()函数使用.pot文件进行翻译。
电子邮件模板为:
联系我们用户

<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>');
?>

发送电子邮件的函数是:

/*  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);
            }

方法如下:

/**
 * 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();
}

所以我想问的是,是否有一种方法或参数可以传递给email来指示我希望用哪种语言发送电子邮件。$language = 'en';
我的Cakephp版本是:2.5.6
先谢谢你。

vhipe2zx

vhipe2zx1#

我修改了一些@drmonkeyninja代码。这样我也告诉站点在session上存储值以访问正确的区域设置:

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;
}

因此,通过这种方式,您甚至可以更改language的会话值,然后再将其更改回先前的值。
希望这对你有帮助!

j5fpnvbx

j5fpnvbx2#

您也许可以尝试在发送电子邮件时切换配置的语言,然后在发送电子邮件后恢复网站语言:-

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;
}

不知道为什么要在这里设置title_for_layout。除了在CakePHP 2.5中不赞成使用title_for_layout这一事实之外,如果在模型中定义这个方法会更好;更好的做法是将电子邮件命名为Event

相关问题