2013-04-16 8 views
7

Desidero creare file di posta elettronica nel formato .eml con più allegati in. Gli allegati sono generati e il contenuto degli allegati è corretto. Ma gli allegati vengono sempre in formato .dat ei nomi dei file vanno come "ATT00001", "ATT0002" .. etcNome allegato ed estensione file non funzionanti nell'email * .eml

Attualmente sto seguendo la soluzione dato in this stackoverflow question e il mio codice è il seguente

PHP

foreach($reports as $pdf){ 
     $attachment = file_get_contents("auto_report_attachments\\Template_Attachment.eml"); 
     $attachment = str_replace("TEMPLATE_MIME_TYPE", $pdf['type'], $attachment); 
     $attachment = str_replace("TEMPLATE_FILE_NAME", $pdf['filename'], $attachment); 
     $attachment = str_replace("TEMPLATE_ATTACHMENT_CONTENT", base64_encode($pdf['file']), $attachment); 

     $content .= $attachment; 
     unset($attachment); 
    } 

Template Allegato

--080107000800000609090108 
Content-Type: "TEMPLATE_MIME_TYPE" 
name="TEMPLATE_FILE_NAME" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; 
filename="TEMPLATE_FILE_NAME" 

TEMPLATE_ATTACHMENT_CONTENT 

$content è l'intestazione e il corpo dell'email principale come descritto nel link precedente. Il mio file .eml sembra;

MIME-Version: 1.0 
Date: Tue, 16 Apr 2013 09:03:37 +0100 
From: [email protected] 
To: [email protected] 
Subject: Email subject 
Content-Type: multipart/mixed; boundary="080107000800000609090108" 

This is a message with multiple parts in MIME format. 

--080107000800000609090108 
Content-Type: text/html 

<p><strong>Project Name: Some Project and the body continues...</p> 



--080107000800000609090108 
Content-Type: "application/pdf" 
name="AM22831 - - Cover Sheet.pdf" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; 
filename="AM22831 - - Cover Sheet.pdf" 

JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAyIDAgUgovQ29udGVudHMgNCAwICiUlRU9GCg== 



--080107000800000609090108 
Content-Type: "application/pdf" 
name="AM22831 - - Manufacturing Status.pdf" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; 
filename="AM22831 - - Manufacturing Status.pdf" 

cSAxMTMuMzkgMCAwIDMwLjUzIDE0LjE3IDU1MC41OCBjbSAvSTEgRG8gUQpxIDAuMDAwIDAuMDAwIDEuMDAwIHJnIEJUIDEzMC4zOSRU9GCg== 

--080107000800000609090108 

È possibile che questo base64 contenuti dà il giusto contenuto nel file PDF quando il file aperto selezionando per aprire con un PDF Reader. Ma i file non sono disponibili nel formato .pdf. Lo stesso accade per .xls, .doc e tutti gli altri tipi di file. Tutti i file sono disponibili nel formato .dat con denominazione standard anziché con i nomi specificati.

Si prega di aiutarmi a risolvere questo allegati venire nel richiesto file type con il nome del file specificato.

NB: Il contenuto base64 nel file di esempio .eml viene troncato

risposta

5

ho trovato la risposta la mia auto .. !! L'intestazione di Template_Attachment dovrebbe essere la seguente

--080107000800000609090108 
Content-Type: TEMPLATE_MIME_TYPE;name="TEMPLATE_FILE_NAME" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment;filename="TEMPLATE_FILE_NAME" 

TEMPLATE_ATTACHMENT_CONTENT 

Dove 080107000800000609090108 è il limite in questo caso e alla fine di tutti gli allegati, ci dovrebbe essere un altro 080107000800000609090108 per terminare la posta elettronica.

Spero che questo sarebbe utile a qualcuno un giorno :-)

PS: in modo che il file vero e proprio .eml sarebbe simile a quanto segue, che funziona perfettamente come dovrebbe fare

MIME-Version: 1.0 
Date: Tue, 16 Apr 2013 09:03:37 +0100 
From: [email protected] 
To: [email protected] 
Subject: Email subject 
Content-Type: multipart/mixed; boundary="080107000800000609090108" 

This is a message with multiple parts in MIME format. 

--080107000800000609090108 
Content-Type: text/html 

<p><strong>Project Name: Some Project and the body continues...</p> 

--080107000800000609090108 
Content-Type: application/pdf;name="AM22831 Cover Sheet.pdf" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment;filename="AM22831 Cover Sheet.pdf" 

JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAyIDAgUgovQ29udGVasdsDFDffjMBakdjKJHBzdHlsZT0iY29= 

--080107000800000609090108 
Content-Type: application/excel;name="AM22831 Manufacturing Status.xls" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment;filename="AM22831 Manufacturing Status.xls" 

DQoNCjx0YWJsZSBib3JkZXI9IjAiPg0KPHRyPg0KPHRkIGNvbHNwYW49IjMiIHJvd3NwYW49IjIiIGFsaWduPSJjZW50ZXIiPg0KICAgIDxoMSBzdHlsZT0iY29=