Martic.net

Talking in binary since 1980.

Sending HTML e-mail with PHP

Darko at 4:27 pm on Thursday, February 19, 2009

emailIt’s really hard for me to read unformatted e-mails, and it’s getting annoying when I recieve some important information and loose my eyes on scanning bunch of text in search for text I need to read.

I was also one of those programmers who didn’t “found time” to format those notification e-mails sent to webshop customers, but after some time I realized what I was missing:

  • attractive looking e-mail newsletters to attrack customers to buy more
  • simplifying user registration with easy to read next registration process step
  • etc. 

Looking from a programming perspective, sending HTML e-mail from PHP is just as easy as sending plain text e-mails – using mail() function + defining a simple parameter in a message header.

Here’s a PHP sample code:

  1.  
  2. <?php
  3. // reciever, sender, subject
  4. $to = "me@here.com";
  5. $from = "you@there.com";
  6. $subject = "Hello HTML e-mail World!";
  7.  
  8. // message body
  9. $message = "<body>
  10. <strong>This is my first HTML e-mail I sent via PHP</strong>
  11. <font color=\"red\">Great!</font>
  12. <a href=\"http://example.com\"> example.com</a>";
  13.  
  14. // headers defining HTML format
  15. $headers = "MIME-Version: 1.0\r\n";
  16. $headers .= "Content-type: text/html; charset=utf-8\r\n";
  17. $headers .= "From: $from\r\n";
  18.  
  19. // sending e-mail
  20. mail($to, $subject, $message, $headers);
  21. ?>
  22.  

 

2 Comments »

  1. Comment by last chaos gold
    August 12, 2009 @ 6:21 am

    Great article, again. These informations are especially useful …

  2. Comment by ramkumar
    August 17, 2009 @ 2:39 pm

    really useful for my urgent doubt thaks

RSS feed for comments on this post. TrackBack URL

Leave a comment

Enter this code

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
 
Close
E-mail It
Socialized through Gregarious 42