0

I have been given the following as a template for use in an email. I am using phpmailer to mail it out, but am having a problem with the way style tags begin and end as they are conflicting with the php. If I go through thier template, I could move all the styles into a seporate style sheet or use classes and put the styles at the top of the page, but I dont want to do this unless I really have to.

Below is a generic example of what is going wrong with the code. The font names are wrapped in "s so the opening and closing tags of the style are 's , these 's are clashing with the opening and closing tags of php. I cant seem to find a way round it though as if I put, style=""font-name","another-font"" this wont work, if I use 's at all php is then screwed up.

What is happening is in php mailer the form contents are declared as

$body = '<span style='font-size: 13.5pt;font-family:"Georgia","serif";color:white'>some content</span></html>';

3 Answers 3

4

You need to escape your quotes. Example:

$body = '<div style=\'background:#000;\'><p>srs business here</p></div>';
Sign up to request clarification or add additional context in comments.

Comments

0

You can also work with HEREDOC (http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc). This will save you a lot of messy code. Example:

$string = <<<SOMELABEL
Here you can put your <span style='font-family: arial, helvetica, sans-serif'>formatted code</span>
SOMELABEL;

And then:

$body = $string;

Comments

0

Don't do this.

Your issue is not using HTML/CSS in PHP, it's just that your strings are basically un-stringing themselves, you need to escape the quotes as @ddubs said.

However, the elaborate on the "Don't do this", you shouldn't be mixing your HTML and PHP code like this. Although it's of course not required you should be separating out your logic from presentation as much as possible.

You can simply jump out of PHP with the closing tag, and jump back in when you need it, for example (based on yours):

// code code code ...
?>
<span style="font-size: 13.5pt; font-family: 'Georgia', serif; color: white">
    <?php echo $variable_from_code ?>
</span></html>

Whilst I'm on the whole crusade, you also shouldn't really be using inline styles ;-)

2 Comments

@Iain Simpson has to send an HTML formatted email. I think that is why he wants to use inline styling. So your example is right, if he wants to output HTML code. But in this case, the code has to be passed to a PHP variable. HEREDOC syntax is very useful to do this. You can use single quotes, variables and the code is still very clean to read and easy to maintain.
Well, ob_get_clean(); is perfect for those purposes :) It's still better to break out of PHP when working with HTML. Of course, a template file would be even better!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.