Solved

Sending HTML email using PHP

Posted on 2004-03-26
9
2,176 Views
Last Modified: 2012-08-14
I'm trying to get PHP to send out MIME HTML emails to our clients. There seems to be conflicting views as to how to do this but all versions I've tried have failed! Anyone know some code that structures the content correctly for the php mail function?
0
Comment
Question by:wjdashwood
9 Comments
 
LVL 4

Expert Comment

by:brunomsilva
ID: 10686186
why don't you use:

http://www.phpguru.org/mime.mail.html

it's quite simple. Some people prefer the Pear solution:
http://pear.php.net/package/Mail_Mime

cheers,
  bruno
0
 
LVL 1

Expert Comment

by:essage
ID: 10686290
Here is a class that I use without any problems (below it is how you would send the mail!):

class mime_mail
{
var $parts;
var $to;
var $from;
var $headers;
var $subject;
var $body;

  /*
  *     void mime_mail()
  *     class constructor
  */
function mime_mail()
  {
  $this->parts = array();
  $this->to =  "";
  $this->from =  "";
  $this->subject =  "";
  $this->body =  "";
  $this->headers =  "";
  }

  /*
  *     void add_attachment(string message, [string name], [string ctype])
  *     Add an attachment to the mail object
  */
function add_attachment($message, $name =  "", $ctype =  "application/octet-stream")
  {
  $this->parts[] = array (
                           "ctype" => $ctype,
                           "message" => $message,
                           "encode" => $encode,
                           "name" => $name
                                      );
  }

/*
*      void build_message(array part=
*      Build message parts of an multipart mail
*/
function build_message($part)
{
$message = $part[ "message"];
$message = chunk_split(base64_encode($message));
$encoding =  "base64";
return  "Content-Type: ".$part[ "ctype"].
                        ($part[ "name"]? "; name = \"".$part[ "name"]. "\"" :  "").
                         "\nContent-Transfer-Encoding: $encoding\n\n$message\n";
}

/*
*      void build_multipart()
*      Build a multipart mail
*/
function build_multipart()
{
$boundary =  "b".md5(uniqid(time()));
$multipart =  "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME
encoded message.\n\n--$boundary";

for($i = sizeof($this->parts)-1; $i >= 0; $i--)
    {
    $multipart .=  "\n".$this->build_message($this->parts[$i]). "--$boundary";
    }
return $multipart.=  "--\n";
}

/*
*      void send()
*      Send the mail (last class-function to be called)
*/
function send()
{
$mime =  "";
if (!empty($this->from))
    $mime .=  "From: ".$this->from. "\n";
if (!empty($this->headers))
    $mime .= $this->headers. "\n";

if (!empty($this->body))
    $this->add_attachment($this->body,  "",  "text/plain");
$mime .=  "MIME-Version: 1.0\n".$this->build_multipart();
mail($this->to, $this->subject,  "", $mime);
}
};  // end of class

$value="something for the body";

//this sends the mail
$mail = new mime_mail();
$mail->from = "test@test.test";
$mail->headers = "test@test.test";
$mail->to = "test@test.test";
$mail->subject = "New Order Specs";
$mail->body = "$values";
//$mail->add_attachment("$attachment", "test.jpg", "image/jpeg");
$mail->send();
0
 

Author Comment

by:wjdashwood
ID: 10686495
When I run the solution from essage this is what I get:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary = b12079fe9803d2ea639736da4b91c6fb2

This is a MIME
encoded message.

--b12079fe9803d2ea639736da4b91c6fb2
Content-Type: text/plain
Content-Transfer-Encoding: base64

PGI+PGZvbnQgY29sb3I9I0ZGMDAwMD5UaGlzIGlzIHRoZSBjb250ZW50PC9mb250PjwvYj4=

--b12079fe9803d2ea639736da4b91c6fb2--
0
 

Author Comment

by:wjdashwood
ID: 10686536
the htm content that was meant to be sent is the base 64 encryption PGI+...

Do you know why the full content (ie header info) is shown rather than just the intended coded mail?

Could it be something at my end on receipt?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:JtR
ID: 10686585
You probably did not code the mail header correctly!
Every linebreak and tab in mail header is important! Check it!

General I suggest using the html_mimemail-class from Richard Heyes (http://www.phpguru.org)
It's proofed and part of the PEAR class repository!

Try it! It's very simple!
0
 
LVL 14

Expert Comment

by:ThG
ID: 10686618
I generally prefer using PEAR packages when possible:
http://pear.php.net/package/Mail_Mime/1.2.1

this one works just fine.
0
 
LVL 1

Expert Comment

by:essage
ID: 10687177
PEAR packages work fine too.   Could you show us the code you are using (because I use the same code above in several of my apps, and I have no problems).   Is my code actually sending your mail?   Or is the problem the extra headers?
0
 

Accepted Solution

by:
robertybob earned 400 total points
ID: 10687335
I searched for this for a while myself and whilst I got the PHPGuru code to work with little effort, it noted an attachment to the mail on receipt.

I came across this code which appears to work fine for me though not sure of any limitations, etc.

<?
$to = "you@you.you";
$subject = "My Subject";
$name = "Me";
$from= "me@me.me";
$message .= "<html><body>Put content here</body></html>";
mail("$to","$subject","$message","From: $name <$from>\n"."MIME-Version: 1.0\n"."Content-type: text/html; charset=iso-8859-1");
?>

Good luck!
0
 

Author Comment

by:wjdashwood
ID: 10688068
That answer seems to do what I want.

Thanks for all the help
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

14 Experts available now in Live!

Get 1:1 Help Now