Emails mit Anhang über PHP

Bash, Shell, PHP, Python, Perl, CGI
Post Reply
fettehenne
Posts: 62
Joined: 2003-05-08 15:20
Location: Hannover
 

Emails mit Anhang über PHP

Post by fettehenne »

Hallo,

ich habe folgendes Problem:
Ich habe ein PHP-Skript welches PDFs generiert und diese dann per Email versendet. Bisher mit Suse 8.1 , PHP 4.2.1 hat es ohne Probleme funktioniert. Ich konnte SIe dann mit Outlook öffnen und den Anhang auf meinem Client -PC @ home speichern.

Jetzt habe ich das Skript unter Debian mit PHP 4.1.2 getestet und es versendet auch die Emails jedoch sind Sie in Outlook kaputt. D.h. ich kann keinen Anhang mehr speichern da keiner da ist. Besser gesagt der Anhang wird mit irgendwelchen kryptischen Zeichen als Textinhalt gezeigt. Ã?ffne ich diese Email unter Webmail von Confixx so kann ich das PDF-File problemlos speichern.

Ich dachte zuerst das könnte etwas mit MIME zu tun haben und habe in der httpd.conf gesehen das die Module mime_module und mime_magic_module included werden. Scheint also alles OK zu sein.

Jemand ne Vermutung womit es zusammenhängen könnte?
dodolin
Posts: 3840
Joined: 2003-01-21 01:59
Location: Sinsheim/Karlsruhe
Contact:
 

Re: Emails mit Anhang über PHP

Post by dodolin »

Ich habe ein PHP-Skript welches PDFs generiert und diese dann per Email versendet.
Wie???

Und wie sieht der Header einer solchen Mail aus?

Sind heute wieder Ratespielchen? ;)
fettehenne
Posts: 62
Joined: 2003-05-08 15:20
Location: Hannover
 

Re: Emails mit Anhang über PHP

Post by fettehenne »

Ich benutze die Funktion:

Code: Select all

function SendMail($From,$FromName,$To,$ToName,$Subject,$Text,$Html,$AttmFiles){
 $OB="----=_OuterBoundary_000";
 $IB="----=_InnerBoundery_001";
 $Html=$Html?$Html:preg_replace("/n/","{br}",$Text)
  or die("neither text nor html part present.");
 $Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail.";
 $From or die("sender address missing");
 $To or die("recipient address missing");
   
 $headers ="MIME-Version: 1.0rn";
 $headers.="From: ".$FromName." <".$From.">n";
 $headers.="To: ".$ToName." <".$To.">n";
 $headers.="Reply-To: ".$FromName." <".$From.">n";
 $headers.="X-Priority: 1n";
 $headers.="X-MSMail-Priority: Highn";
 $headers.="X-Mailer: My PHP Mailern";
 $headers.="Content-Type: multipart/mixed;ntboundary="".$OB.""n";

 //Messages start with text/html alternatives in OB
 $Msg ="This is a multi-part message in MIME format.n";
 $Msg.="n--".$OB."n";
 $Msg.="Content-Type: multipart/alternative;ntboundary="".$IB.""nn";

 //plaintext section
 $Msg.="n--".$IB."n";
 $Msg.="Content-Type: text/plain;ntcharset="iso-8859-1"n";
 $Msg.="Content-Transfer-Encoding: quoted-printablenn";
 // plaintext goes here
 $Msg.=$Text."nn";

 // html section
 $Msg.="n--".$IB."n";
 $Msg.="Content-Type: text/html;ntcharset="iso-8859-1"n";
 $Msg.="Content-Transfer-Encoding: base64nn";
 // html goes here
 $Msg.=chunk_split(base64_encode($Html))."nn";

 // end of IB
 $Msg.="n--".$IB."--n";

 // attachments
 if($AttmFiles){
  foreach($AttmFiles as $AttmFile){
   $patharray = explode ("/", $AttmFile);
   $FileName=$patharray[count($patharray)-1];
   $Msg.= "n--".$OB."n";
   $Msg.="Content-Type: application/octetstream;ntname="".$FileName.""n";
   $Msg.="Content-Transfer-Encoding: base64n";
   $Msg.="Content-Disposition: attachment;ntfilename="".$FileName.""nn";
           
   //file goes here
   $fd=fopen ($AttmFile, "r");
   $FileContent=fread($fd,filesize($AttmFile));
   fclose ($fd);
   $FileContent=chunk_split(base64_encode($FileContent));
   $Msg.=$FileContent;
   $Msg.="nn";
  }
 }
   
 //message ends
 $Msg.="n--".$OB."--n";
 mail($To,$Subject,$Msg,$headers);   
 //syslog(LOG_INFO,"Mail: Message sent to $ToName <$To>");
}
flo
Posts: 2223
Joined: 2002-07-28 13:02
Location: Berlin
 

Re: Emails mit Anhang über PHP

Post by flo »

Schick doch bitte mal den geparsten Header aus der Mail - das kann sich sonst keine alte Sau anschauen - so kann es am Script, an der Variablen usw. liegen ...

flo.
fettehenne
Posts: 62
Joined: 2003-05-08 15:20
Location: Hannover
 

Re: Emails mit Anhang über PHP

Post by fettehenne »

Gern doch, sorry:

Outlook Ausgabe:

Code: Select all

From: XXX <c@xxx.de>
To: xxx@xxx.de <xxx@xxx.de>
Reply-To: xXx <xxx@xxx.de>
X-Priority: 1
X-MSMail-Priority: High
X-Mailer: My PHP Mailer
Content-Type: multipart/mixed;
	boundary="----=_OuterBoundary_000"
Message-Id: <20040117210028.0B604620066@xxxxxx.com>
Date: Sat, 17 Jan 2004 22:00:28 +0100 (CET)
X-UIDL: 1,,!!)?'#!k<h!!:^P"!


This is a multi-part message in MIME format.

------=_OuterBoundary_000
Content-Type: multipart/alternative;
	boundary="----=_InnerBoundery_001"


------=_InnerBoundery_001
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

bla bla bla bla


------=_InnerBoundery_001
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: base64

PGZvbnQgY29sb3I9cmVkIGZhY2U9YXJpYWw+TmV1ZSBSZWNobnVuZzogMDEtVzAxLTAxPC9mb250

Pg==




------=_InnerBoundery_001--

------=_OuterBoundary_000
Content-Type: application/octetstream;
	name="datei.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
	filename="datei.pdf"

JVBERi0xLjMKJeLjz9MKCjEgMCB
.....
...jetzt kommen paar tausend zeilen...
...
eHJlZgo5MTQ0CiUlRU9GCg==




------=_OuterBoundary_000--

Und das ist der ganze Quellcode der Email (über Confixx Webmail):

Code: Select all

Return-Path: <www-data@xxxxxxx.com>
Delivered-To: web3p1@xxxxxxx.com
Received: by xxxxxxx.com (Postfix, from userid 33)
	id 6CA87620066; Sat, 17 Jan 2004 22:10:16 +0100 (CET)
To: xxx@xxx.de
Subject: Titel
MIME-Version: 1.0

From: XXX <xxx@xxx.de>
To: xxx@xxx.de <xxx@xxx.de>
Reply-To: XXX <xxx@xxx.de>
X-Priority: 1
X-MSMail-Priority: High
X-Mailer: My PHP Mailer
Content-Type: multipart/mixed;
	boundary="----=_OuterBoundary_000"
Message-Id: <20040117211016.6CA87620066@xxx.com>
Date: Sat, 17 Jan 2004 22:10:16 +0100 (CET)
X-UIDL: HCo!!m=1!!*%'!!YYI!!
Status: RO


This is a multi-part message in MIME format.

------=_OuterBoundary_000
Content-Type: multipart/alternative;
	boundary="----=_InnerBoundery_001"


------=_InnerBoundery_001
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

bla bla bla bla


------=_InnerBoundery_001
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: base64

PGZvbnQgY29sb3I9cmVkIGZhY2U9YXJpYWw+TmV1ZSBSZWNobnVuZzogMDEtVzAxLTAxPC9mb250

Pg==




------=_InnerBoundery_001--

------=_OuterBoundary_000
Content-Type: application/octetstream;
	name="datei.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
	filename="datei.pdf"

JVBERi0xLjMKJeLjz9MK
.....
...jetzt kommen paar tausend zeilen...
...
eHJlZgo5MTQ0CiUlRU9GCg==




------=_OuterBoundary_000--
Und so sieht der Quellcode der Email aus, auf dem Server wo es funktioniert:

Code: Select all

Return-Path: <xxx@xxx.com>
Received: from xxx.com (localhost [127.0.0.1])
     by xxx.com (8.12.7/8.12.7/SuSE Linux 0.6) with ESMTP id hA2HqeNf013205
     for <info@xxx.de>; Sun, 2 Nov 2003 18:52:43 +0100
Received: (from wwwrun@localhost)
     by xxx.com (8.12.7/8.12.7/Submit) id hA2Hqeel013204;
     Sun, 2 Nov 2003 18:52:40 +0100
Date: Sun, 2 Nov 2003 18:52:40 +0100
Message-Id: <200311021752.hA2Hqeel013204@xxx.com>
To: info@xxx.de
Subject: Titel
MIME-Version: 1.0
From: <info@xxx.de>
To: <info@xxx.de>
Reply-To: <info@xxx.de>
X-Priority: 1
X-MSMail-Priority: High
X-Mailer: My PHP Mailer
Content-Type: multipart/mixed;
     boundary="----=_OuterBoundary_000"
X-UIDL: R![!!?[O!!`*!!$%3"!
Status: RO


This is a multi-part message in MIME format.

------=_OuterBoundary_000
Content-Type: multipart/alternative;
     boundary="----=_InnerBoundery_001"


------=_InnerBoundery_001
Content-Type: text/plain;
     charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

bla bla bla bla


------=_InnerBoundery_001
Content-Type: text/html;
     charset="iso-8859-1"
Content-Transfer-Encoding: base64

PGZvbnQgZmFjZT1hcmlhbD5IaWVybWl0IGVyaGFsdGVuIFNpZSBJaHJlIERhdGVuYmFua3NpY2hl
cnVuZy48L2ZvbnQ+
....
tausende von zeiledn
...
D+N/2LmQ1GhUAAA==



------=_OuterBoundary_000--



------=_InnerBoundery_001--

------=_OuterBoundary_000
Content-Type: application/octetstream;
     name="datei.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
     filename="datei.pdf"

H4sIAGdEpT8AA6V
dodolin
Posts: 3840
Joined: 2003-01-21 01:59
Location: Sinsheim/Karlsruhe
Contact:
 

Re: Emails mit Anhang über PHP

Post by dodolin »

Content-Type: application/octetstream;
name="datei.pdf"
Ich hasse es auch immer, wenn irgendwelche <zensiert> meinen, jegliche Attachments als application/octetsrteam schicken zu müssen. Ist ja auch kein Wunder, dass das nicht funkioniert: application/octetstream kann genau alles und nichts sein. Der einzige bescheuerte Client, der anhand der Dateiendungen funktioniert ist wohl Outlook/Outlook Express bzw. Windows selbst. Korrekt funktionierende Clients wissen nicht, was sie damit anfangen sollen.
fettehenne
Posts: 62
Joined: 2003-05-08 15:20
Location: Hannover
 

Re: Emails mit Anhang über PHP

Post by fettehenne »

Hä? Also vorher hat es ja Outlook geschluckt. Erst nach dem Serverumzug waren die Emails kaputt. Also scheint Content-Type: application/octetstream; nichts direkt mit meinem Problem zu tun zu haben. Oder habe ich dich jetzt falsch verstanden?

Was schlägst du als Alternative statt application/octetstream vor? Versandt werden sollen .pdf und .gz Dateien.
dodolin
Posts: 3840
Joined: 2003-01-21 01:59
Location: Sinsheim/Karlsruhe
Contact:
 

Re: Emails mit Anhang über PHP

Post by dodolin »

Also scheint Content-Type: application/octetstream; nichts direkt mit meinem Problem zu tun zu haben.
Das sehe ich anders.
http://www.karlsruhe.org/rfc/rfc2046.txt
The recommended action for an implementation that receives an
"application/octet-stream" entity is to simply offer to put the data
in a file, with any Content-Transfer-Encoding undone, or perhaps to
use it as input to a user-specified process.
Wie gesagt, wenn es "nicht funktioniert", dann "funktioniert" es korrekt.
Was schlägst du als Alternative statt application/octetstream vor? Versandt werden sollen .pdf und .gz Dateien.

Code: Select all

dominik@trinity:~$ grep pdf /etc/apache/mime.types
application/pdf                                 pdf
Für gz ist etwas schwierig, eventuell application/x-gzip. Das Problem ist, dass gzip selbst nur ein Encoding für irgendwelche anderen Daten ist. Für gtar, tgz und taz kann man jedenfalls application/x-gtar nehmen.
fettehenne
Posts: 62
Joined: 2003-05-08 15:20
Location: Hannover
 

Re: Emails mit Anhang über PHP

Post by fettehenne »

Ich habe das bei PDFs mit application/pdf; probiert jedoch gleicher Fehler. In meiner /etc/mailcap steht:

Code: Select all

text/plain; less '%s'; needsterminal                                                                                                
application/x-troff-man; /usr/bin/nroff -mandoc -Tlatin1; copiousoutput; print=/usr/bin/nroff -mandoc -Tlatin1 | print text/plain:- 
text/plain; more '%s'; needsterminal                                                                                                
text/plain; view '%s'; edit=vim '%s'; compose=vim '%s'; needsterminal                                                               
text/html; /usr/bin/lynx -force_html '%s'; needsterminal; description=HTML Text; nametemplate=%s.html                               
text/html; /usr/bin/lynx -dump -force_html '%s'; copiousoutput; description=HTML Text; nametemplate=%s.html                         
text/*; less '%s'; needsterminal                                                                                                    
text/*; view '%s'; edit=vim '%s'; compose=vim '%s'; needsterminal                                                                   
text/*; more '%s'; needsterminal                                                                                                    
application/x-debian-package; /usr/lib/mime/debian-view '%s'; needsterminal; description=Debian GNU/Linux Package; nametemplate=%s.d
audio/basic; /usr/lib/mime/playaudio '%s'; description=Basic uLaw Audio; nametemplate=%s.au 
Müsste die vielleicht ein bischen erweitert werden?
fettehenne
Posts: 62
Joined: 2003-05-08 15:20
Location: Hannover
 

Re: Emails mit Anhang über PHP

Post by fettehenne »

Code: Select all

application/pdf                                 pdf
ist in meiner auch drin.

Ich habe eben mal auf dem Server geschaut bei dem es funktionert und daraus folgende Zeilen in meine mailcap kopiert:

Code: Select all

application/x-gunzip; gunzip -c %s; copiousoutput                                                                                   
application/x-gzip; gunzip -c %s; copiousoutput                                                                                     
application/x-bunzip2; bunzip2 -c %s; copiousoutput                                                                                 
application/x-tar-gz; gunzip -c %s | tar -tf -; copiousoutput 
Postfixx und Apache durchgestartet , immernoch gleicher Fehler :(

Ich glaube langsam das hat eher was mit dem AUfbau und der Reihenfolge des Headers weiter oben zu tun. Aber was genau? Ohhh mann, langsam macht das hier wirklich keinen Spass mehr *heul* Sitze schon den ganzen Tag an dieser Geschichte .....
fettehenne
Posts: 62
Joined: 2003-05-08 15:20
Location: Hannover
 

Re: Emails mit Anhang über PHP

Post by fettehenne »

Des Rätsels Lösung:

In der folgenden Zeile der PHP-Funktion war das 'r' zuviel. Anscheinend ist da PHP 4.1.2 bischen sensibler als PHP 4.2.1. Naja muss man erst mal wissen :)

Code: Select all

$headers ="MIME-Version: 1.0rn";


Hier richtig:

Code: Select all

$headers ="MIME-Version: 1.0n";
flo
Posts: 2223
Joined: 2002-07-28 13:02
Location: Berlin
 

Re: Emails mit Anhang über PHP

Post by flo »

Ich hatte bei mir ein Problem bei Verwendung von Cyrus-Mailbox-Dateien, da diese anscheinend Windows-Zeilenenden benutzten.

Ich speicher mir Deine beiden Dateien mal ab und lasse mal diff drüber, wird aber nachts ...

flo.
andreask2
Posts: 696
Joined: 2004-01-27 14:16
Location: Aachen
 

Re: Emails mit Anhang über PHP

Post by andreask2 »

Hi!

Ein kleiner Tipp am Rande:

http://pear.php.net/manual/en/package.m ... xample.php

Warum das Rad neu erfinden...

Grüße
Andreas
Post Reply