Return to Snippet

Revision: 61088
at November 27, 2012 23:38 by Zayac


Initial Code
class Letter
{
    private $subject;
    private $message;
    
    private $addHeaders;
    private $charset;
    
    function __construct( $subject, $message, $fromEmail = null, $fromName = null, $charset = 'utf-8')
    {
        $this->charset = $charset;
        
        $this->subject = "=?$charset?b?" . base64_encode($subject) . "?=";
        
        if (is_array($message))
        {
            $messageString = '';
            foreach ($message as $value) 
                $messageString .= "$value\n";
            $message = $messageString;
        }
        
        $this->message = $message;
        
        $addHeadersList = array(
            'From' => "=?$charset?b?" .base64_encode($fromName). "?= <$fromEmail>",
            'Content-Type' => 'text/plain; charset="' .$charset. '"',
            'Content-Transfer-Encoding' => '8bit'
        );
        
        $addHeaders = '';
        foreach ($addHeadersList as $key => $value)
            $addHeaders .= "$key: $value
";
        
        $this->addHeaders = $addHeaders;
    }
    
    public function send($toEmail, $toName = null)
    {
        if (is_array($toEmail))
        {
            $index = 0;
            while ($toEmail[$index])
            {
                $overallResult = false;
                $to = "=?$this->charset?b?" .base64_encode($toName). "?= <$toEmail[$index]>";
                $overallResult |= mail($to, $this->subject, $this->message, $this->addHeaders);
                return $overallResult;
            }
        }
        else 
        {
            $to = "=?$this->charset?b?" .base64_encode($toName). "?= <$toEmail>";
            return mail($to, $this->subject, $this->message, $this->addHeaders);
        }
        
    }
}

Initial URL

                                

Initial Description
This is a Class that allows you create a letter with correct headers and send it to multi-emails.

Initial Title
PHP Mail Letter Class

Initial Tags
class, mail

Initial Language
PHP