2016-07-21 29 views
0

Ich möchte eine E-Mail mit einem PDF-Anhang mit PHP mail() senden. Das Problem ist, dass am Ende meine Anhangsdatei nicht lesbar ist, weil, anstatt sie von base64 zu entschlüsseln, ein Teil der Header angehängt wird. Meine Vermutung ist, dass ich Header falsch verkette, aber ich habe versucht, es auf verschiedene Arten zu ändern und das Ergebnis war immer das Gleiche.Anhang kann nicht ordnungsgemäß hinzugefügt werden - Teile der Header werden zur Datei hinzugefügt

Dies ist die letzten Mail-Inhalte, die ich im Klartext erhalten:

MIME-Version: 1.0 
From: XXX - xxxxx <[email protected]> 
Content-Type: multipart/mixed; boundary=_x27e6cd8de4c00e2104105d5353947a1b0ca2a770x 

This is a MIME encoded message. 

--_x27e6cd8de4c00e2104105d5353947a1b0ca2a770x 
Content-type: text/plain;charset=utf-8 

Verification Email 
To activate the account click on the following link or copy-paste it in your browser: 
http://10.24.35.92:443/?ctrl=users&act=verify&id=174&hash=4e4b5fbbbb602b6d35bea8460aa8f8e5 

--_x27e6cd8de4c00e2104105d5353947a1b0ca2a770x 
Content-type: text/html;charset=utf-8 

<h4>Verification Email</h4> 
<table> 
<tr><td>To activate the account click on the following link or copy-paste it in your browser:</td></tr> 
<tr><td>http://10.24.35.92:443/?ctrl=users&act=verify&id=174&hash=4e4b5fbbbb602b6d35bea8460aa8f8e5</td></tr> 

--_x27e6cd8de4c00e2104105d5353947a1b0ca2a770x 
Content-Type: application/pdf; name="xxxx.pdf"; 
Content-Transfer-Encoding: base64; 
Content-Disposition: attachment; filename="xxxx.pdf"; 

(here goes the encoded attachment) 
JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFu 
MDAwMCBuDQowMDAwMTUzODUwIDAwMDAwIG4NCjAwMDAxNzM4NzUgMDAwMDAgbg0KMDAwMDE3NDEw 
MSAwMDAwMCBuDQp0cmFpbGVyDQo8PC9TaXplIDIxOS9Sb290IDEgMCBSL0luZm8gMzEgMCBSL0lE 
(...) 

--_x27e6cd8de4c00e2104105d5353947a1b0ca2a770x-- 

und dies ist, wie die PDF-Anlage wie in Texteditor sieht:

Content-Transfer-Encoding: base64; 

Content-Disposition: attachment; filename="xxxx.pdf" 

JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFu 
Zyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDMyIDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4+ 
(...) 

Antwort

0

Sie müssen die mehren verwenden/gemischter MIME-Typ So etwas sollte funktioniert:

<form name="form1" enctype="multipart/form-data" method="post" action=""> 
<label for="name">Your Name</label> 
<input type="text" id="name" name="name" /> 
<label for="email">Your Email</label> 
<input type="email" id="email" name="email" /> 
<label for="myfile">Attachment</label> 
<input type="file" id="myfile" name="my_file" /> 
<input type="submit" name="button" value="Submit" /> 
</form> 

PHP CODE

if($_POST && isset($_FILES['my_file'])){ 

$from_email = '[email protected]'; 
$to = '[email protected]'; 
$subject = 'Test mail with attachment'; 
$message = 'This is body of the message'; 

//get file details we need 
$file_tmp_name = $_FILES['my_file']['tmp_name']; 
$file_name  = $_FILES['my_file']['name']; 
$file_size  = $_FILES['my_file']['size']; 
$file_type  = $_FILES['my_file']['type']; 
$file_error  = $_FILES['my_file']['error']; 

$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL); 

if($file_error>0) 
{ 
    die('upload error'); 
} 
//read from the uploaded file 
$handle = fopen($file_tmp_name, "r"); 
$content = fread($handle, $file_size); 
fclose($handle); 
//encode it with MIME base64, and split it into smaller chunks 
$encoded_content = chunk_split(base64_encode($content)); 

//create a boundary string. It must be unique, you can use the MD5 algorithm to generate a random hash 
$boundary = md5(date('r', time())); 
//header 
$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "From:".$from_email."\r\n"; 
$headers .= "Reply-To: ".$user_email."" . "\r\n"; 
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 

//plain text 
$body = "--$boundary\r\n"; 
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n"; 
$body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
$body .= chunk_split(base64_encode($message)); 

//attachment 
$body .= "--$boundary\r\n"; 
$body .="Content-Type: $file_type; name="$file_name"\r\n"; 
$body .="Content-Disposition: attachment; filename="$file_name"\r\n"; 
$body .="Content-Transfer-Encoding: base64\r\n"; 
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
$body .= $encoded_content; 

$sentMail = @mail($recipient_email, $subject, $body, $headers); 
if($sentMail) //output success or failure messages 
{  
    die('Thank you for your email'); 
}else{ 
    die('Could not send mail! Please check your PHP mail configuration.'); 
} 

} 

Hoffe, es hilft.

Prost, Vince.

+0

Ich verwende mehrteilige/gemischte MIME-Typ in der 3. Zeile. –

+0

Ah richtig. Probieren Sie diesen Code aus. Ansonsten fügen Sie Ihren Code ein, damit ich einen Blick darauf werfen kann. – Vince