Cakephp 以不同的用户选择的语言发送电子邮件
Cakephp send email in different user selected language
我想发送有关网络语言的特定语言的电子邮件。
例如,当用户注册时,他有可能 select 一种语言,例如英语 - en、意大利语 - it、德语 - de 和法语 - fr.
该网站是多语言的,所以我想做的是当用户填写表格(例如联系表格)并在他提交表格后向他发送一封电子邮件。
所以让我们假设他 selected 意大利语作为网站的语言,但是当他注册时他 selected 英语。因此,即使网站是意大利语,也应使用英语发送电子邮件。
使用 .pot 文件通过 cakephp 的 __() 函数翻译电子邮件。
邮件模板是这样的:
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>');
?>
发送邮件的函数是这样的:
/* 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();
}
所以我想问的是,有没有一种方法或参数可以传递给电子邮件,以指示我希望以哪种语言发送电子邮件。
类似于:$language = 'en';
我的 Cakephp 版本是:2.5.6
提前致谢。
您或许可以尝试在发送电子邮件时切换配置的语言,然后在发送电子邮件后恢复站点语言:-
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
。除了 title_for_layout
是 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 代码。
通过这种方式,我还告诉站点将值存储在会话中以访问正确的语言环境:
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;
}
因此,您甚至可以通过这种方式更改语言的会话值,然后再将它们改回以前的值。
希望对您有所帮助!
我想发送有关网络语言的特定语言的电子邮件。
例如,当用户注册时,他有可能 select 一种语言,例如英语 - en、意大利语 - it、德语 - de 和法语 - fr.
该网站是多语言的,所以我想做的是当用户填写表格(例如联系表格)并在他提交表格后向他发送一封电子邮件。
所以让我们假设他 selected 意大利语作为网站的语言,但是当他注册时他 selected 英语。因此,即使网站是意大利语,也应使用英语发送电子邮件。
使用 .pot 文件通过 cakephp 的 __() 函数翻译电子邮件。
邮件模板是这样的:
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>');
?>
发送邮件的函数是这样的:
/* 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();
}
所以我想问的是,有没有一种方法或参数可以传递给电子邮件,以指示我希望以哪种语言发送电子邮件。
类似于:$language = 'en';
我的 Cakephp 版本是:2.5.6
提前致谢。
您或许可以尝试在发送电子邮件时切换配置的语言,然后在发送电子邮件后恢复站点语言:-
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
。除了 title_for_layout
是 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 代码。 通过这种方式,我还告诉站点将值存储在会话中以访问正确的语言环境:
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;
}
因此,您甚至可以通过这种方式更改语言的会话值,然后再将它们改回以前的值。
希望对您有所帮助!