Adding style to a php html email
Solution 1
You need to use inline style to get it works on your email
$to = $newsletter_email;
$subject = 'Thank you for subscribing';
$message = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<div id="email-wrap" style='background: #151515;color: #FFF;'>
<p>Hi,</p><br>
<p>Thank you.</p><br>
<p>Thank you,</p>
<p>Administration</p>
</div>
</body>
</html>
';
$from = "[email protected]";
//$Bcc = "[email protected]";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
// $headers .= 'Bcc: '.$Bcc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
For the second part of your question you can use something like this
$to = '[email protected]';
$email_from = "[email protected]";
$full_name = 'Best Buy';
$from_mail = $full_name.'<'.$email_from.'>';
$from = $from_mail;
$headers = "" .
"Reply-To:" . $from . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $from_email . "\r\n";
mail($to,$subject,$message,$headers);
Solution 2
Just try to make it like template.
Here is the little bit example may be helpful.
Create my_template.html file & add following code in that file.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Newsletter</title>
<style>
#email-wrap {
background: #151515;
color: #FFF;
}
</style>
</head>
<body>
<p>Hi {{USERNAME}}</p>
<p>Please click on below link </p><br>
{{LINK}}
</body>
</html>
now Write send email function where you want. and follow the step :
1) Read HTML file you recently created.
$html = file_get_contents('my_template.html');
2) Replace Variables
$link = "<a href='LINK YOU WANR TO PLACE'>Click Here </a>";
$html = str_replace("{{USERNAME}}",$username,$html);
$html = str_replace("{{LINK}}",$link,$html);
3) Finally send email.
$from = "[email protected]";
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Bcc: '.$Bcc. "\r\n";
// Send the email
mail($to,$subject,$html,$headers);
Ralph
Updated on June 17, 2022Comments
-
Ralph almost 2 years
I looked all over for this question and all I found on this was to add the following:
$headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
and
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I am wanting to send a newsletter type email, so the styling really matters for this. All of the videos I watched were just making html sheets, so I really didn't get that. I want to style the content in my email.
I have this right now:
$to = $newsletter_email; $subject = 'Thank you for subscribing'; $message = ' <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style> #email-wrap { background: #151515; color: #FFF; } </style> </head> <body> <div id="email-wrap"> <p>Hi,</p><br> <p>Thank you.</p><br> <p>Thank you,</p> <p>Administration</p> </div> </body> </html> '; $from = "[email protected]"; //$Bcc = "[email protected]"; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'To: ' .$to. "\r\n"; $headers .= 'From: ' .$from. "\r\n"; // $headers .= 'Bcc: '.$Bcc. "\r\n"; // Send the email mail($to,$subject,$message,$headers);
I have tried taking out the style from this message variable and turning this file into a html styled file, outside of the php:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Newsletter</title> <style> #email-wrap { background: #151515; color: #FFF; } </style> </head>
etc.
The email actually sends, but I cannot figure out how to add style to this. What am I doing wrong??
$email_from = "[email protected]"; $full_name = 'Company Name'; //$from_mail = $full_name.'<'.$email_from.'>'; $from = $from_mail; //$from = "[email protected]"; //$Bcc = "[email protected]"; // To send HTML mail, the Content-type header must be set $headers .= "From: ".$full_name." <".$email_from.">\r\n"; and $headers .= "Return-Path: ".$full_name." <".$email_from.">\r\n"; /*$headers = "" . "Reply-To:" . $from . "\r\n" . "X-Mailer: PHP/" . phpversion();*/ $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'To: ' .$to. "\r\n"; $headers .= 'From: ' .$from_email. "\r\n"; // $headers .= 'Bcc: '.$Bcc. "\r\n"; // Send the email mail($to,$subject,$message,$headers);
-
Ralph about 8 yearsDo you know why my name is coming up as newsletter when sending this? I assumed it was because of my header. So I tried doing this
$headers .= 'From: Company'"\r\n";
and it breaks the code -
Peter Wilson about 8 years@Ralph do you mean it appears as
newsletter
instead of[email protected]
-
Ralph about 8 yearsNo, when you go into gmail or whatever mail client you use, next to the subject line, where it shows who the email is from, mine says
newsletter
. I am wanting it to say myCompany Name
. So if I was Best Buy, I would want it to sayBest Buy
. -
symlink about 8 yearsI believe the from param in a PHP header is looking for an email address.
-
Peter Wilson about 8 yearsYou need to Change you variable $from to be
$from = "Best Buy";
instead of$from = "[email protected]";
-
Ralph about 8 years@PeterWilson Won't that not use my email address as the recipient then?
-
Peter Wilson about 8 years@Ralph No, the recipient is $to not $from
-
Ralph about 8 years@PeterWilson Yea, sorry. I saw that and tried to edit my post as quick as possible. Guess it wasn't quick enough. Doing this :
$from = "Company Name";
shows up asunknown sender
and doesn't show my email address. -
Peter Wilson about 8 years@Ralph Ok I got it please refer to this question stackoverflow.com/questions/8365754/…
-
Ralph about 8 years@PeterWilson Unfortunately, that is still showing the unknown sender. I added my code to my question.
-
Peter Wilson about 8 years@Ralph try
$headers .= "From: ".$full_name." <".$email_from.">\r\n";
and$headers .= "Return-Path: ".$full_name." <".$email_from.">\r\n";
-
Ralph about 8 years@PeterWilson That isn't allowing me to send it. OR my code could just be so messed up from trying this in so many variations, I updated my question with current code,
-
Peter Wilson about 8 yearsLet us continue this discussion in chat.