Style tags in php variable?

I wanted to know if its possible, I’m like 95% sure it can’t be done but just wanted to ask :slight_smile:

I’m sending out emails using mail function in php, so all my HTML has to be in PHP variables, now what I want to do is style the PHP, I know I can use inline styling, but its a lot of code and repeated styles so I wanted to know if I could link in an external stylesheet(tried but failed) also if I could put CSS code between syle tags like so:

$message .= echo "<style> //css code </style>";

Tried but failed, maybe I’m going about it in the wrong way.

Anyone can help ?

Are you building the message body or are you writing onto the page?
You appear to be trying to do both. I don’t think the echo should be there.
You use echo to write to the page, don’t use it to set a variable, just use $variable = 'Some string' ; or .= to append it.

building,

I added echo here by mistake my code doesn’t have echo

Well, then that line of code should work.

I haven’t done email-shots for some time, but I do recall reading an article that suggested that inline CSS is the way to do it. Mine were never complex enough that it mattered. You’re relying on people allowing mails to link to external sources, though, which might be an issue - I don’t have that enabled, so that people who insist on sending emails as images don’t automatically use up my (limited) bandwidth. Mail clients are behind browsers in terms of what they will (or would, when I last tried) render correctly, so don’t be too ambitious.

If you could expand on “tried but failed” that might help others to be helpful, show us what didn’t work for example. Using PHP to generate your HTML code doesn’t make any difference to what HTML tags or styling or whatever you can use in the email body.

it doesn’t thus the reason for this topic.

Yes I know but thought maybe there was another way.

What are you talking about??, I have no such thing, this is for a notification that goes out when the admin updates a users account.

Many email clients have limited support for modern css, but that varies from one client to the next.
But it is generally considered that email clients are living in the past and the best way to deal with them is to code like it’s 1999. So as much as I dislike it, in-line styling is probably the most robust way of styling emails for wide support in clients.
I know some clients do support <style> tags, but maybe some don’t.
This is really nothing to do with PHP.

2 Likes

What I mean is, if you link to an external stylesheet, it’s going to need to be separately downloaded by the email client, in the same way that images that are not sent as attachments to the email message are, and those external downloads can be disabled in the mail client. By “external stylesheet” I presumed you would be linking to a hosted stylesheet rather than sending it as an attachment with each mail.

Can you expand on that - that is, does the line of code throw an error (which I can’t imagine as there’s nothing wrong with it, spurious ‘echo’ aside), or does the code not appear in your email message text, or do you mean that the browser isn’t acting on the style instructions? If the latter, then it may be as @SamA74 said above, email clients are quite limited in what styling they will interpret.

AFAIK some email clients don’t even accept a <head> section.

The safest approach is to cater to the least common denominator.
A table for layout with rare inline styles.

  • or even -
    A plain text only email

The more an email is designed to be a “work of art” the more chance it will be a “broken mess” for some email clients.

:thumbsup:

Yep. gmail in particular does not allow external style sheets. Inline is the only way to go if you want gmail to work.

1 Like

@SamA74 Sam thanks that cleared up a lot :slight_smile:

Excellent answer!

@droopsnoot
Okay I get what you’re saying.

The echo that is here was my mistake on typing in the forum I didn’t copy what I had and when I looked back I don’t have echo in my code, no error and no styling is being added.

Yes I will look at the email client :slight_smile:

Thanks for your time

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.