Solved

how to add nice formatting/css etc. to php internet email

Posted on 2010-11-16
10
260 Views
Last Modified: 2012-06-27
I am putting forward this small example to ask how to add styling to php internet email..::


$to='ag@groos.net';

$body=$account_name;

$subject = "HTML email";



$message = "

<html>

<head>

<title>HTML email</title>

</head>

<body>

<p>This email contains HTML Tags!</p>

<table>

<tr>

<th>Firstname</th>

<th>Lastname</th>

</tr>

<tr>

<td>John</td>

<td>Doe</td>

</tr>

</table>

</body>

</html>

";



// Always set content-type when sending HTML email

$headers = "MIME-Version: 1.0" . "\r\n";

$headers = "Content-type:text/html;charset=iso-8859-1" . "\r\n";



// More headers

$headers .= 'From: <admin@groos.net>' . "\r\n";

$headers .= 'Cc: ag@groos.net' . "\r\n";



mail($to,$subject,$message,$headers);

Open in new window

0
Comment
Question by:solunatec
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 3

Expert Comment

by:Prograministrator
ID: 34147791
Simply, add <style> tag inside the <head> tag and style the Email as you want

like this :

$to='ag@groos.net';
$body=$account_name;
$subject = "HTML email";

$message = "
<html>
<head>
<title>HTML email</title>
<style>
.test {
/* style here*/
}
/* and so on*/
</style>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers = "Content-type:text/html;charset=iso-8859-1" . "\r\n";

// More headers
$headers .= 'From: <admin@groos.net>' . "\r\n";
$headers .= 'Cc: ag@groos.net' . "\r\n";

mail($to,$subject,$message,$headers);

Open in new window


And take a look here : http://www.campaignmonitor.com/css/
0
 
LVL 7

Accepted Solution

by:
armchang earned 250 total points
ID: 34148469
Hi,

The above code may not be complete. You need to specify also the type='text/css' inside the <style></style> tags like this inside the <head></head> tags:

<style type="text/css">
h1 {color:red}
p {color:blue}
</style>

Open in new window

0
 

Author Comment

by:solunatec
ID: 34149512
<style type='text/css">

did not work, gave me an error in my editor...and browser
plain old <style> did though....???
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 34149590
What editor? What error?
0
 
LVL 3

Assisted Solution

by:Prograministrator
Prograministrator earned 250 total points
ID: 34149597
Yep, that's right,

type is a required attribute in <style> tag

Thanks  @armchang

@solunatec : this should working well :

 
$to='ag@groos.net';
$body=$account_name;
$subject = "HTML email";

$message = "
<html>
<head>
<title>HTML email</title>
<style type='text/css'>
.test {
/* style here*/
}
/* and so on*/
</style>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers = "Content-type:text/html;charset=iso-8859-1" . "\r\n";

// More headers
$headers .= 'From: <admin@groos.net>' . "\r\n";
$headers .= 'Cc: ag@groos.net' . "\r\n";

mail($to,$subject,$message,$headers);

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 3

Expert Comment

by:Prograministrator
ID: 34149615
The error is double quotes,

I replaced it with single quote.
0
 
LVL 7

Expert Comment

by:armchang
ID: 34149633
Yes, Thanks for that.
0
 

Author Comment

by:solunatec
ID: 34149654
Yes it was the double quotes: thanks everyone....
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 34149710
Double quote have never given an error, nor in Netbeans nor in browser (any browser)... not in my experience.
"The most important feature of double-quoted strings is the fact that variable names will be expanded." (http://php.net/manual/en/language.types.string.php)

Cheers
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 34149730
Ooops, I didn't thought to the others double quotes which styles ones were wrapped in!. I'm sorry.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now