Solved

Emailing a dynamically generated document with PHP mail();

Posted on 2002-06-10
5
239 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:Richard Quadling
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert html page to a php post email form automatically? 10 55
PHP Sessions in WordPress 8 31
Loop through multiple arrays 13 28
Fulfillment API php code sample 1 40
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

770 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