2012-02-10 101 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已经完成了什么,并在需要的地方使用这些代码片段。