email-met-attachments-class

Gesponsorde koppelingen

PHP script bestanden

  1. email-met-attachments-class

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?
/****************************************************************/
/*         Content Management System                       */
/* -------------------------------------------------------------*/
/*                                        */
/*    File         : class_mail_attachment.php        */
/*    Version        : 1.0.1                    */
/*    Last modified    : 20/05/2006                */
/*                                */
/*    Author        : B.Kreleger */
/*                                                              */
/****************************************************************/

/*    Description                        */
/*--------------------------------------------------------------*/
/*    Mail class which can send optional attachments.        */


error_reporting(E_ALL);

class MailAttach
{
    //-----------------------
    //-- constructor
    //-----------------------

    function MailAttach()
    {

        //-----------------------
        //-- config (change this)
        //-----------------------

        
        $this->go_email = "emailadres@ontvanger.nl"; // to e-mail address
        $this->go_name = "Naam van Ontvanger"; // to name
        
        $this->reply_email = "emailadres@afzender.nl"; // from e-mail address
        $this->reply_name = "Naam van Afzender"; // from name
        
        //-- attachment settings

        $this->mb = 1048576; // 1Mb in bytes
        $this->max_file_size = 5; // Max file size of attachment(s) in Mb's
        
        $this->file_size_count = 1; // 1 = total size of all attachments
                        // 2 = size per attachment
        
        //-----------------------
        //-- errors
        //-----------------------
        
        //-- file error

        $this->error[0] = "<pre><code><b>Error 0:</b><br />Mail could not be send: File could not be opened or does not exist.</code></pre>";
        
        //-- file error
        $this->error[1] = "<pre><code><b>Error 1:</b><br />Mail could not be send: Filesize is unkown.</code></pre>";
        
        //-- sendmail error
        $this->error[2] = "<pre><code><b>Error 2:</b><br />Mail could not be send: Something's wrong with the mail function.</code></pre>";
        
        //-- attachment error
        $this->error[3] = "<pre><code><b>Error 3:</b><br />Mail could not be send: Attachment is too big (max: " . $this->mb * $this->max_file_size . " bytes).</code></pre>";
        
        //-- attachment error
        $this->error[4] = "<pre><code><b>Error 4:</b><br />Mail could not be send: Attachment is not unique.</code></pre>";
        
        //-----------------------
        //-- results
        //-----------------------
        
        //-- email with attachment sent

        $this->result[0] = "<p><b>E-mail met bijlage verstuurd</b></p>";
        
        //-- email without attachment sent
        $this->result[1] = "<p><b>E-mail verstuurd</b></p>";
    }

    
    //-----------------------
    //-- sendmail function
    //-----------------------

    function SendMailAttach ($subject, $message, $files = '', $to_name = '', $to_email = '', $from_name = '', $from_email = '')
    {

        //-- generate boundary
        $bound = md5(uniqid(time()));
        
        //-- check for "TO" addresses
        if (empty($to_name))
            $to_name = $this->go_name;
        if (empty($to_email))
            $to_email = $this->go_email;

        //-- check for "FROM" adresses
        if (empty($from_name))
            $from_name = $this->reply_name;
        if (empty($from_email))
            $from_email = $this->reply_email;
            
        if (!empty($files))
        {

            $headers = "From: " . $from_name . " <" . $from_email . ">\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            
            //-- mixed
            $headers .= "Content-Type: multipart/mixed; boundary=\"" . $bound . "\"\r\n";
            
            //-- sent attachment
            $headers .= "Content-Disposition:  attachment\r\n";
            
            //-- create body
            $body = "This is a multi-part message in MIME format.\r\n";
            $body .= "\r\n";
            
            //-- bound
            $body.= "--" . $bound . "\r\n";
            
            //-- charset
            $body.= "Content-Type: text/plain; charset=iso-8859-1\r\n";
            
            //-- 7bit coding
            $body.= "Content-Transfer-Encoding: 7bit\r\n";
            $body.= "\r\n";
            
            //-- message
            $body.= $message ."\r\n";
            
            foreach($files as $filename)
            {

                //-- set array with filename
                if (empty($attachment_name))
                    $attachment_name = array();
                
                if (in_array($filename, $attachment_name))
                    die($this->error[4]);
                else
                    $attachment_name[] = $filename;
                
                //-- read content
                if ($fp = @fopen($filename, 'r'))
                {

                    if ($filesize = @filesize($filename))
                        $inhoud = fread($fp, $filesize);
                    else
                        die($this->error[1]);
                        
                    fclose($fp);
                }

                else
                    die($this->error[0]);
                    
                //-- check if filesize is below maximum
                if ($filesize > ($this->mb * $this->max_file_size))
                    die($this->error[3]);
                    
                //-- check for filesize counting
                if ($this->file_size_count == 1)
                {

                    //-- calculate total of all files
                    if (empty($total_filesize))
                        $total_filesize = $filesize;
                    else
                    {
                        $total_filesize = $total_filesize + $filesize;
                        
                        //-- check if total filesize is below maximum
                        if ($total_filesize > ($this->mb * $this->max_file_size))
                            die($this->error[3]);
                    }
                }

                
                //-- bound
                $body .= "--" . $bound . "\r\n";
                
                //-- userfriendly filename
                $show_filename_array = explode("/", $filename);
                $show_filename = $show_filename_array[count($show_filename_array)-1];
                                
                //-- content type + name file
                $body .= "Content-Type: application/octet-stream; name=" . $show_filename . ";\r\n";
                
                //-- base64 encoding
                $body .= "Content-Transfer-Encoding: base64\r\n";
                
                //-- attached as attachment
                $body .= "Content-disposition: attachment\r\n";
                $body .= "\n";
                
                //-- content of attachment
                $body .= chunk_split(base64_encode($inhoud)) . "\r\n";
            }

            
            if (mail($to_name . " <" . $to_email . ">", $subject, $body, $headers))
                return $this->result[0];
            else
                die($this->error[2]);
        }

        //-- no attachment has been added, send e-mail without attachment
        else
        {
            $headers = "From: " . $from_name . " <" . $from_email . ">\r\n";
               $headers .= "MIME-Version: 1.0\r\n";
               $message;
               
               mail($to_name . " <" . $to_email . ">", $subject, $message, $headers);
               
               return $this->result[1];
         }
     }
}

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.