WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Class for sending mail with MIME attachments in multipart format using external sendmail, mimencode and zip
Categories : Email, Network, PHP, PHP Classes Update Picture
Anthony Spataro
Date : Dec 17th 1998
Grade : 2 of 5 (graded 3 times)
Viewed : 21415
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Anthony Spataro
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?
/*
Mail: An object to encapsulate the sending of email. Allows user-specified
From: addresses, handles the encoding of attachments; conforms more or less to
MIME standards.. Uses sendmail to send the mail, mimencode to do the MIME
encoding, and zip to automatically zip attachments.

Contacting the author(s):
Brought to you by the team at Sequoia Softworks, http://www.sequoiasoft.com
Feel free to contact tony@sequoiasoft.com and tell us how you like it!
(Or to complain bitterly, report bugs, or suggest new features.)

Known shortcomings/bugs:
o guessMIMEType()only knows about a few MIME types. You can expand this as
you need.
o $mime_boundary in the Send() method should be randomly generated, but it
isn't likely to ever hurt anything in its current form

Example:
require("Mail.phtml");
$mymessage = new Mail();
$mymessage->from = "php3.script@www.somedomain.net";
$mymessage->to = "luckyuser@destination.org";
$mymessage->subject = "This is your lucky day";
$mymessage->headers["Reply-To"] = "webmaster@www.somedomain.net";
$mymessage->headers["X-Extra-Header"] = "Pointless Header v3.0";
$mymessage->body = "Doesn't it feel good to get mail?\nEspecially with files
attached!\n";
$mymessage->attachments[0] = "tarball.tar.gz";
$mymessage->attachments[1] = "images/smiling_countenance.gif";
$mymessage->attachments[2] = "/usr/share/reference/jargondict.html";
$mymessage->attachments[3] = "./core";
$mymessage->attachments[4] = "/etc/passwd"; //naughty naughty!!
$mymessage->ZipAttachments("your_files.zip"); //uncomment this to zip all
//the attachments into one big
//attachment.
$mymessage->Send();
*/
class Mail {
        var $from; //The sender
        var $to; //The recipient
        var $subject; //The subject line
        var $headers; //A hash of additional headers (headername => headervalue)
        var $zipname; //The name of the file the attachments are zipped into
         //($zipname == false if attachments are to be sent
//individually)
        var $attachments; //An array of files to attach
        var $body; //The body text of the message
        
        //Mail constructor: initializes vars to default values. 'Nuff said.
        function Mail() {
                $this->from = "";
                $this->to = "";
                $this->subject = "";
                $this->headers = array();
                $this->zipname = false;
                $this->attachments = array();
                $this->body = "";
        }
        
        //Auxiliary method, used to guess a file's MIME type
        //based on its extension. Doesn't know about too many
        //extensions right now
        function guessMIMEType($filename) {
                //GUESS MIME TYPE
                $filename = basename($filename);
                if(strrchr($filename,".") == false) {
                        return("application/octet-stream");
                }
                
                $ext = strrchr($filename,".");
                switch($ext) {
                        case ".gif":
                                return "image/gif";
                                break;
                        case ".gz":
                                return "application/x-gzip";
                        case ".htm":
                        case ".html":
                                return "text/html";
                                break;
                        case ".jpg":
                                return "image/jpeg";
                                break;
                        case ".tar":
                                return "application/x-tar";
                                break;
                        case ".txt":
                                return "text/plain";
                                break;
                        case ".zip":
                                return "application/zip";
                                break;
                        default:
                                return "application/octet-stream";
                                break;
                }
        }

        //Cute little convenience method. Supply it with a filename to
        //zip attachments to, or supply it with false if attachments are
        //sent individually
        function ZipAttachments($name) {
                $this->zipname = $name;
        }

        //The workhorse method, does the actually sending of the mail.
        //Doesn't check for errors so be careful!
        function Send($sendmail = "sendmail") {
                if($this->from == "")
                        $fp = popen($sendmail . " -i " . $this->to, "w");
                else
                        $fp = popen($sendmail . " -i -f\"" . $this->from . "\"
" . $this->to, "w");

                $mime_boundary = "-1747901728-1448367683-913849620=:4553";
                
                if($fp == false)
                        return false;
                
                //Write subject header        
                fwrite($fp,"Subject: " . $this->subject . "\n");
        
                //Write user-defined headers        
                reset($this->headers);
                while(list($hdrname,$hdrval) = each($this->headers)) {
                        fwrite($fp,$hdrname . ": " . $hdrval . "\n");
                }
                
                //If there are attachments, this needs to be a MIME message
                if(count($this->attachments) > 0) {
                        //Write MIME headers
                        fwrite($fp,"MIME-Version: 1.0\n");
                        fwrite($fp,"Content-Type: multipart/mixed; BOUNDARY=\""
. $mime_boundary . "\"\n");
                        fwrite($fp,"\n");
                        //Write dummy message body
                        fwrite($fp," This message is in MIME format. The
first part should be readable text,\n");
                        fwrite($fp," while the remaining parts are likely
unreadable without MIME-aware tools.\n");
                        fwrite($fp," Send mail to
mime@docserver.cac.washington.edu for more info.\n");
                        fwrite($fp,"\n");
                        
                        //Write message text
                        fwrite($fp,"--" . "$mime_boundary" . "\n");
                        fwrite($fp,"Content-Type: text/plain; charset=US-
ASCII\n");
                        fwrite($fp,"\n");
                        fwrite($fp,$this->body);
                        fwrite($fp,"\n");
                        
                        //Handle attachments
                        if($this->zipname != false) { //IF we've been told to
//zip the attachments
                                fwrite($fp,"--" . $mime_boundary . "\n");
                                fwrite($fp,"Content-Type: application/zip; name=
\"". $this->zipname . "\"\n");
                                fwrite($fp,"Content-Transfer-Encoding: base64
\n");
                         //fwrite($fp,"Content-ID: " . $content_ID . "\n");
                                fwrite($fp,"Content-Description:\n");
                                fwrite($fp,"\n");
                                $cmdline = "zip - ";
                while(list($key, $attachment_name) = each($this->attachments))
                                        $cmdline .= "$attachment_name ";
                                $cmdline .= "| mimencode -b";
                                $pp = popen($cmdline,"r");
                                while(!feof($pp)) {
                                        $data = fread($pp,4096);
                                        fwrite($fp,$data);
                                }
                                pclose($pp);
                        }
                        else { //no need to zip the attachments, attach them
//separately
                while(list($key, $attachment_name) = each($this->attachments)) {
                                fwrite($fp,"--" . $mime_boundary . "\n");
fwrite($fp,"Content-Type: " . $this->guessMIMEType($attachment_name) . ";
name=\"". basename($attachment_name) . "\"\n");
                        fwrite($fp,"Content-Transfer-Encoding: base64\n");
                                        //fwrite($fp,"Content-ID: " .
//$content_ID . "\n");
                                        fwrite($fp,"Content-Description:\n");
                                        fwrite($fp,"\n");
                         $pp = popen("mimencode -b $attachment_name","r");
                                        while(!feof($pp)) {
                                                $data = fread($pp,4096);
                                                fwrite($fp,$data);
                                        }
                                        pclose($pp);
                                }
                        }
                        
                        fwrite($fp,"--" . $mime_boundary . "--\n");
                }
                //No need for a MIME message, so it's an RFC822 message
                else {
                        fwrite($fp,"\n");
                        fwrite($fp,$this->body);
                }
                
                
                pclose($fp);
        }
}
?>



Class that allows the PHP developer to establish connections with a POP3 mail server amd be able to list, retrieve and delete mail messages from a given mail box.
Categories : Network, Email, PHP, PHP Classes
POP3 Class
Categories : PHP Classes, PHP, Email
Client classes for Dictionary servers UPDATED: 2000-06-06
Categories : Network, Search, Complete Programs, PHP Classes, PHP
Validator - A PHP class that can can be used for validating Email IDs and Dates
Categories : PHP, PHP Classes, Data Validation, Email, Date Time
Three Cool Classes and One Trick
Categories : PHP, PHP Classes, Graphics, Email
Sample usage of IPv6 and IPv4 with PHP
Categories : PHP, PHP Classes, Network
PHP MIME Decoder. This class decodes Mime Encoded email message. Attachments are stored in a director. Works with Multipart/alternative, multipart/mixed etc. see http://p3mail.com for example.
Categories : PHP, PHP Classes, Email
An email validation script that actually checks against the recipient's mail server.
Categories : Email, Complete Programs, PHP, Network, Debugging
cPanel Email Accounts Creator
Categories : PHP, PHP Classes, Email, Form Processing, Web Services
base class to query the whois database
Categories : Network, PHP, PHP Classes
A class for sending email; it has support for To:, Cc:, Bcc: and Reply-To: headers. It requires that you have sendmail installed.
Categories : Email, PHP Classes, PHP
file class , uploade file , download file already uploaded on another website
Categories : PHP, PHP Classes, Filesystem, Web Services
Authorize.net AIM Interface Class v1.0.0
Categories : PHP, PHP Classes, Ecommerce, Payment Gateways
A simple class with some HTML output functions that would come in handy for consistent page layout etc.
Categories : PHP, PHP Classes, HTML and PHP, HTML, Navigation
simple script to send emails via a html-form to different users
Categories : Email, MySQL, PHP, Databases
 Damien Blood wrote : 484

I`ve tried your class for sending attachments.

I am trying to send a simple mail with a jpg attached.

However when it gets to the recipients outlook in box.  The attachment does not work instead you get:

 This is a MIME  
encoded message. 

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

 VGhpcyBpcyB0aGUgYm9keSBvZiB0aGUgdGVzdCBlbWFpbA==
 
 --b55de5923797e9d86e058274d84402440
Content-Type: image/jpeg; name = "testfile.jpg" 
 Content-Transfer-Encoding: base64 

 /9j/4AAQSkZJRgABAQEAyADIAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsK
CwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQU
FBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAH0Av4DASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3


etc, etc

Basically reads the whole file into the text file.

Any ideas?

Cheers

Damien

P.S.  System is PHP on Linux with exim mail program.
 
 Michael Cannon wrote :781
The MIME message appears to missing the bottom boundary call to tell the mail reader that it`s the end of the message.