Vucko
Vucko

Reputation: 20834

PHP and CSS quotes issue

Hy,

I'm having trouble with quotes. I'm working on a html mail newsletter and it's a nightmare. I've read a lot about how to fix this but nothing won't work. I've tried without quotes, with \ ... Nothing won't work (maybe I was doing something wrong).

So , let's say this is the code:

echo '<td style="
    background-image:url(?http://x/y/image.png?);
                "
      </td>';

? - what quotes to use

Upvotes: 3

Views: 1181

Answers (9)

Boundless
Boundless

Reputation: 2464

You should use heredoc syntax.

echo<<<EOT
    <td style="background-image:url(?http://x/y/image.png?);"
      </td>
EOT;

This is very helpful when you need to use variables, and when you need to print quotes.

Make sure nothing else is on the lines with EOT or this won't work.

Upvotes: 0

andyb
andyb

Reputation: 43823

I don't think this is a problem with selecting the correct quotes. As many others have already pointed out, the quotes are optional or alternatively, you can simply escape a quote and it will still be valid.

I believe the problem is that not all mail clients support background-image, in particular Microsoft Outlook 2007+ and even Outlook.com do not support it.

I recommend consulting the following resources on the level of CSS support for e-mail clients.

One workaround you may want to try is using background instead of background-image.

Also, you might want to try out https://www.mailrox.com/ if you are having problems with building HTML e-mails. I've not tried it myself though so cannot comment on how good the product is.

Upvotes: 2

Leon Revill
Leon Revill

Reputation: 2010

You could try using the following method:

echo <<<HTML
     <td style="background-image:url('image/url/here.jpg');">Table Data</td>
HTML;

Take a look at PHP EOD if your note sure how to use this: PHP.net Strings

Also make sure you have the correct content-type and charset's set in the E-Mail headers (e.g. $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";) as well as the PHP depending on how you are using the newsletter template.

CSS can be limited when creating e-mails as mail clients vary a lot more than browsers and some support more than others. Give it a go and be sure to check in multiple browsers and e-mail clients.

Hope this helps.

Leon.

Upvotes: 0

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

Quotes are optional : Is quoting the value of url() really necessary?

However, you can escape them:

echo "<td style=\"url('example.com')\"";

or

echo '<td style="url(\'example.com\')";

Upvotes: 2

Curtis
Curtis

Reputation: 103358

Use \ before your quotes to escape the end of string:

echo '<td style="
    background-image:url(\'http://x/y/image.png\');
                "
      </td>';

Upvotes: 2

Escape the single quotes in your example code, like this:

echo '<td style="
    background-image:url(\'http://x/y/image.png\');
                "
      </td>';

Upvotes: 2

Christophe
Christophe

Reputation: 4828

If you want quotes It should be like this, but in this case you don't really need them. If you open with a single quote then you have to escape all single quotes inside youre string.

echo '<td style="
    background-image:url(\'http://x/y/image.png\');
                "
      </td>';

and as far as I know css in html mail is very limited, especially positioning. But I could be wrong.

Upvotes: 3

HellaMad
HellaMad

Reputation: 5374

You don't need quotes inside css url().

Upvotes: 0

Andy
Andy

Reputation: 1141

Quotes are optional in CSS for background url - So simply omit them.

Here's a similar question with more information on that: Is quoting the value of url() really necessary?

Additionally you could escape your quotes by putting a \ in front of them e.g. url(\'http://www.domain.com\')

As for HTML mail question. Best standards say to stick with tables & inline style CSS. I would avoid any kind of relative/absolute positioning. Even though some email clients may display it correctly, not all will. So better to be consistent as possible and avoid certain markup.

Upvotes: 2

Related Questions