views:

39

answers:

2

I'm using Zend_View to template text email content. However, I've come across one problem which I have been unable to solve: If a line ends with a php block (?>), then the line break is lost.

Does anyone have any idea how I can solve this?

The application is using Zend framework, but I have no particular reason to use Zend_View to render the email template (except that it looked the easiest, most obvious way to do it), so if there's an alternative solution, that would be good.

I've tried examining the raw text output, and the linebreaks are completely lost. No "\r" or "\n" to be seen, so there's nothing left that I can replace.

Code to send email:

$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();

Email template content:

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

Example of resulting email content:

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
+1  A: 

I believe it's PHP eating the newline after the ?>. Generally this is a good thing, but I can see why it is annoying in your case.

You'll probably need to do something like this:

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

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

Edit:

Here's a reference just to confirm I'm not going crazy: http://www.php.net/manual/en/faq.using.php#faq.using.newlines

Why is this a good thing? Well imagine your email template was:

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

in that situation you wouldn't want to end up with several newlines between each of the lines that's actually output (a problem I had recently in an email template for a non-PHP project).

Another option for you might be to not store your templates in PHP, just use {name}, {department_name} or similar for your variables and then parse them out.

Tim Fountain
I'd prefer not to have to use this method. This project is a framework that other developers will be using, so I'd rather not require code which only applies in specific circumstances.If at all possible, I'd like to have it so newlines in the template file === newlines in the resulting email.Frankly, I don't see how PHP unnecessarily eating newlines just because they appear after a ?> is a good thing (except when they appear at the very end of the file - but that should be a special case rather than the rule).
AllenJB
I've extended my answer above a little based on your comment. I think it's just one of those things which is handy most of the time when you don't even notice, but not ideal in situations like this. Sometimes when you view the HTML source of a non-PHP website and see massive runs of newlines - this is the reason!
Tim Fountain
+1  A: 

There are 3 options only I believe.

1- Do as Tim suggested and add extra "\n" 's

2- Add a single space to the end of ?>:

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

Results are:

Title: xxxLocation: loc
Department:

3- Add a second newline i.e:

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

Department: <?= 'RIAA' ?>

Results are:

Title: xxxLocation: loc
Department:

Sucks I know but there aren't many other things you can do. You could tell other developers using your system that it's a limitation of PHP I'm sure they'd understand.

The reason PHP does this is because in general PHP is used for HTML files, and it does NOT need all the extra newlines that would be caused, which could cause problems with HTML files. If you're dealing with straight text, you got to be ready to implement a few special things, as you'll run into a number of oddities when dealing with plan text and scripting languages.

Viper_Sb