I wanted to know if its possible, I’m like 95% sure it can’t be done but just wanted to ask
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.
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.
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.
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.
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.