<?php

    
/**
    * Allows easy sending of emails, both plain text, HTML and
    * mixed messages with alternate content and attachments.
    **/
    
class smtp
    
{
        
/**
        * Creates a message with attachments and sends it as plain text.
        **/
        
public function send_plain($subject$body$to$from 'utskick@puggan.com'$attachments NULL$charset "ISO-8859-1")
        {
            return 
$this->send($subject$body$to$from$attachmentsFALSE$charset);
        }

        
/**
        * Creates a message with attachments and sends it as HTML.
        **/
        
public function send_html($subject$body$to$from 'utskick@puggan.com'$attachments NULL$charset "ISO-8859-1")
        {
            return 
$this->send($subject$body$to$from$attachmentsTRUE$charset);
        }

        
/**
        * Creates a message with attachments and sends it as HTML or plain text.
        **/
        
public function send($subject$body$to$from 'utskick@puggan.com'$attachments NULL$html TRUE$charset "ISO-8859-1")
        {
            
$old_internal_encoding mb_internal_encoding();
            
$encoding mb_detect_encoding($body'ASCII, UTF-8, ISO-8859-1'TRUE);
            
$body mb_convert_encoding($body$charset$encoding);

            if(!
mb_detect_encoding($from"ASCII"TRUE))
            {
                
$from_parts explode("<"$from);
                if(
count($from_parts) == 2)
                {
                    
$from_encoding mb_detect_encoding($from'ASCII, UTF-8, ISO-8859-1'TRUE);
                    
mb_internal_encoding($from_encoding);
                    
$from mb_encode_mimeheader($from_parts[0], $from_encoding'Q') . "<" $from_parts[1];
                    
mb_internal_encoding($old_internal_encoding);
                }
            }

            
/* Set the SMTP headers for recipient and senders. */
            
$headers  "From: {$from}\r\n";
            
// Added by PHP, don't want a duplicate header row, $headers .= "To: {$to}\r\n";
            
$headers .= "Reply-To: {$from}\r\n";

            
/* Set the language of the content. */
            
$headers .= "Content-Language: sv-SE\r\n";

            
/* Set the mail client field. */
            
$headers .= "X-Mailer: Puggan PHP SMTP v1.x\r\n";

            
/* adding Message-Id or spamassaisng complaining about MISSING_MID */
            
$headers .= "Message-Id: <" md5(uniqid(microtime())) . "@puggan.com>\r\n";

            if(
$attachments)
            {
                
/* Generate a unique boundary string. */
                
$BREAK_BOUNDARY md5('BREAK' date('Y-m-d H:i:s'));

                
/* Set the SMTP headers to use HTML. */
                
$headers .= "MIME-Version: 1.0\n";
                
$headers .= "Content-Type: multipart/mixed; boundary=\"{$BREAK_BOUNDARY}\"\r\n";

                
/* Mark the end of the headers. */
                
$headers .= "\r\n";

                
/* Introduce the message for those who do not have a capable client. */
                
$message  "Det här är ett flerdelat meddelande i MIME-format.\n";
                
$message .= "\n";

                
/* Split the message. */
                
$message .= "--{$BREAK_BOUNDARY}\n";

                if(
$html)
                {
                    
/* Set message headers for the HTML version of the message. */
                    
$message .= "Content-Type: text/html; charset={$charset}\n";
                    
$message .= "Content-Transfer-Encoding: 7bit\n";
                    
$message .= "\n";

                    
/* Wrap the message in a html tag-set. */
                    
if(strpos($body'<html') === FALSE)
                    {
                        
$message .= "<html><head><title>{$subject}</title></head><body>\n";
                        
$message .= "{$body}\n";
                        
$message .= "</body></html>\n";
                    }
                    else
                    {
                        
$message .= "{$body}\n";
                    }
                    
$message .= "\n";
                }
                else
                {
                    
/* Set message headers for the HTML version of the message. */
                    
$message .= "Content-Type: text/plain; charset={$charset}\n";
                    
$message .= "Content-Transfer-Encoding: 7bit\n";
                    
$message .= "\n";

                    
/* Set the message text. */
                    
$message .= "{$body}\n";
                    
$message .= "\n";
                }

                
/* Create a resource to PECL Fileinfo. */
                //$file_info = finfo_open(FILEINFO_MIME);

                /* Iterate over each file attachment. */
                
foreach((array) $attachments as $row_id => $attachment)
                {
                    
/* Split the message. */
                    
$message .= "--{$BREAK_BOUNDARY}\n";

                    
/* If the attachment is a file.. */
                    
if(is_array($attachment))
                    {
                        
/* Read the file mime type, name etc. */
                        
$file_mime_type $attachment['file_mime_type'];
                        
$file_name $attachment['file_name'];

                        
/* Set the headers for the file attachment. */
                        
$message .= "Content-Type: {$file_mime_type}; name=\"{$file_name}\"\n";
                        
$message .= "Content-Disposition: attachment; filename=\"{$file_name}\"\n";
                        
$message .= "Content-Transfer-Encoding: base64\n";
                        
$message .= "\n";

                        
/* Encode the attached file into base64 and split it up to fit on a line. */
                        
$message .= chunk_split(base64_encode($attachment['data']));
                        
$message .= "\n";
                    }
                    else if(
file_exists($attachment))
                    {
                        
/* 5.3 or better ? */
                        
if(function_exists('finfo_open') AND (PHP_MAJOR_VERSION OR PHP_MAJOR_VERSION == AND PHP_MINOR_VERSION >= 3))
                        {
                            
/* moved in here */
                            
$file_info finfo_open(FILEINFO_MIME);

                            
/* Read the file mime type, name etc. */
                            
$file_mime_type finfo_file($file_info$attachment);
                            
$file_name basename($attachment);

                            
/* moved in here */
                            
finfo_close($file_info);
                        }
                        
/* still 5.2? */
                        
else if(function_exists('mime_content_type'))
                        {
                            
$file_mime_type mime_content_type($attachment);
                            
$file_name basename($attachment);
                        }
                        else
                        {
                            
$file_mime_type trim(shell_exec('file -ib "' str_replace('"''\\"'str_replace('\\''\\\\'$attachment)) . '"'));
                            
$file_mime_type_no_charset strstr(';'$file_mime_type);
                            if(
$file_mime_type_no_charset)
                            {
                                
$file_mime_type $file_mime_type_no_charset;
                            }
                        }

                        
/* Set the headers for the file attachment. */
                        
$message .= "Content-Type: {$file_mime_type}; name=\"{$file_name}\"\n";
                        
$message .= "Content-Disposition: attachment; filename=\"{$file_name}\"\n";
                        
$message .= "Content-Transfer-Encoding: base64\n";
                        
$message .= "\n";

                        
/* Encode the attached file into base64 and split it up to fit on a line. */
                        
$message .= chunk_split(base64_encode(file_get_contents($attachment)));
                        
$message .= "\n";
                    }
                    else
                    {
                        
/* Read the file mime type, name etc. */
                        
$file_mime_type "text/plain";
                        
$file_name "attachment_{$row_id}.txt";

                        
/* Set the headers for the file attachment. */
                        
$message .= "Content-Type: {$file_mime_type}; name=\"{$file_name}\"\n";
                        
$message .= "Content-Disposition: attachment; filename=\"{$file_name}\"\n";
                        
$message .= "Content-Transfer-Encoding: base64\n";
                        
$message .= "\n";

                        
/* Encode the attached file into base64 and split it up to fit on a line. */
                        
$message .= chunk_split(base64_encode($attachment));
                        
$message .= "\n";
                    }
                }

                
/* Free the resource to PECL Fileinfo. */
                //finfo_close($file_info);

                /* End the message. */
                
$message .= "--{$BREAK_BOUNDARY}--\n";
            }
            else
            {
                
$message "";
                if(
$html)
                {
                    
/* Set message headers for the HTML version of the message. */
                    
$headers .= "Content-Type: text/html; charset={$charset}\r\n";
                    
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
                    
$headers .= "\r\n";

                    
/* Wrap the message in a html tag-set. */
                    
if(strpos($body'<html') === FALSE)
                    {
                        
$message .= "<html><head><title>{$subject}</title></head><body>\n";
                        
$message .= "{$body}\n";
                        
$message .= "</body></html>\n";
                    }
                    else
                    {
                        
$message .= "{$body}\n";
                    }
                    
$message .= "\n";
                }
                else
                {
                    
/* Set message headers for the HTML version of the message. */
                    
$headers .= "Content-Type: text/plain; charset={$charset}\r\n";
                    
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
                    
$headers .= "\r\n";

                    
/* Set the message text. */
                    
$message .= "{$body}\n";
                    
$message .= "\n";
                }
            }

            
/* if subject is none ascii, mime encode it */
            
if(!mb_detect_encoding($subject"ASCII"TRUE))
            {
                
$subject_encoding mb_detect_encoding($subject'ASCII, UTF-8, ISO-8859-1'TRUE);
                
mb_internal_encoding($subject_encoding);
                
$subject mb_encode_mimeheader($subject$subject_encoding'Q');
                
mb_internal_encoding($old_internal_encoding);

            }

            
/* Send the actual message. */
//echo "sending started\n";
//echo "mail('{$to}', '{$subject}', '{$message}', '{$headers}');";
            
$result mail($to$subject$message$headers);
//echo "sending ended\n";

            
return $result;
        }

        public function 
send_both($subject$html_body$text_body$to$from 'utskick@puggan.com'$attachments NULL$charset "ISO-8859-1")
        {
            
$old_internal_encoding mb_internal_encoding();
            
$encoding mb_detect_encoding($html_body'ASCII, UTF-8, ISO-8859-1'TRUE);
            
$html_body mb_convert_encoding($html_body$charset$encoding);

            
$encoding_text mb_detect_encoding($text_body'ASCII, UTF-8, ISO-8859-1'TRUE);
            
$text_body mb_convert_encoding($text_body$charset$encoding_text);

            if(!
mb_detect_encoding($from"ASCII"TRUE))
            {
                
$from_parts explode("<"$from);
                if(
count($from_parts) == 2)
                {
                    
$from_encoding mb_detect_encoding($from'ASCII, UTF-8, ISO-8859-1'TRUE);
                    
mb_internal_encoding($from_encoding);
                    
$from mb_encode_mimeheader($from_parts[0], $from_encoding'Q') . "<" $from_parts[1];
                    
mb_internal_encoding($old_internal_encoding);
                }
            }

            if(!
mb_detect_encoding($to"ASCII"TRUE))
            {
                
$to_parts explode("<"$to);
                if(
count($to_parts) == 2)
                {
                    
$to_encoding mb_detect_encoding($to'ASCII, UTF-8, ISO-8859-1'TRUE);
                    
mb_internal_encoding($to_encoding);
                    
$to mb_encode_mimeheader($to_parts[0], $to_encoding'Q') . "<" $to_parts[1];
                    
mb_internal_encoding($old_internal_encoding);
                }
            }

            
/* Set the SMTP headers for recipient and senders. */
            
$headers  "From: {$from}\n";
            
$headers .= "Reply-To: {$from}\n";

            
/* Set the language of the content. */
            
$headers .= "Content-Language: sv-SE\n";

            
$headers .= "MIME-Version: 1.0\n";

            
/* Set the mail client field. */
            
$headers .= "X-Mailer: Puggan PHP SMTP v1.x\n";

            
/* adding Message-Id or spamassaisng complaining about MISSING_MID */
            
$headers .= "Message-Id: <" md5(uniqid(microtime())) . "@puggan.com>\n";

            
/* Generate unique boundary string. */
            
$ATTACHMENTS_BREAK_BOUNDARY md5('ATTACHMENTS_BREAK' date('Y-m-d H:i:s'));
            
$ALTERNATIVE_BREAK_BOUNDARY md5('ALTERNATIVE_BREAK' date('Y-m-d H:i:s'));

            if(
$attachments)
            {
                
/* Set the SMTP headers to use HTML. */
                
$headers .= "Content-Type: multipart/mixed; boundary=\"{$ATTACHMENTS_BREAK_BOUNDARY}\"\n";

                
/* Mark the end of the headers. */
                
$headers .= "\n";

                
/* Introduce the message for those who do not have a capable client. */
                
$message  "Det här är ett flerdelat meddelande i MIME-format.\n";
                
$message .= "\n";

                
/* Split the message. */
                
$message .= "\n";
                
$message .= "--{$ATTACHMENTS_BREAK_BOUNDARY}\n";
                
$message .= "Content-Type: multipart/alternative; boundary=\"{$ALTERNATIVE_BREAK_BOUNDARY}\"\n";
            }
            else
            {
                
$headers .= "Content-Type: multipart/alternative; boundary=\"{$ALTERNATIVE_BREAK_BOUNDARY}\"\n";

                
$message  "Det här är ett flerdelat meddelande i MIME-format.\n";
                
$message .= "\n";
            }

            
$message .= "\n";
            
$message .= "--{$ALTERNATIVE_BREAK_BOUNDARY}\n";
            
$message .= "Content-Type: text/plain; charset={$charset}\n";
            
$message .= "Content-Transfer-Encoding: 7bit\n";
            
$message .= "\n";

            
/* Set the message text. */
            
$message .= "{$text_body}\n";
            
$message .= "\n";
            
$message .= "--{$ALTERNATIVE_BREAK_BOUNDARY}\n";
            
/* Set message headers for the HTML version of the message. */
            
$message .= "Content-Type: text/html; charset={$charset}\n";
            
$message .= "Content-Transfer-Encoding: 7bit\n";
            
$message .= "\n";

            
/* Wrap the message in a html tag-set. */
            
if(strpos($html_body'<html') === FALSE)
            {
                
$message .= "<html><head><title>{$subject}</title></head><body>\n";
                
$message .= "{$html_body}\n";
                
$message .= "</body></html>\n";
            }
            else
            {
                
$message .= "{$html_body}\n";
            }
            
$message .= "\n";
            
$message .= "--{$ALTERNATIVE_BREAK_BOUNDARY}--\n";

            if(
$attachments)
            {
                
/* Iterate over each file attachment. */
                
foreach((array) $attachments as $row_id => $attachment)
                {
                    
/* Split the message. */
                    
$message .= "\n";
                    
$message .= "--{$ATTACHMENTS_BREAK_BOUNDARY}\n";

                    
/* If the attachment is a file.. */
                    
if(is_array($attachment))
                    {
                        
/* Read the file mime type, name etc. */
                        
$file_mime_type $attachment['file_mime_type'];
                        
$file_name $attachment['file_name'];

                        
/* Set the headers for the file attachment. */
                        
$message .= "Content-Type: {$file_mime_type}; name=\"{$file_name}\"\n";
                        
$message .= "Content-Disposition: attachment; filename=\"{$file_name}\"\n";
                        
$message .= "Content-Transfer-Encoding: base64\n";
                        
$message .= "\n";

                        
/* Encode the attached file into base64 and split it up to fit on a line. */
                        
$message .= chunk_split(base64_encode($attachment['data']));
                        
$message .= "\n";
                    }
                    else if(
file_exists($attachment))
                    {
                        
/* 5.3 or better ? */
                        
if(PHP_MAJOR_VERSION OR PHP_MAJOR_VERSION == AND PHP_MINOR_VERSION >= 3)
                        {
                            
/* moved in here */
                            
$file_info finfo_open(FILEINFO_MIME);

                            
/* Read the file mime type, name etc. */
                            
$file_mime_type finfo_file($file_info$attachment);
                            
$file_name basename($attachment);

                            
/* moved in here */
                            
finfo_close($file_info);
                        }
                        
/* still 5.2? */
                        
else if(function_exists('mime_content_type'))
                        {
                            
$file_mime_type mime_content_type($attachment);
                            
$file_name basename($attachment);
                        }
                        else
                        {
                            
$file_mime_type trim(shell_exec('file -ib "' str_replace('"''\\"'str_replace('\\''\\\\'$attachment)) . '"'));
                            
$file_mime_type_no_charset strstr(';'$file_mime_type);
                            if(
$file_mime_type_no_charset)
                            {
                                
$file_mime_type $file_mime_type_no_charset;
                            }
                        }

                        
/* Set the headers for the file attachment. */
                        
$message .= "Content-Type: {$file_mime_type}; name=\"{$file_name}\"\n";
                        
$message .= "Content-Disposition: attachment; filename=\"{$file_name}\"\n";
                        
$message .= "Content-Transfer-Encoding: base64\n";
                        
$message .= "\n";

                        
/* Encode the attached file into base64 and split it up to fit on a line. */
                        
$message .= chunk_split(base64_encode(file_get_contents($attachment)));
                        
$message .= "\n";
                    }
                    else
                    {
                        
/* Read the file mime type, name etc. */
                        
$file_mime_type "text/plain";
                        
$file_name "attachment_{$row_id}.txt";

                        
/* Set the headers for the file attachment. */
                        
$message .= "Content-Type: {$file_mime_type}; name=\"{$file_name}\"\n";
                        
$message .= "Content-Disposition: attachment; filename=\"{$file_name}\"\n";
                        
$message .= "Content-Transfer-Encoding: base64\n";
                        
$message .= "\n";

                        
/* Encode the attached file into base64 and split it up to fit on a line. */
                        
$message .= chunk_split(base64_encode($attachment));
                        
$message .= "\n";
                    }
                }

                
/* Free the resource to PECL Fileinfo. */
                //finfo_close($file_info);

                /* End the message. */
                
$message .= "--{$ATTACHMENTS_BREAK_BOUNDARY}--\n";
            }

            
/* if subject is none ascii, mime encode it */
            
if(!mb_detect_encoding($subject"ASCII"TRUE))
            {
                
$subject_encoding mb_detect_encoding($subject'ASCII, UTF-8, ISO-8859-1'TRUE);
                
mb_internal_encoding($subject_encoding);
                
$subject mb_encode_mimeheader($subject$subject_encoding'Q');
                
mb_internal_encoding($old_internal_encoding);

            }

            
/* Send the actual message. */
            
$result mail($to$subject$message$headers);

            return 
$result;
        }
    }
        
$smtp = new smtp();

//     // Example: Send a test message from Santa claus with no attachments.
//     // $smtp->send("Test Message", "This really is just a <strong>test</strong> message.", "Pugga <test@puggan.se>", "Tomten <tomten@nordpolen.com>");