Email w/ Attachments class

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Email w/ Attachments class

Post by Todd_Z »

Email.php

Code: Select all

 
<?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
method_man
Forum Contributor
Posts: 257
Joined: Sat Mar 19, 2005 1:38 am

Post by method_man »

looks cool :D
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I'm almost positive the last seperator has to have a trailing "--" after it or else its out of spec and some mail clients will choke.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

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??

Code: Select all

 
            for ( $i=0; $i < strlen($base64) / 76; $i++ ) 
                    $message .= substr( $base64, $i*76, 76 )."\r\n"; 
 
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

It simply brakes up the base64 encoded string in lines of 76 chars long..
I believe netiquette says you shouldn't have lines longer than 80 chars?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ahhhh, nostalgia.. I actually still code with that restriction (when possible)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

timvw wrote:It simply brakes up the base64 encoded string in lines of 76 chars long..
I believe netiquette says you shouldn't have lines longer than 80 chars?
clarification:
do we do this 'base64 encoding' for all types of files images/music when they are made as attachements to mail? And all files are broken to several lines where each line is less than 80 chars??
can you tell me why the transfer should not contain a line more than 80 chars? :roll:
What are the situations when base64 encoding has to be done???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can encode in any fashion you wish. Base64 is the most commonly accepted form. Reason: the characters universally transfer between various character-sets and platforms.

As for why lines aren't longer than 80 characters: back in the days of dinosaurs, cave-coders had displays that could only go so wide. This width was 80 characters. I'm showing my age a bit there.. :oops:
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

A lot of people still do a fair bit of work over ssh/telnet style terminals. These still have the 80 character line length. So I think its still an important consideration for today. Historically the suggestion was to wrap lines closer to 70 on the initial email so that it could support a few levels of nesting without going completely screwy with "> " & "> >" reply indentations.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Code: Select all

 
            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

Code: Select all

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

Code: Select all

 
                $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.
student101
Forum Newbie
Posts: 20
Joined: Sun Sep 21, 2008 4:04 pm

Re: Email w/ Attachments class

Post by student101 »

I noticed your code can send attachments and looks like from the recordset.
How would I be able to use it? I am new to this class stuff
Todd_Z wrote:Email.php

Code: Select all

 
<?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;
            
        }
    
    }
?>
 
student101
Forum Newbie
Posts: 20
Joined: Sun Sep 21, 2008 4:04 pm

Re: good!

Post by student101 »

Huoliuhi wrote:bump the thread up.
Thanks, it seems that this is a one to one session forum.

I think it's;
Read the manual if you can't find it Search the forums and if you still can't find it Post it in the forums
I am a bit lost at the Post it in the forums part as nobody except you responded.
Cheers
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Re: Email w/ Attachments class

Post by JayBird »

This thread is 3 years old.

If you want to send attachments via email, take a look at SwiftMailer
student101
Forum Newbie
Posts: 20
Joined: Sun Sep 21, 2008 4:04 pm

Re: Email w/ Attachments class

Post by student101 »

Yes I am attempting that, doesn't this forum deal with SwiftMail?
JayBird wrote:This thread is 3 years old.
If you want to send attachments via email, take a look at SwiftMailer
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Email w/ Attachments class

Post by alex.barylski »

Post Reply