2012-02-10 102 views
2

有了這個代碼:PHP:發送郵件與附加

$Message = 'TEXT'; 
    $boundary = "---"; 
    $headers = "From: $from\nX-Mailer: Carline Server www.carline.ru"; 
    $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\""; 
    $body = "--$boundary\n"; 
    $body .= "Content-type: text/html; charset='utf-8'\n"; 
    $body .= "Content-Transfer-Encoding: quoted-printable\n\n"; 
    $body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($_FILES['photo1']['name'])."?=\n\n"; 
    $body .= $Message."\n"; 
    $body .= "--$boundary\n"; 
    $text = file_get_contents($_FILES['photo1']['tmp_name']); 
    $body .= "Content-Type: application/octet-stream; name==?utf-8?B?".base64_encode($_FILES['photo1']['name'])."?=\n"; 
    $body .= "Content-Transfer-Encoding: base64\n"; 
    $body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($_FILES['photo1']['name'])."?=\n\n"; 
    $body .= chunk_split(base64_encode($text))."\n"; 
    $body .= "--".$boundary ."--\n"; 


    mail($AdminEmail, "RE:", $body, $headers); 

它發送的郵件正文

Content-Type: application/octet-stream; name==?utf-8?B?MDAwMDU3ODcuanBn?= 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; filename==?utf-8?B?MDAwMDU3ODcuanBn?= 

/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcG 
BwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwM 
DAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAGQAZADASIA 
... 

一個文件的內容如何更改發送文件作爲附件的代碼?

回答

1

從php.net @http://php.net/manual/en/function.mail.php

發送多附件的電子郵件

<?php 
function multi_attach_mail($to, $files, $sendermail){ 
// email fields: to, from, subject, and so on 
$from = "Files attach <".$sendermail.">"; 
$subject = date("d.M H:i")." F=".count($files); 
$message = date("Y.m.d H:i:s")."\n".count($files)." attachments"; 
$headers = "From: $from"; 

// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

// multipart boundary 
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 

// preparing attachments 
for($i=0;$i<count($files);$i++){ 
    if(is_file($files[$i])){ 
     $message .= "--{$mime_boundary}\n"; 
     $fp = @fopen($files[$i],"rb"); 
    $data = @fread($fp,filesize($files[$i])); 
       @fclose($fp); 
     $data = chunk_split(base64_encode($data)); 
     $message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" . 
     "Content-Description: ".basename($files[$i])."\n" . 
     "Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" . 
     "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
     } 
    } 
$message .= "--{$mime_boundary}--"; 
$returnpath = "-f" . $sendermail; 
$ok = @mail($to, $subject, $message, $headers, $returnpath); 
if($ok){ return $i; } else { return 0; } 
} 
?> 
0

$header = "From: ".$from_name." <".$from_mail.">\r\n"; 
    $header .= "Reply-To: ".$replyto."\r\n"; 
    $header .= "MIME-Version: 1.0\r\n"; 
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; 
    // content type: mixed or related (better mixed for attachments 
    $header .= "This is a multi-part message in MIME format.\r\n"; 
    $header .= "--".$uid."\r\n"; 
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; 
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
    $header .= $message."\r\n\r\n"; 
    $header .= "--".$uid."\r\n"; 
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; 
    // use different content types here 
    $header .= "Content-Transfer-Encoding: base64\r\n"; 
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; 
    $header .= $content."\r\n\r\n"; 
    $header .= "--".$uid."--"; 
0

您應該使用預先編寫的庫的頭結構的例子(如http://swiftmailer.org/)至發送電子郵件,尤其是有附件的人,無論是否內聯。

構造有效的和跨郵件客戶端支持的消息是相當先進的任務,如果你以前從未這樣做過。那麼爲什麼當有人爲你完成這項工作時花時間重新發明了呢?

從另一端提出這個問題,如果你想建立你自己的庫,分析SwiftMailer已經完成了什麼,並在需要的地方使用這些代碼片段。