Add css to php mail
A great question!
By default, PHP's mail()
function does not support adding CSS styles to the email body. However, there are a few ways to add CSS styles to your email:
-
Inline styles: You can add inline styles to your email body using the
style
attribute. For example:$headers = 'Content-Type: text/html; charset=UTF-8'; $message = '<html><body style="font-family: Arial, sans-serif; font-size: 14px;">Hello World!</body></html>'; mail('[email protected]', 'Subject', $message, $headers);
In this example, the
style
attribute is used to define the font family and font size for the email body. -
External stylesheet: You can link to an external stylesheet using the
link
tag. For example:$headers = 'Content-Type: text/html; charset=UTF-8'; $message = '<html><head><link rel="stylesheet" type="text/css" href="styles.css"></head><body>Hello World!</body></html>'; mail('[email protected]', 'Subject', $message, $headers);
In this example, the
link
tag is used to link to an external stylesheet namedstyles.css
. -
Embedded stylesheet: You can embed a stylesheet using the
style
tag. For example:$headers = 'Content-Type: text/html; charset=UTF-8'; $message = '<html><head><style type="text/css">body { font-family: Arial, sans-serif; font-size: 14px; }</style></head><body>Hello World!</body></html>'; mail('[email protected]', 'Subject', $message, $headers);
In this example, the
style
tag is used to define the stylesheet.
Note that not all email clients support CSS styles, so it's a good idea to test your email in different clients to ensure that the styles are applied correctly.
Also, keep in mind that some email clients may strip out CSS styles or apply their own styles to the email, so it's a good idea to use a combination of inline styles and external stylesheets to ensure that your email looks consistent across different clients.