Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Emailing a dynamically generated document with PHP mail();

Posted on 2002-06-10
5
Medium Priority
?
249 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 200 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 …
Suggested Courses

885 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