Hallo,

ik heb een php script waarbij een pdf als e-mail bijlage moet worden verstuurd. Dit lukt 100% goed op mijn windows test server (IIS).
Maar zodra ik hetzelfde wil doen op een linux webserver (RaspberryPI) met Apache dan krijg ik de mail met heleboel tekens in de mail.

Ik zal de hele pagina besparen en alleen de mail routine vermelden.

<?php
// email fields: to, from, subject, and so on
$message = $bericht;
$from_name = "factuur ".$invoice_number;
$header = "From: ".$from_name." <".$email_from.">\r\n";

// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$header .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";

// preparing attachments
$data = chunk_split(base64_encode(file_get_contents($attach_file)));
$message .= "Content-Type: {\"application/octet-stream\"};\n name=\"".$invoice_number.".pdf\"\nContent-Disposition: attachment;\n filename=\"".$invoice_number.".pdf\"\nContent-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";

if(mail($email_invoice, "$subject", $message, $header)) {
print "<tr><td colspan='2'><br />You will receive a invoice email.</td></tr>\n";
} else {
print "<tr><td colspan='2'><br />There is a error sending the invoice email.</td></tr>\n";
}
?>
Test eens wat er gebeurt als je de constante PHP_EOL gebruikt in plaats van allerlei varianten van \n en \r? Die constante is namelijk platform-afhankelijk.
Waar vind ik die PHP_EOL? php.ini?

heb de \r weggehaald en heb al een paar regels uit de body in de header kunnen plaatsen (raspberry (unix ))

boundary="==Multipart_Boundary_x907fa3a67a9aeacc5ac7661709f46fa8x"

This is a multi-part message in MIME format.
--==Multipart_Boundary_x907fa3a67a9aeacc5ac7661709f46fa8x
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
En hier begint het bericht .................
echo 'een string'. PHP_EOL;

PHP_EOL is gewoon een systeem constrante.

[size=xsmall]Toevoeging op 15/01/2014 12:28:17:[/size]

en overbodige enters mogen niet in de header staan.

Wat is nu je script en wat is de restulteren de mail (zie bron van de mail in je mailclient)
Is vervelend, dan moet ik een apart bestandje maken (os.inc.php) want aan dit php bestand wordt veel gewerkt op een windows iis test server en daarna gekopieerd naar de Linux web server.

Script volgt nog ....
Rene Wennekes op 15/01/2014 12:32:33

Is vervelend, dan moet ik een apart bestandje maken (os.inc.php) want aan dit php bestand wordt veel gewerkt op een windows iis test server en daarna gekopieerd naar de Linux web server.
Met PHP_EOL zou dat nu juist overbodig moeten zijn. Daarom eerst even testen of dit de oplossing is, voordat je aan andere onderdelen van de mailheaders gaat sleutelen.

Kun je dan een voorbeeld geven van PHP_EOL en is deze dan voor beide versies hetzelfde?
Van dit:

$headers .= 'een of andere header' . "\r\n";
$headers .= 'een andere header' . "\r\n";

maak je:

$headers .= 'een of andere header' . PHP_EOL;
$headers .= 'een andere header' . PHP_EOL;

Omdat de constante PHP_EOL platform-afhankelijk is, wordt dan automatisch op een specifiek platform de juiste string voor een End Of Line (EOL) ingevuld.
Goed heb alle \n vervangen door PHP_EOL.
Helaas werkt de bijlage op beide nu niet meer.

Helemaal onderaan de output.

Overigens is dit een link van o.a. 1 van de bronnen die ik gebruikt heb.
http://www.texelate.co.uk/blog/send-email-attachment-with-php/

<?php
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$header .= "MIME-Version: 1.0".PHP_EOL;
$header .= "Content-Type: multipart/mixed".PHP_EOL;
$header .= "boundary=\"{$mime_boundary}\"";
$header .= PHP_EOL;

// multipart boundary
$message = "This is a multi-part message in MIME format.".PHP_EOL.PHP_EOL;
$message .= "--{$mime_boundary}".PHP_EOL;
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"".PHP_EOL;
$message .= "Content-Transfer-Encoding: 7bit".PHP_EOL.PHP_EOL.$bericht.PHP_EOL.PHP_EOL;
$message .= "--{$mime_boundary}".PHP_EOL;

// preparing attachments
$data = chunk_split(base64_encode($attach_file)); //No need file-get-content because pdf is generated.
$message .= "Content-Type: {\"application/octet-stream\"};".PHP_EOL;
$message .= "name=\"".$invoice_number.".pdf\"".PHP_EOL;
$message .= "Content-Disposition: attachment;".PHP_EOL;
$message .= "filename=\"".$invoice_number.".pdf\"".PHP_EOL;
$message .= "Content-Transfer-Encoding: base64".PHP_EOL.PHP_EOL.$data.PHP_EOL.PHP_EOL;
$message .= "--{$mime_boundary}".PHP_EOL;

//Sending
if(mail($email_invoice, "$subject", $message, $header)) {
print "<tr><td colspan='2'><br />You will receive a invoice email.</td></tr>\n";
} else {
print "<tr><td colspan='2'><br />There is a error sending the invoice email.</td></tr>\n";
}
?>


Output:

boundary="==Multipart_Boundary_xba62b5666df3404aa5393021e1c1ad89x"

This is a multi-part message in MIME format.

--==Multipart_Boundary_xba62b5666df3404aa5393021e1c1ad89x
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hier komt de tekst van de bericht-variabele.




--==Multipart_Boundary_xba62b5666df3404aa5393021e1c1ad89x
Content-Type: {"application/octet-stream"}; name="VAC31.pdf"
Content-Disposition: attachment;
filename="VAC31.pdf"
Content-Transfer-Encoding: base64

JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAy

IDAgUgovQ29udGVudHMgNCAwIFI+PgplbmRvYmoKNCAwIG9iago8PC9GaWx0ZXIgL0ZsYXRl
IDAgUgovQ29udGVudHMgNCAwIFI+RGVj

b2RlIC9MZW5ndGggMzcwPj4Kc3RyZWFtCnichZFBj9MwEIXv/RXvuHsZYid27N5aSpH2gEBEXQnt

xSVDNtvUXly3/fu4Qd0KhMJlpNG8783ojcTDrCBV4zxbNni3FpAVFQWaH/jQ5ImWNQqqVZmrrQRi

N5OGSoVaaTIKeyityZbXfsBX/Mza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GUERGIDEuNykKL0NyZWF0aW9uRGF0

ZSAoRDoyMDE0MDExNTEzMjQ1MykKPj4KZW5kb2JqCjggMCBvYmoKPDwKL1R5cGUgL0NhdGFsb2cK

L1BhZ2VzIDEgMCBSCj4+CmVuZG9iagp4cmVmCjAgOQowMDAwMDAwMDAwIDY1NTM1IGYgCjAw
L1BhZ2VzIDEgMCBSCj4+MDAw

MDA1MjcgMDAwMDAgbiAKMDAwMDAwMDgxMSAwMDAwMCBuIAowMDAwMDAwMDA5IDAwMDAwIG4gCjAw

MDAwMDAwODcgMDAwMDAgbiAKMDAwMDAwMDYxNCAwMDAwMCBuIAowMDAwMDAwNzE1IDAwMDAwIG4g

CjAwMDAwMDA5MjUgMDAwMDAgbiAKMDAwMDAwMTAwMCAwMDAwMCBuIAp0cmFpbGVyCjw8Ci9TaXpl

IDkKL1Jvb3QgOCAwIFIKL0luZm8gNyAwIFIKPj4Kc3RhcnR4cmVmCjEwNDkKJSVFT0YK



--==Multipart_Boundary_xba62b5666df3404aa5393021e1c1ad89x
maar hoe zien de headers er verder uit?

Laat ik een voorbeeld geven van een mailtje:

Return-Path: <[email protected]>
Received: from secure.halma.nl (secure.halma.nl [212.79.254.164])
	by server6.**-ict.nl (8.14.4/8.14.4) with ESMTP id s0FCN9vR021257
	for <phphulp@p***.nl>; Wed, 15 Jan 2014 13:23:09 +0100
Received: from www.phphulp.nl (unknown [212.79.254.176])
	by secure.halma.nl (Postfix) with ESMTPA id 31E337763A
	for <phphulp@p***.nl>; Wed, 15 Jan 2014 13:22:18 +0100 (CET)
Message-ID: <[email protected]>
Date: Wed, 15 Jan 2014 13:22:18 +0100
Subject: Nieuwe reactie op Email bijlage onder windows test server ok maar
 onder linux fout
From: PHPhulp <[email protected]>
To: Ivo <phphulp@p*****.nl>
MIME-Version: 1.0
Content-Type: multipart/alternative;
 boundary="_=_swift_v4_138978853852d67d7a314b3_=_"


--_=_swift_v4_138978853852d67d7a314b3_=_
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Beste Ivo,

Er is een nieuwe reactie geplaatst op het forum beric=
ht Email bijlage onder windows test server ok maar onder linux fout door=
 Rene Wennekes waarop jij ook hebt gereageerd. Om deze nieuwe reactie te=
 bekijken ga je naar:

http://www.phphulp.nl/php/forum/topic/ema=
il-bijlage-onder-windows-test-server-ok-maar-onder-linux-fout/93586/last=
/

Wil je deze of andere notificaties niet meer ontvangen, dan k=
un je dit als ingelogde gebruiker aanpassen op http://www.phphulp.nl/voo=
rkeuren/.

PHPhulp
http://www.phphulp.nl/

--_=_swift_v4_138978853852d67d7a314b3_=_
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<html>
<head>

....


Je ziet hier een witregel na regel 16
Dat is het einde van de headers.

Een deel van de headers (die received from etc.) die zijn gedurende het verstuur en aflever proces toegevoegd.

Ik vermoed een witregels in de headers die jij hebt.

[size=xsmall]Toevoeging op 15/01/2014 14:07:14:[/size]

Als ik een alternatief mag voorstellen?


<?php
include 'phpmailer.class.php';

$mailer = new PHPMailer();
  $mailer->Subject = 'Voorbeeldje';
  $mailer->SetFrom('[email protected]', 'Webmaster van MijnSite.nl');
  $mailer->AltBody = 'Dit is het bericht.'; 
  $mailer->Body = '<html><body><h1>in html</h1><p>kan het ook</p></body></html>');
  $mailer->AddAddress('[email protected]', 'footje bar'); 
  $mailer->AddCC('[email protected]', 'footje bar nr 2');
  $mailer->AddBCC('[email protected]', 'footje bar nr 3');
  $mailer->AddAttachment('/home/mywebsite/public_html/file.pdf', 'factuur.pdf');
  $mailer->Send();
?>
Dit zijn de headers (berichtopties outlook).

xnix:
==============================================================
Delivered-To: [email protected]
Received: by 10.180.77.101 with SMTP id r5csp273327wiw;
Wed, 15 Jan 2014 05:07:28 -0800 (PST)
X-Received: by 10.15.48.201 with SMTP id h49mr3460650eew.43.1389791247949;
Wed, 15 Jan 2014 05:07:27 -0800 (PST)
Return-Path: <[email protected]>
Received: from cpsmtp-fia02.kpnxchange.com ([191.121.247.5])
by mx.google.com with ESMTP id n47si7577370eey.245.2014.01.15.05.07.05
for <[email protected]>;
Wed, 15 Jan 2014 05:07:05 -0800 (PST)
Received-SPF: neutral (google.com: 191.121.247.5 is neither permitted nor denied by best guess record for domain of [email protected]) client-ip=191.121.247.5;
Authentication-Results: mx.google.com;
spf=neutral (google.com: 191.121.247.5 is neither permitted nor denied by best guess record for domain of [email protected]) [email protected]
Received: from raspberry3 ([80.61.205.8]) by cpsmtp-fia02.kpnxchange.com with Microsoft SMTPSVC(6.0.3790.4675);
Wed, 15 Jan 2014 14:05:29 +0100
Received: by raspberry3 (sSMTP sendmail emulation); Wed, 15 Jan 2014 14:05:29 +0100
Date: Wed, 15 Jan 2014 14:05:29 +0100
To: [email protected]
Subject: Faktuur VAC32
X-PHP-Originating-Script: 0:myvacancies.php
From: factuur VAC32 <[email protected]>
MIME-Version: 1.0
Content-Type: multipart/mixed;
Return-Path: [email protected]
Message-ID: <[email protected]>
X-OriginalArrivalTime: 15 Jan 2014 13:05:29.0858 (UTC) FILETIME=[7750DE20:01CF11F2]



Windoos
======================================================================================
Delivered-To: [email protected]
Received: by 10.180.77.101 with SMTP id r5csp273198wiw;
Wed, 15 Jan 2014 05:05:35 -0800 (PST)
X-Received: by 10.14.3.130 with SMTP id 2mr3569256eeh.36.1389791135421;
Wed, 15 Jan 2014 05:05:35 -0800 (PST)
Return-Path: <[email protected]>
Received: from cpsmtp-fia05.kpnxchange.com (cpsmtp-fia05.kpnxchange.com. [191.121.247.9])
by mx.google.com with ESMTP id d41si7668817eep.113.2014.01.15.05.05.35
for <[email protected]>;
Wed, 15 Jan 2014 05:05:35 -0800 (PST)
Received-SPF: neutral (google.com: 191.121.247.9 is neither permitted nor denied by best guess record for domain of [email protected]) client-ip=191.121.247.9;
Authentication-Results: mx.google.com;
spf=neutral (google.com: 195.121.247.9 is neither permitted nor denied by best guess record for domain of [email protected]) [email protected]
Received: from FS8 ([70.61.205.8]) by cpsmtp-fia05.kpnxchange.com with Microsoft SMTPSVC(6.0.3790.4675);
Wed, 15 Jan 2014 14:05:17 +0100
Date: Wed, 15 Jan 2014 14:05:21 +0100
Subject: Faktuur VAC51
To: [email protected]
From: Naam VAC51 <[email protected]>
MIME-Version: 1.0
Content-Type: multipart/mixed;
Return-Path: [email protected]
Message-ID: <[email protected]>
X-OriginalArrivalTime: 15 Jan 2014 13:05:17.0433 (UTC) FILETIME=[6FE8F690:01CF11F2]

Reageren