PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Jul 20, 2018 4:02 am

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


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


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


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


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


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



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
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
Quote:
I am a bit lost at the Post it in the forums part as nobody except you responded.

Cheers


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


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 1 guest


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