Email text template using Zend_View line breaks

I am using Zend_View for text to text content of a template. However, I ran into one problem that I could not solve: if the line ends with a php block (?>), Then the line break is lost.

Does anyone know how I can solve this?

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

I tried to examine the original text output, and line breaks are completely lost. There is no "\ r" or "\ n" to see, so I cannot replace anything.

Code for sending 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 License: <? = ($ 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 received email content:

  Title: Example Vacancy Type
 Location: Example LocationDepartment: Example DepartmentRequire Driving License: 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
+4
source share
2 answers

I believe PHP is eating a new line after? >. This is generally good, but I can understand why it is annoying in your case.

You will 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 is a link to confirm that I'm not losing my mind: http://www.php.net/manual/en/faq.using.php#faq.using.newlines

Why is it good? 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 this situation, you would not want to have multiple lines of new line between each of the lines that are actually displayed (a problem that I recently used in an email template for a project other than PHP).

Another option for you might not be saving your templates in PHP, just use {name}, {department_name} or the like for your variables, and then parse them.

+2
source

There are only three options.

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

2- Add one place to the end? >:

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

Results:

 Title: xxxLocation: loc Department: 

3- Add the second line of the new line ie:

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

Results:

 Title: xxxLocation: loc Department: 

Sucks, I know, but not many other things you can do. You could tell other developers using your system that this is a limitation of PHP, I'm sure they will understand.

PHP's reason is that in general, PHP is used for HTML files, and it does NOT need all the extra lines that can be caused, which can cause problems with HTML files. If you are dealing with plain text, you should be prepared to implement several special things, as you will encounter a lot of oddities when working with plan text and scripting languages.

+3
source

All Articles