从控制台发送的电子邮件中的链接未正确呈现
Links not rendering correctly in email send from console
我有以下发送电子邮件的代码:
/**
* @param array $to
* @param string $subject
* @param array $vars
* @param string $template
* @param array $from
*/
public function sendEmail(array $to, $subject, array $vars, $template = 'default', array $from = ['dev@example.com' => 'Online'])
{
$transport = 'default';
if (Configure::read('debug')) {
$transport = 'dev';
}
$mailer = new Email($transport);
if ($this->isCommandLineInterface()) {
$mailer->setDomain('http://local.peepznew.com');
}
$this->addRecipients($mailer, $to);
$mailer->setFrom($from);
$mailer->setSubject($subject);
$mailer->setTemplate($template);
if (isset($vars['preheaderText']) === false) {
$vars['preheaderText'] = '';
}
$vars['subject'] = $subject;
$mailer->setViewVars($vars);
$mailer->setEmailFormat('both');
$mailer->send();
}
从网络界面和命令行调用此代码。在努力让完整的 url 显示在从命令行发送的消息中之后,我阅读了文档并遇到了 this:
这就是我调用 setDomain 的原因。我再次 运行 我的代码,但它仍然没有完整的 Urls。所以我在 web 界面和 cli 中创建了完全相同的函数,看起来像这样:
$this->sendEmail(
['my.email@example.com'],
'Test Email',
[
'title' => 'We need to select another peep',
'showFooterLinks' => true,
]
);
die;
默认模板看起来像这样(实际上只有这一行):
echo $this->Html->link('test link', ['controller' => 'jobs', 'action' => 'select_staff', 1, '_full' => true]);
Web 界面的电子邮件,使用上面的代码,发送完美。完整的 URL 和一切。然而,从 cli 中,它只发送 /jobs/select_staff/1
.
为什么会这样,我该如何解决?
仔细阅读文档,他们说在生成消息 ID 时使用了通过 setDomain()
设置的域,即它被用于 E-Mail header.
生成链接是完全不同的事情,并且受 App.fullBaseUrl
配置选项的影响,默认情况下从应用程序 config/bootstrap.php
中的 env('HTTP_HOST')
派生,除非已经在 config/bootstrap.php
中配置=15=].
也可以在您的 config/bootstrap_cli.php
文件中为 CLI 环境单独配置基础 URL,应该已经有这样的注释片段:
// Set the fullBaseUrl to allow URLs to be generated in shell tasks.
// This is useful when sending email from shells.
//Configure::write('App.fullBaseUrl', php_uname('n'));
另见
我有以下发送电子邮件的代码:
/**
* @param array $to
* @param string $subject
* @param array $vars
* @param string $template
* @param array $from
*/
public function sendEmail(array $to, $subject, array $vars, $template = 'default', array $from = ['dev@example.com' => 'Online'])
{
$transport = 'default';
if (Configure::read('debug')) {
$transport = 'dev';
}
$mailer = new Email($transport);
if ($this->isCommandLineInterface()) {
$mailer->setDomain('http://local.peepznew.com');
}
$this->addRecipients($mailer, $to);
$mailer->setFrom($from);
$mailer->setSubject($subject);
$mailer->setTemplate($template);
if (isset($vars['preheaderText']) === false) {
$vars['preheaderText'] = '';
}
$vars['subject'] = $subject;
$mailer->setViewVars($vars);
$mailer->setEmailFormat('both');
$mailer->send();
}
从网络界面和命令行调用此代码。在努力让完整的 url 显示在从命令行发送的消息中之后,我阅读了文档并遇到了 this:
这就是我调用 setDomain 的原因。我再次 运行 我的代码,但它仍然没有完整的 Urls。所以我在 web 界面和 cli 中创建了完全相同的函数,看起来像这样:
$this->sendEmail(
['my.email@example.com'],
'Test Email',
[
'title' => 'We need to select another peep',
'showFooterLinks' => true,
]
);
die;
默认模板看起来像这样(实际上只有这一行):
echo $this->Html->link('test link', ['controller' => 'jobs', 'action' => 'select_staff', 1, '_full' => true]);
Web 界面的电子邮件,使用上面的代码,发送完美。完整的 URL 和一切。然而,从 cli 中,它只发送 /jobs/select_staff/1
.
为什么会这样,我该如何解决?
仔细阅读文档,他们说在生成消息 ID 时使用了通过 setDomain()
设置的域,即它被用于 E-Mail header.
生成链接是完全不同的事情,并且受 App.fullBaseUrl
配置选项的影响,默认情况下从应用程序 config/bootstrap.php
中的 env('HTTP_HOST')
派生,除非已经在 config/bootstrap.php
中配置=15=].
也可以在您的 config/bootstrap_cli.php
文件中为 CLI 环境单独配置基础 URL,应该已经有这样的注释片段:
// Set the fullBaseUrl to allow URLs to be generated in shell tasks.
// This is useful when sending email from shells.
//Configure::write('App.fullBaseUrl', php_uname('n'));
另见