Solved

Emailing a dynamically generated document with PHP mail();

Posted on 2002-06-10
5
236 Views
Last Modified: 2008-02-26
I've used PHP's mail() function to send form data and text but is it possible to send a dynamically generated document as an attachement or something using PHP (or any other method).

The dynamically generated document is say a premade Rich Text Format (RTF) document that resides on the server and is read into the PHP script as a template where form data is inserted into the RTF file stream to replace its placeholders and then output to the browser as an MS Word associated file. That I can do but then how do I take that output and make it an attachement to be sent via email thru PHP's mail?
0
Comment
Question by:daluu
  • 3
5 Comments
 
LVL 40

Expert Comment

by:RQuadling
ID: 7069440
Sending attachments requires MIME.

A fairly good article is ...

http://www.phpbuilder.com/columns/kartic20000807.php3

and there is a class based on this at ...

http://px.sklar.com/code.html?code_id=233

I've not used either of these myself, but I would be starting with them and see if they do the job.

Regards,

Richard Quadling.
0
 
LVL 1

Accepted Solution

by:
LJunior earned 50 total points
ID: 7069448
this might help:

<?

/*
 *  Class mime_mail
 *  Original implementation by Sascha Schumann <sascha@schumann.cx>
 *  Modified by Tobias Ratschiller <tobias@dnet.it>:
 *      - General code clean-up
 *      - separate body- and from-property
 *      - killed some mostly un-necessary stuff
 */
 
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

/*
 * Example usage
 *
 
 $attachment = fread(fopen("test.jpg", "r"), filesize("test.jpg"));

 $mail = new mime_mail();
 $mail->from = "foo@bar.com";
 $mail->headers = "Errors-To: foo@bar.com";
 $mail->to = "bar@foo.com";
 $mail->subject = "Testing...";
 $mail->body = "This is just a test.";
 $mail->add_attachment("$attachment", "test.jpg", "image/jpeg");
 $mail->send();
 
 */
?>
0
 
LVL 4

Author Comment

by:daluu
ID: 7077393
thanks for the input. I'll try them out soon.
0
 
LVL 4

Author Comment

by:daluu
ID: 7144744
Thanks for giving me the code directly, LJunior. It saved me lots of time from searching for the solution.
0
 
LVL 4

Author Comment

by:daluu
ID: 7144751
Thanks for giving me the code directly, LJunior. It saved me lots of time from searching for the solution.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

17 Experts available now in Live!

Get 1:1 Help Now