Solved

Emailing a dynamically generated document with PHP mail();

Posted on 2002-06-10
5
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 look for a specific file type in a local or remote server directory using PHP.

763 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