gpt4 book ai didi

php - 使用 Zend_View 丢失换行符的文本电子邮件模板

转载 作者:可可西里 更新时间:2023-10-31 22:59:38 25 4
gpt4 key购买 nike

我正在使用 Zend_View 来模板化文本电子邮件内容。但是,我遇到了一个我无法解决的问题:如果一行以 php block (?>) 结尾,那么换行符就会丢失。

有人知道我该如何解决这个问题吗?

该应用程序使用 Zend 框架,但我没有特别的理由使用 Zend_View 来呈现电子邮件模板(除了它看起来是最简单、最明显的方法),所以如果有替代解决方案,那将是好。

我试过检查原始文本输出,换行符完全丢失了。看不到“\r”或“\n”,所以没有任何东西可以替换。

发送邮件的代码:

$emailView = new Zend_View();$emailView->setScriptPath( realpath (FRONTEND_APPLICATION_PATH .'/../email/') );$bodyText = $emailView->render('recruitment-job-alert-text.phtml');// $model = new Model_VacancyDetail();// $model->find(1);// $emailView->vacancyDetail = $model;$mail = new Zend_Mail();$mail->setSubject( 'Job Alert: ' . $model->references['vacancy_type'] );$mail->addTo($fromEmail, $fromName);$mail->setFrom( $fromEmail, $fromName );$mail->setBodyHtml( $bodyHtml );$mail->setBodyText( $bodyText );$mail->send();

邮件模板内容:

Title: <?= $this->escape($this->vacancyDetail->references['vacancy_type']) ?>Location: <?= $this->vacancyDetail->references['name'] ?>Department: <?= $this->vacancyDetail->references['department_name'] ?>Require Driving Licence: <?= ( $this->vacancyDetail->requireDrivingLicence == 1 ) ? 'Yes' : 'No' ?>Working Hours: <?= $this->vacancyDetail->workingHours ?>.Benefits: <?= $this->vacancyDetail->benefits ?>.Salary: <?= $this->vacancyDetail->salary ?>.Details:<?= strip_tags($this->vacancyDetail->description) ?>Apply now at http://www.example.com/recruitment

生成的电子邮件内容示例:

Title: Example Vacancy TypeLocation: Example LocationDepartment: Example DepartmentRequire Driving Licence: YesWorking Hours: 40.Benefits: Company car.Salary: £15,000pa.Details:This is an example position. It does not really exist.Apply now at http://www.example.com/recruitment

最佳答案

我相信是 PHP 在 ?> 之后吃换行符。通常这是一件好事,但我明白为什么这对您来说很烦人。

你可能需要做这样的事情:

Title: <?= $this->escape($this->vacancyDetail->references['vacancy_type'])."\n" ?>

Location: <?= $this->vacancyDetail->references['name']."\n" ?>
Department: <?= $this->vacancyDetail->references['department_name']."\n" ?>

编辑:

这里有一个引用,只是为了确认我不会发疯:http://www.php.net/manual/en/faq.using.php#faq.using.newlines

为什么这是一件好事?想象一下您的电子邮件模板是:

Title: <?= $this->escape($this->vacancyDetail->references['vacancy_type'])."\n" ?>

<?php if (!empty($this->vacancyDetail->references['name'])) { ?>
Location: <?= $this->vacancyDetail->references['name']."\n" ?>
<?php } ?>
Departments:
<?php foreach ($this->vacancyDetail->references['departments'] as $dept) { ?>
<?=$dept->name."\n"?>
<?php } ?>

在那种情况下,您不希望在实际输出的每一行之间以多个换行符结束(这是我最近在非 PHP 项目的电子邮件模板中遇到的问题)。

您的另一个选择可能是不将您的模板存储在 PHP 中,只需使用 {name}、{department_name} 或类似变量作为您的变量,然后将它们解析出来。

关于php - 使用 Zend_View 丢失换行符的文本电子邮件模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3405382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com