Solved

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

Posted on 2010-11-16
10
261 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 31

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 31

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 31

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

911 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

19 Experts available now in Live!

Get 1:1 Help Now