puggan icon

mail attachments

puggan | PRO | 08/11/11 07:02:26 AM UTC | 0 ⭐ | 599 👁️ | Never ⏰ | []
PHP |

3.71 KB

|

None

|

0 👍

/

0 👎

function send($subject, $body, $to, $from = '[email protected]', $attachments = NULL, $html = TRUE, $charset = "iso-8859-1")
        {
            /* Set the SMTP headers for recipient and senders. */
            $headers  = "From: {$from}\n";
            $headers .= "Reply-To: {$from}\n";
 
            /* 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}\"\n";
 
            /* Set the language of the content. */
            $headers .= "Content-Language: sv-SE\n";
 
            /* Set the mail client field. */
            $headers .= "X-Mailer: Interfaceways v1.x\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 .= "--{$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. */
                $message .= "<html><head><title>{$subject}</title></head><body>\n";
                $message .= "{$body}\n";
                $message .= "</body></html>\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 $attachment)
            {
                /* Split the message. */
                $message .= "--{$BREAK_BOUNDARY}\n";
 
                /* If the attachment is a file.. */
                if(file_exists($attachment))
                {
                    /* 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);
 
                    /* 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($file)));
                    $message .= "\n";
                }
                else
                {
                    /* 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";
                }
            }
 
            /* Free the resource to PECL Fileinfo. */
            //finfo_close($file_info);
 
            /* End the message. */
            $message .= "--{$BREAK_BOUNDARY}--\n";
 
            /* Send the actual message. */
            mail($to, $subject, $message, $headers);
        }

Comments