Ancora class.phpmailer.php:

Codice PHP:
    /////////////////////////////////////////////////
    // MESSAGE CREATION METHODS
    /////////////////////////////////////////////////

    /**
     * Creates recipient headers.  
     * @access private
     * @return string
     */
    
function AddrAppend($type$addr) {
        
$addr_str $type ": ";
        
$addr_str .= $this->AddrFormat($addr[0]);
        if(
count($addr) > 1)
        {
            for(
$i 1$i count($addr); $i++)
                
$addr_str .= ", " $this->AddrFormat($addr[$i]);
        }
        
$addr_str .= $this->LE;

        return 
$addr_str;
    }
    
    
/**
     * Formats an address correctly. 
     * @access private
     * @return string
     */
    
function AddrFormat($addr) {
        if(empty(
$addr[1]))
            
$formatted $addr[0];
        else
        {
            
$formatted $this->EncodeHeader($addr[1], 'phrase') . " <" 
                         
$addr[0] . ">";
        }

        return 
$formatted;
    }

    
/**
     * Wraps message for use with mailers that do not
     * automatically perform wrapping and for quoted-printable.
     * Original written by philippe.  
     * @access private
     * @return string
     */
    
function WrapText($message$length$qp_mode false) {
        
$soft_break = ($qp_mode) ? sprintf(" =%s"$this->LE) : $this->LE;

        
$message $this->FixEOL($message);
        if (
substr($message, -1) == $this->LE)
            
$message substr($message0, -1);

        
$line explode($this->LE$message);
        
$message "";
        for (
$i=;$i count($line); $i++)
        {
          
$line_part explode(" "$line[$i]);
          
$buf "";
          for (
$e 0$e<count($line_part); $e++)
          {
              
$word $line_part[$e];
              if (
$qp_mode and (strlen($word) > $length))
              {
                
$space_left $length strlen($buf) - 1;
                if (
$e != 0)
                {
                    if (
$space_left 20)
                    {
                        
$len $space_left;
                        if (
substr($word$len 11) == "=")
                          
$len--;
                        elseif (
substr($word$len 21) == "=")
                          
$len -= 2;
                        
$part substr($word0$len);
                        
$word substr($word$len);
                        
$buf .= " " $part;
                        
$message .= $buf sprintf("=%s"$this->LE);
                    }
                    else
                    {
                        
$message .= $buf $soft_break;
                    }
                    
$buf "";
                }
                while (
strlen($word) > 0)
                {
                    
$len $length;
                    if (
substr($word$len 11) == "=")
                        
$len--;
                    elseif (
substr($word$len 21) == "=")
                        
$len -= 2;
                    
$part substr($word0$len);
                    
$word substr($word$len);

                    if (
strlen($word) > 0)
                        
$message .= $part sprintf("=%s"$this->LE);
                    else
                        
$buf $part;
                }
              }
              else
              {
                
$buf_o $buf;
                
$buf .= ($e == 0) ? $word : (" " $word); 

                if (
strlen($buf) > $length and $buf_o != "")
                {
                    
$message .= $buf_o $soft_break;
                    
$buf $word;
                }
              }
          }
          
$message .= $buf $this->LE;
        }

        return 
$message;
    }
    
    
/**
     * Set the body wrapping.
     * @access private
     * @return void
     */
    
function SetWordWrap() {
        if(
$this->WordWrap 1)
            return;
            
        switch(
$this->message_type)
        {
           case 
"alt":
              
// fall through
           
case "alt_attachment":
              
$this->AltBody $this->WrapText($this->AltBody$this->WordWrap);
              break;
           default:
              
$this->Body $this->WrapText($this->Body$this->WordWrap);
              break;
        }
    }

    
/**
     * Assembles message header.  
     * @access private
     * @return string
     */
    
function CreateHeader() {
        
$result "";
        
        
// Set the boundaries
        
$uniq_id md5(uniqid(time()));
        
$this->boundary[1] = "b1_" $uniq_id;
        
$this->boundary[2] = "b2_" $uniq_id;

        
$result .= $this->HeaderLine("Date"$this->RFCDate());
        if(
$this->Sender == "")
            
$result .= $this->HeaderLine("Return-Path"trim($this->From));
        else
            
$result .= $this->HeaderLine("Return-Path"trim($this->Sender));
        
        
// To be created automatically by mail()
        
if($this->Mailer != "mail")
        {
            if(
count($this->to) > 0)
                
$result .= $this->AddrAppend("To"$this->to);
            else if (
count($this->cc) == 0)
                
$result .= $this->HeaderLine("To""undisclosed-recipients:;");
            if(
count($this->cc) > 0)
                
$result .= $this->AddrAppend("Cc"$this->cc);
        }

        
$from = array();
        
$from[0][0] = trim($this->From);
        
$from[0][1] = $this->FromName;
        
$result .= $this->AddrAppend("From"$from); 

        
// sendmail and mail() extract Bcc from the header before sending
        
if((($this->Mailer == "sendmail") || ($this->Mailer == "mail")) && (count($this->bcc) > 0))
            
$result .= $this->AddrAppend("Bcc"$this->bcc);

        if(
count($this->ReplyTo) > 0)
            
$result .= $this->AddrAppend("Reply-to"$this->ReplyTo);

        
// mail() sets the subject itself
        
if($this->Mailer != "mail")
            
$result .= $this->HeaderLine("Subject"$this->EncodeHeader(trim($this->Subject)));

        
$result .= sprintf("Message-ID: <%s@%s>%s"$uniq_id$this->ServerHostname(), $this->LE);
        
$result .= $this->HeaderLine("X-Priority"$this->Priority);
        
$result .= $this->HeaderLine("X-Mailer""PHPMailer [version " $this->Version "]");
        
        if(
$this->ConfirmReadingTo != "")
        {
            
$result .= $this->HeaderLine("Disposition-Notification-To"
                       
"<" trim($this->ConfirmReadingTo) . ">");
        }

        
// Add custom headers
        
for($index 0$index count($this->CustomHeader); $index++)
        {
            
$result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), 
                       
$this->EncodeHeader(trim($this->CustomHeader[$index][1])));
        }
        
$result .= $this->HeaderLine("MIME-Version""1.0");

        switch(
$this->message_type)
        {
            case 
"plain":
                
$result .= $this->HeaderLine("Content-Transfer-Encoding"$this->Encoding);
                
$result .= sprintf("Content-Type: %s; charset=\"%s\"",
                                    
$this->ContentType$this->CharSet);
                break;
            case 
"attachments":
                
// fall through
            
case "alt_attachments":
                if(
$this->InlineImageExists())
                {
                    
$result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s"
                                    
"multipart/related"$this->LE$this->LE
                                    
$this->boundary[1], $this->LE);
                }
                else
                {
                    
$result .= $this->HeaderLine("Content-Type""multipart/mixed;");
                    
$result .= $this->TextLine("\tboundary=\"" $this->boundary[1] . '"');
                }
                break;
            case 
"alt":
                
$result .= $this->HeaderLine("Content-Type""multipart/alternative;");
                
$result .= $this->TextLine("\tboundary=\"" $this->boundary[1] . '"');
                break;
        }

        if(
$this->Mailer != "mail")
            
$result .= $this->LE.$this->LE;

        return 
$result;
    }

    
/**
     * Assembles the message body.  Returns an empty string on failure.
     * @access private
     * @return string
     */
    
function CreateBody() {
        
$result "";

        
$this->SetWordWrap();

        switch(
$this->message_type)
        {
            case 
"alt":
                
$result .= $this->GetBoundary($this->boundary[1], ""
                                              
"text/plain""");
                
$result .= $this->EncodeString($this->AltBody$this->Encoding);
                
$result .= $this->LE.$this->LE;
                
$result .= $this->GetBoundary($this->boundary[1], ""
                                              
"text/html""");
                
                
$result .= $this->EncodeString($this->Body$this->Encoding);
                
$result .= $this->LE.$this->LE;
    
                
$result .= $this->EndBoundary($this->boundary[1]);
                break;
            case 
"plain":
                
$result .= $this->EncodeString($this->Body$this->Encoding);
                break;
            case 
"attachments":
                
$result .= $this->GetBoundary($this->boundary[1], """""");
                
$result .= $this->EncodeString($this->Body$this->Encoding);
                
$result .= $this->LE;
     
                
$result .= $this->AttachAll();
                break;
            case 
"alt_attachments":
                
$result .= sprintf("--%s%s"$this->boundary[1], $this->LE);
                
$result .= sprintf("Content-Type: %s;%s" .
                                   
"\tboundary=\"%s\"%s",
                                   
"multipart/alternative"$this->LE
                                   
$this->boundary[2], $this->LE.$this->LE);
    
                
// Create text body
                
$result .= $this->GetBoundary($this->boundary[2], ""
                                              
"text/plain""") . $this->LE;

                
$result .= $this->EncodeString($this->AltBody$this->Encoding);
                
$result .= $this->LE.$this->LE;
    
                
// Create the HTML body
                
$result .= $this->GetBoundary($this->boundary[2], ""
                                              
"text/html""") . $this->LE;
    
                
$result .= $this->EncodeString($this->Body$this->Encoding);
                
$result .= $this->LE.$this->LE;

                
$result .= $this->EndBoundary($this->boundary[2]);
                
                
$result .= $this->AttachAll();
                break;
        }
        if(
$this->IsError())
            
$result "";

        return 
$result;
    }