代码之家  ›  专栏  ›  技术社区  ›  AllenJB

使用Zend\u视图中的文本电子邮件模板丢失换行符

  •  4
  • AllenJB  · 技术社区  · 14 年前

    有人知道我怎么解决这个问题吗?

    发送电子邮件的代码:

    $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 Type
    Location: 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
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   Tim Fountain    14 年前

    我相信这是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\u name}或类似的方法,然后将它们解析出来。

        2
  •  3
  •   Viper_Sb    14 年前

    1-按照Tim的建议做,并添加额外的“\n”

    2-在?>结尾添加一个空格:

    Title: <?= 'xxx' // NO SPACE here >>> ?>
    Location: <?= 'loc' // SPACE HERE >>> ?> 
    Department: <?= 'RIAA' ?>
    

    结果如下:

    Title: xxxLocation: loc
    Department:
    

    3-添加第二个换行符,即:

    Title: <?= 'xxx' // NO EXTRA newline >>> ?>
    Location: <?= 'loc' // EXTRA HERE >>> ?>
    
    Department: <?= 'RIAA' ?>
    

    结果如下:

    职务:XXX地点:loc
    部门:
    

    PHP这样做的原因是,一般来说PHP用于HTML文件,它不需要所有额外的换行符,这可能会导致HTML文件出现问题。如果您处理的是纯文本,那么您必须准备好实现一些特殊的东西,因为在处理计划文本和脚本语言时,您会遇到一些奇怪的情况。