Changeset trunk,266


Ignore:
Timestamp:
01/13/09 09:28:35 (3 years ago)
Author:
azza-bazoo
revision id:
svn-v4:bb28ca92-9e36-0410-91a4-d1cafbab33cc:trunk:270
Message:

[composer] feature - experimental new word-wrapping for plain text messages (and minor cleanup in the sending function)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/requests.inc.php

    r264 r266  
    730730        $mimeMessage->setFrom( $FROM_EMAIL ); 
    731731 
    732         //die( print_r( parseRecipientList( $_POST['comp_to'] ) ) ); 
    733  
    734         // Set the Various recipient addresses. 
    735         /* Long diatribe about why we do the following twice, to end up with $messageRecipients and $mimeMessageRecipients. 
    736            PHP5: Creating a single $messageRecipients and using it for creating the appropriate headers in 
    737            the message (with $mimeMessage->setTo( $messageRecipients->getTo() ) works fine, and then we use 
    738            the same $messageRecipients object for the Swift::send() call, to let it know where to send the email. 
    739            PHP4: This doesn't work, Swift::send() dies with a fatal error. Cause: when we call 
    740            $mimeMessage->setTo( $messageRecipients->getTo() ) what happens is that $messageRecipients->getTo() returns 
    741            a reference to the internal array of Swift_Address objects that $messageRecipients stores. $mimeMessage->setTo() 
    742            then proceeds to iterate over the array, replacing each key in the array with the results of calling the build() 
    743            function of each element of the array (which returns a string representation of the email address stored by the 
    744            Swift_Address object). Because getTo() returns a reference, setTo()'s actions do this in place, changing  
    745            $messageRecipients internal state. And then when it comes time to call Swift:send(), it's expecting 
    746            $messageRecipients to have an array of Swift_Address objects, not strings. I hope that's confusing enough. 
    747         */ 
     732        // Set the various recipient addresses. 
     733        // (old $mimeMessageRecipients is removed, which is hopefully OK in all PHP5 uses!) 
    748734        $messageRecipients =& new Swift_RecipientList(); 
    749         $mimeMessageRecipients =& new Swift_RecipientList(); 
     735 
    750736        // TO: 
    751737        $toRecipients = parseRecipientList( $_POST['comp_to'] ); 
     
    761747        foreach ( $toRecipients as $recipient ) { 
    762748                $messageRecipients->addTo( $recipient['address'], $recipient['name'] ); 
    763                 $mimeMessageRecipients->addTo( $recipient['address'], $recipient['name'] ); 
    764749        } 
    765750 
     
    770755                foreach ( $ccRecipients as $recipient ) { 
    771756                        $messageRecipients->addCc( $recipient['address'], $recipient['name'] ); 
    772                         $mimeMessageRecipients->addCc( $recipient['address'], $recipient['name'] ); 
    773757                } 
    774758        } 
     
    780764                foreach ( $bccRecipients as $recipient ) { 
    781765                        $messageRecipients->addBcc( $recipient['address'], $recipient['name'] ); 
    782                         $mimeMessageRecipients->addBcc( $recipient['address'], $recipient['name'] ); 
    783                 } 
    784         } 
    785  
    786         //die( var_dump( $messageRecipients ) ); 
     766                } 
     767        } 
    787768 
    788769        // The Swift docs say that these don't need to be set to send a message, 
    789770        // but it does seem to be harmless - if we don't set them, then when we save 
    790771        // a draft we have no idea what these addresses are. 
    791         $mimeMessage->setTo( $mimeMessageRecipients->getTo() ); 
    792         $mimeMessage->setCc( $mimeMessageRecipients->getCc() ); 
    793         $mimeMessage->setBcc( $mimeMessageRecipients->getBcc() ); 
     772        $mimeMessage->setTo( $messageRecipients->getTo() ); 
     773        $mimeMessage->setCc( $messageRecipients->getCc() ); 
     774        $mimeMessage->setBcc( $messageRecipients->getBcc() ); 
    794775 
    795776        // Do the body of the email. 
    796777        // TODO: The body coming back should be UTF-8... so set this as the encoding... 
    797778        if ( $_POST['format'] == "text/plain" ) { 
    798                 // Just a plain text email. 
    799                 $mimeMessage->attach( new Swift_Message_Part( $_POST['comp_msg'], "text/plain" ) ); 
     779                // process text body: wordwrap if not a draft (but don't rewrap quoted lines) 
     780                // TODO: handle \r\n? efficiency? do input validation? 
     781                $message_string = ''; 
     782                if ( !$draftMode ) { 
     783                        $message_lines = explode( '\n', $_POST['comp_msg'] ); 
     784                        $this_block = $next_line = ''; 
     785                        $inside_quote_block = false; 
     786                        do { 
     787                                if(substr( ltrim( $next_line ), 0, 1 ) != '>') { 
     788                                        if ( $inside_quote_block ) { 
     789                                                $inside_quote_block = false; 
     790                                                $message_string .= $this_block; 
     791                                                $this_block = ''; 
     792                                        } 
     793                                } else { 
     794                                        if ( !$inside_quote_block ) { 
     795                                                $inside_quote_block = true; 
     796                                                $message_string .= wordwrap( $this_block, 75, " \n" ); 
     797                                                $this_block = ''; 
     798                                        } 
     799                                } 
     800                                $this_block .= $next_line; 
     801                                $next_line = array_pop( $message_lines ); 
     802                        } while ( $next_line ); 
     803                        $message_string .= $this_block; 
     804                } else { 
     805                        $message_string = $_POST['comp_msg']; 
     806                } 
     807 
     808                $mimeMessage->attach( new Swift_Message_Part( $message_string, "text/plain" ) ); 
    800809        } else { 
    801810                // It's a HTML email. This is fun! 
     
    810819                $htmlVersion .= "<meta http-equiv=\"Generator\" content=\"Lichen Webmail / TinyMCE\" />\n"; 
    811820                $htmlVersion .= "<title>{$_POST['comp_subj']}</title>\n"; 
    812                 $htmlVersion .= "</head>\n"; 
    813                 $htmlVersion .= "<body>\n"; 
     821                $htmlVersion .= "</head><body>\n"; 
    814822                $htmlVersion .= $source; 
    815                 $htmlVersion .= "\n</body>\n"; 
    816                 $htmlVersion .= "</html>\n"; 
     823                $htmlVersion .= "\n</body></html>\n"; 
    817824 
    818825                // Now generate a text version. TODO: This is crude. 
     
    914921                } 
    915922        } else { 
    916                 // Time to send the message. 
    917                 // Save it into Sent first. 
     923                // Time to send the message; first, check that the Sent folder exists 
    918924                if ( !imapCheckMailboxExistence( $SPECIAL_FOLDERS['sent'] ) ) { 
    919925                        $result['success']     = false; 
     
    923929                        return $result; // TODO: Returning in the middle of a function... don't do it! 
    924930                } 
    925                 $res = streamSaveMessage( $IMAP_SERVER, $IMAP_PORT, $IS_SSL, $IMAP_USE_TLS, $_SESSION['user'], $_SESSION['pass'], 
    926                         $SPECIAL_FOLDERS['sent'], $rawMessage, $messageLength, ""); 
    927                 if ( $res != null ) { 
    928                         $msg = sprintf( _("Unable to save sent message: %s - message NOT sent."), $res ); 
    929                          
    930                         $result['success']     = false; 
    931                         $result['errorCode']   = 'SENT'; 
    932                         $result['errorString'] = $msg; 
    933                          
    934                         return $result; // TODO: Returning in the middle of a function... don't do it! 
    935                 } 
    936931 
    937932                // Create the connection to the remote SMTP server. 
    938                 // TODO: Add TLS support. 
    939933                $smtpConnection = null; 
    940934                if ( $SMTP_USE_SSL ) { 
     
    980974                        } 
    981975 
    982                         // Remove the draft that this message was based on. ?? Actually, not yet. 
     976                        // now save the sent message ... 
     977                        $res = streamSaveMessage( $IMAP_SERVER, $IMAP_PORT, $IS_SSL, $IMAP_USE_TLS, $_SESSION['user'], $_SESSION['pass'], 
     978                                $SPECIAL_FOLDERS['sent'], $rawMessage, $messageLength, ""); 
     979                        if ( $res != null ) { 
     980                                $msg = sprintf( _("Unable to save sent message: %s - message NOT sent."), $res ); 
     981                                 
     982                                $result['success']     = false; 
     983                                $result['errorCode']   = 'SENT'; 
     984                                $result['errorString'] = $msg; 
     985                                 
     986                                return $result; // TODO: Returning in the middle of a function... don't do it! 
     987                        } 
     988 
     989                        // TODO: ... and delete the draft, if there was one 
    983990 
    984991                        // Mark the original message as answered, in the case of Replies. 
Note: See TracChangeset for help on using the changeset viewer.