All pastes #744315 Raw Edit

Untitled

public text v1 · immutable
·published 2007-10-21 10:18 UTC
rendered paste body
class imap
{
    var $from = "";
    var $to = "";
    var $cc = "";
    var $msgFooter = "";
    // protected from public
    protected $body = array(
        // should start with 1... just in case ;)
        1 => array(
            "type" => TYPEMULTIPART, // we only send multipart
            "subtype" => "mixed" // mixed formats
        )
    );
    
    // someone calls class without any function
    public function __toString()
    {
        return "rsIMAP v0.4";
    }
    
    // attach file to message composition
    public function attachFile($filename)
    {
        $x = count($this->body) + 1; // next part  
        
        $this->body[$x] = array();
        $this->body[$x]["type"] = TYPEAPPLICATION;
        $this->body[$x]["encoding"] = ENCBASE64;
        $this->body[$x]["subtype"] = "octet-stream";
        $this->body[$x]["description"] = basename($filename);
        $this->body[$x]["disposition.type"] = "attachment";
        $this->body[$x]["disposition"] = array("filename" => basename($filename));
        $this->body[$x]["dparameters.filename"] = basename($filename);
        $this->body[$x]["parameters.name"] = basename($filename); 
        $this->body[$x]["contents.data"] = base64_encode(file_get_contents($filename));
    }
    
    public function addEmbeddedImage($filename, $cid)
    {
        $x = count($this->body) + 1; // next part  
        
        $this->body[$x] = array();
        $this->body[$x]["type"] = TYPEIMAGE;
        $this->body[$x]["encoding"] = ENCBASE64;
        $this->body[$x]["subtype"] = "octet-stream";
        $this->body[$x]["description"] = basename($filename);
        $this->body[$x]["id"] = "<" . $cid . ">";
        $this->body[$x]["disposition.type"] = "inline";
        $this->body[$x]["disposition"] = array("filename" => basename($filename));
        $this->body[$x]["content.type"] = "octet-stream";
        $this->body[$x]["content"] = array("name" => $cid);
        $this->body[$x]["dparameters.filename"] = basename($filename);
        $this->body[$x]["parameters.name"] = basename($filename);
        $this->body[$x]["cid"] = $cid;  
        $this->body[$x]["contents.data"] = base64_encode(file_get_contents($filename));
    }
    
    // send message
    public function sendMessage($to, $subject, $message, $type = 'text', $subtype = 'html')
    {
        $this->to = $to;
            
        // assemble envelop
        $envelop = array (
            "from" => $this->from,
            "to" => $this->to,
            "cc" => $this->cc
        );
        
        $x = count($this->body) + 1; // next part
        
        $this->body[$x] = array();
        
       $this->body[$x]["type"] = $type; // text
       $this->body[$x]["subtype"] = $subtype; // sending in html
       $this->body[$x]["description"] = "Message body"; // just a description for this part. can skip it
        // footer of message is in $this->msgFooter (just if you want to put some ads in outgoing emails or something)
        $this->body[$x]["contents.data"] =  $message . $this->msgFooter;
        
        // assemble message from parts
        $message = imap_mail_compose($envelop, $this->body);
        list($t_header, $t_body) = split("\r\n\r\n", $message, 2); // split headers and body
        $t_header = str_replace("\r",'', $t_header);

        // send it now
        return imap_mail($to, $subject, $t_body, $t_header); 
    }
    
}

$imap = new imap();
$imap->from = "danielss89@gmail.com";
$imap->attachFile("attachment/test.txt");
$imap->sendMessage("danielss89@gmail.com", "my subject", "my <b>html</b> message");