Bij het versturen van een email met attachment met behulp van PHP loop ik tegen een probleem aan. Het gaat namelijk goed als de bijlage, die via een formulier wordt geupload, een gewoon tekstbestand is, dan is dit gewoon te openen bij het lezen van de mail. Is het echter een ander soort bestand (bijv word/pdf/gif) dan resulteert dit wel in een bestand dat even groot is als het originele bestand, maar de inhoud lijkt wel verkeerd gerecode of iets dergelijks.
PHP:
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
| if (is_uploaded_file($_FILES['businessplan']['tmp_name'])) { $file = $_FILES['businessplan']['tmp_name']; $type = $_FILES['businessplan']['type']; $fp = fopen($file,"rb"); $fcontent = fread($fp ,filesize($file)); fclose($fp); $content = chunk_split(base64_encode($fcontent)); $sep = strtoupper(md5(uniqid(time()))); $name = $_FILES['businessplan']['name']; } $header = "From: Van <van@dit_emailadres.nl>\r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Type: multipart/mixed; boundary=$sep\r\n"; $body = "--$sep\r\n"; $body .= "Content-Type: text/plain\r\n"; $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n"; $body .= "$msgtext\r\n"; $body .= "--$sep\r\n"; $body .= "Content-Type: application/octet-stream; name=\"$name\"\r\n"; $body .= "Content-Transfer-Encoding: base64\r\n"; $body .= "Content-Disposition: attachment; filename=\"$name\"\r\n"; $body .= "$content\r\n"; $body .= "--$sep--"; mail("naar@dit_emailadres.nl", "Mailform", $body, $header); |