[PHP] Bijlage wordt als inhoud/body gemailed

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Mijn bedoeling is een script te maken die een PDF-document genereert (veel tijd gekost, maar is gelukt en ik snap het nu) en (lijkt makkelijker) deze als een bijlage toemailt. Maar hierbij heb ik een probleem, de .pdf file wordt niet als bijlage maar als body van de e-mail gestuurd. Kan iemand me vertellen waar ik de mist in ga?
code:
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
<?php error_reporting(E_ALL);
$tijd = date('U');
$email = "dennis@dennisweb.com";
// enkele variabelen verwijderd (voor in de pdf file)

// irrelevante pdf-generation code verwijderd

$pdf->Output("/home/httpd/vhosts/dennis.nl/httpdocs/pdf/data/".$tijd.".pdf","F");

$file = "/home/httpd/vhosts/dennis.nl/httpdocs/pdf/data/".$tijd.".pdf";
$onderwerp = "Welkom bij Dennis Internetdiensten";
$bericht = "hoi";

$headers = "From: Dennis Internetdiensten <info@dennis.nl>\r\n"; 
$headers .= "Reply-To: Dennis Internetdiensten <info@dennis.nl>\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
//email bestaat uit meerdere bestanden dus vertel wat de scheidings teken is en dat het een multipart is 
$headers .= "Content-Type: multipart/mixed; boundary=\"".bound."\"\r\n"; 
//we zenden een attachment mee 
$headers .= "Content-Disposition: attachment\r\n"; 
//readfile 
$fp = fopen($file,'r');
$bestand = fread($fp,filesize($file)); 
fclose($fp); 
//create body 
//generenen een body. Dit is een multi part gezeik 
$body.= "This is a multi-part message in MIME format.\r\n"; 
$body.= "\r\n"; 
//boundary 
$body.= "--".bound."\r\n"; 
//content type + charater set (iso in dit geval) 
$body.= "Content-Type: text/plain; charset=iso-8859-1\r\n"; 
//codering (7 bit) 
$body.= "Content-Transfer-Encoding: 7bit\r\n"; 
$body.= "\r\n"; 
//het bericht 
$body.= $bericht; 
//boundary 
$body.= "--".bound."\r\n"; 
//content type + naam bestand (database.sql) 
$body .= "Content-Type: application/octet-stream; name=overeenkomst.pdf\r\n"; 
//codering 
$body .= "Content-Transfer-Encoding: base64\r\n"; 
//als bijlage toegevoegd 
$body.= "Content-disposition: attachment\r\n"; 
$body .= "\n"; 
//de inhoud van het bestand 
$body .= chunk_split(base64_encode($bestand )) . "\r\n"; 
mail($email,$onderwerp,$body,$headers); 

?>


Gemailde e-mail:
Datum: 24 Jun 2005 12:50:28 -0000
Van: Dennis Internetdiensten <info@dennis.nl>
Aan: dennis@dennisweb.com
Onderwerp: Welkom bij Dennis Internetdiensten

hoi--bound
Content-Type: application/octet-stream; name=overeenkomst.pdf
Content-Transfer-Encoding: base64
Content-disposition: attachment
JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAy
IDAgUgovQ29udGVudHMgNCAwIFI+PgplbmRvYmoKNCAwIG9iago8PC9GaWx0ZXIgL0ZsYXRlRGVj
(en dit nog zo'n 35kg extra)
De body begint bij "hoi", dat zou ook eigenlijk de enige body-inhoud mogen zijn.

Acties:
  • 0 Henk 'm!

  • PowerSp00n
  • Registratie: Februari 2002
  • Laatst online: 19-08 08:24

PowerSp00n

There is no spoon

check je "bound" variabelen even. ($) En zet de --$bound ook op nieuwe regels.

Acties:
  • 0 Henk 'm!

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 09-09 13:58

NMe

Quia Ego Sic Dico.

Je hebt error_reporting op E_ALL staan, je moet hiervoor een notice krijgen. ("Found bound, assumed "bound" " of iets dergelijks.)

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Boundary was ik inderdaad vergeten :)

DEFINE('bound',md5(uniqid(time())));
toegevoegd.
code:
1
2
3
4
5
6
hoi--14785e0350a019458501e3b13c69766a
Content-Type: application/octet-stream; name=overeenkomst.pdf
Content-Transfer-Encoding: base64
Content-disposition: attachment

JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAy
Nog steeds niet als bijlage, maar als body.

[ Voor 11% gewijzigd door Verwijderd op 24-06-2005 15:48 ]


Acties:
  • 0 Henk 'm!

  • Osiris
  • Registratie: Januari 2000
  • Niet online
Moet zo'n boundary niet op een nieuwe regel staan?

[ Voor 8% gewijzigd door Osiris op 24-06-2005 15:49 ]


Acties:
  • 0 Henk 'm!

  • T-MOB
  • Registratie: Maart 2001
  • Laatst online: 22:34
Osiris schreef op vrijdag 24 juni 2005 @ 15:49:
Moet zo'n boundary niet op een nieuwe regel staan?
Volgens mij ook ja. Daarnaast is de content-disposition header in de header-sectie niet helemaal de bedoeling. Die hoort alleen in de sub-body die je als attachment stuurt. Daar moet je volgens mij ook filename=naam.pdf gebruiken ipv name=naam.pdf. Nog een mierenneukerige toevoeging is dat het correcte mime-type voor een PDF "application/pdf" is :)

edit:
De code in dit topic werkt bij mij overigens altijd prima. Wellicht heb je d'r wat aan :)

[ Voor 17% gewijzigd door T-MOB op 24-06-2005 16:04 ]

Regeren is vooruitschuiven


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
code:
1
2
3
4
5
6
7
8
9
10
-- Content-Type: text/plain  <html><head><style>
body {
font-family: Verdana;
color: #000000;
text-decoration: none;
font-size: 10px;
}
</style></head><body>
<b>Betreffende klant gaat 'AKKOORD' met onze AV.</b>
</body></html> -- Content-Type: application/pdf Content-Disposition: attachment; filename=overeenkomst.pdf; Content-transfer-encoding: base64  T2JqZW...Ci9JbmZvIDI5IDAgUgo+PgpzdGFydHhyZWYKMzk5ODgKJSVFT0YK  ----
Helaas, een totaal nieuwe phpcode lijkt ook niet te helpen. Zowel bij text/plain als bij text/html.

Nieuwe PHP-code:
code:
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
<?php
DEFINE('boundary',md5(uniqid(time())));

//read PDF (note the 'rb'for binary mode)
$handle = fopen($file,"rb");
while ($tmpstring = fgets($handle, 8096)) { 
    $pdf .= $tmpstring; 
}
fclose($handle);

//base-64 encode the pdf
$pdf = chunk_split(base64_encode($pdf)); 

$subject = 'Dennis Internetdiensten - Uw aanmelding voor '.$voorzieningen.''; 
// $bericht = '<b>Welkom bij Dennis Internetdiensten</b>.\n\nStuur bijlage per post naar Belgium. \n\n Met vriendelijke groet,\nSebastiaan\nAfdeling Administratie';

$bericht = "<html><head><style>
body {
font-family: Verdana;
color: #000000;
text-decoration: none;
font-size: 10px;
}
</style></head><body>
<b>Betreffende klant gaat 'AKKOORD' met onze AV.</b>
</body></html>";

$headers  = 'MIME-Version: 1.0' ."\r\n"
          . 'Content-type: multipart/mixed; boundary="' .$boundary .'"' ."\r\n" 
          . 'From: Dennis Internetdiensten <info@dennis.nl>' ."\r\n"
          . 'To: ' .$recipient ." \r\n"
          . 'Reply-To: info@dennis.nl ' ."\r\n"
          . 'X-Mailer: Dennis, PHP4 webserver';

$message = '--' .$boundary ."\r\n"
         . 'Content-Type: text/plain' ."\r\n\r\n" 
         . ''.$bericht.'' ."\r\n"
     . '--' .$boundary ."\r\n" 
         . 'Content-Type: application/pdf' ."\r\n"
         . 'Content-Disposition: attachment; filename=overeenkomst.pdf;' ."\r\n"
         . 'Content-transfer-encoding: base64' ."\r\n\r\n"
         . $pdf ."\r\n"
         . '--' .$boundary .'--';

mail($email, $subject, $message, $headers);
?>

[ Voor 18% gewijzigd door Verwijderd op 24-06-2005 16:37 ]

Pagina: 1