PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Nov 15, 2019 4:47 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: Thu Jun 09, 2005 3:05 pm 
Offline
Forum Regular
User avatar

Joined: Thu Nov 25, 2004 10:53 pm
Posts: 708
Location: U Michigan
Email.php
Syntax: [ Download ] [ Hide ]
 
<?PHP
    /***************/
    /* Email Class */
    /***************/
   
    class Email {
   
        var $info;
        var $files;
        var $mimeBoundary;
       
        function Email ( $to, $from, $subject, $message ) {
            $this->info = array( "To" => $to, "From" => $from, "Subject" => $subject, "Message" => $message );
            $this->mimeBoundary = "<<<--==+X[".md5(time())."]";
            $files = array();
        }
       
        function addAttachment ( $filename, $file, $mimetype ) {
            $this->files[$filename] = array( "MIME" => $mimetype, "FILE" => $file );
        }
       
        function sendEmail ( ) {
            $headers = "From: {$this->info['From']}\r\n";
            $headers .= "To: {$this->info['To']}\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: multipart/mixed;\r\n";
            $headers .= " boundary=\"".$this->mimeBoundary."\"";
           
            $message = "This is a multi-part message in MIME format.  Your email system does not support this feature.  Please contact sender for information.\r\n";
            $message .= "\r\n--".$this->mimeBoundary."\r\n";
            $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
            $message .= "Content-Transfer-Encoding: 7bit\r\n";
            $message .= "\r\n{$this->info['Message']}\r\n";
            $message .= "--".$this->mimeBoundary;
           
            foreach ( $this->files as $filename => $file ) {
                $message .= "\r\nContent-Type: {$this->info['MIME']}; name=\"$filename\"\r\n";
                $message .= "Content-Transfer-Encoding: base64\r\n";
                $message .= "Content-Disposition: attachment; filename=\"$filename\"\r\n\r\n";
                $base64 = base64_encode($file['FILE']);
                for ( $i=0; $i < strlen($base64) / 76; $i++ )
                    $message .= substr( $base64, $i*76, 76 )."\r\n";
                $message .= "--".$this->mimeBoundary;
            }
           
            echo "\nTo: {$this->info['To']}";
            echo "\nSubject: {$this->info['Subject']}";
            echo "\n\n$headers";
            echo "\n\n$message";
           
            echo mail ( $this->info["To"], $this->info["Subject"], $message, $headers ) == true;
           
        }
   
    }
?>
 


Improvements are encouraged!


feyd | "fixed" old tag syntax


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 09, 2005 4:30 pm 
Offline
Forum Contributor

Joined: Sat Mar 19, 2005 2:38 am
Posts: 257
looks cool :D


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 10:46 pm 
Offline
DevNet Resident
User avatar

Joined: Fri Aug 16, 2002 8:57 am
Posts: 1834
Location: Watertown, MA


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 6:03 pm 
Offline
DevNet Resident
User avatar

Joined: Sat Jun 11, 2005 6:57 am
Posts: 1451
Location: London, UK
Its a good class.
It would have been better if you had included comments for your code.
If you have got time...can you explain what this piece of code does??

Syntax: [ Download ] [ Hide ]
 
            for ( $i=0; $i < strlen($base64) / 76; $i++ )
                    $message .= substr( $base64, $i*76, 76 )."\r\n";
 


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 6:31 pm 
Offline
DevNet Master

Joined: Tue Jan 20, 2004 12:11 am
Posts: 4897
Location: Leuven, Belgium


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 7:31 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
ahhhh, nostalgia.. I actually still code with that restriction (when possible)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 8:38 pm 
Offline
DevNet Resident
User avatar

Joined: Sat Jun 11, 2005 6:57 am
Posts: 1451
Location: London, UK


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 8:44 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 8:50 pm 
Offline
DevNet Resident
User avatar

Joined: Fri Aug 16, 2002 8:57 am
Posts: 1834
Location: Watertown, MA


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 10:27 pm 
Offline
DevNet Resident
User avatar

Joined: Sat Jun 11, 2005 6:57 am
Posts: 1451
Location: London, UK
Syntax: [ Download ] [ Hide ]
 
            foreach ( $this->files as $filename => $file ) {
                $message .= "\r\nContent-Type: {$this->info['MIME']}; name=\"$filename\"\r\n";
                $message .= "Content-Transfer-Encoding: base64\r\n";
                $message .= "Content-Disposition: attachment; filename=\"$filename\"\r\n\r\n";
                $base64 = base64_encode($file['FILE']);
                for ( $i=0; $i < strlen($base64) / 76; $i++ )
                    $message .= substr( $base64, $i*76, 76 )."\r\n";
                $message .= "--".$this->mimeBoundary;
            }
 
 


in that particular set of code, this line
Syntax: [ Download ] [ Hide ]
 
                $message .= "\r\nContent-Type: {$this->info['MIME']}; name=\"$filename\"\r\n";
 


should be
Syntax: [ Download ] [ Hide ]
 
                $message .= "\r\nContent-Type: {$this->files['MIME']}; name=\"$filename\"\r\n";
 

the MIME should be the element in the files array not in the info array.


Top
 Profile  
 
PostPosted: Wed Sep 24, 2008 5:10 pm 
Offline
Forum Newbie

Joined: Sun Sep 21, 2008 4:04 pm
Posts: 20


Top
 Profile  
 
 Post subject: Re: good!
PostPosted: Mon Sep 29, 2008 5:16 am 
Offline
Forum Newbie

Joined: Sun Sep 21, 2008 4:04 pm
Posts: 20


Top
 Profile  
 
PostPosted: Tue Sep 30, 2008 5:29 am 
Offline
Admin
User avatar

Joined: Wed Aug 13, 2003 7:02 am
Posts: 4522
Location: York, UK
This thread is 3 years old.

If you want to send attachments via email, take a look at SwiftMailer


Top
 Profile  
 
PostPosted: Tue Sep 30, 2008 5:38 am 
Offline
Forum Newbie

Joined: Sun Sep 21, 2008 4:04 pm
Posts: 20


Top
 Profile  
 
PostPosted: Sun Oct 19, 2008 6:46 pm 
Offline
DevNet Evangelist

Joined: Tue Dec 21, 2004 6:00 pm
Posts: 6267
Location: Winnipeg
viewforum.php?f=52


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group