0

我有功能将带有附件的邮件发送到 Microsoft Exchange 服务器。我的问题是,当我想添加附件时,整个消息部分正在将我的文本添加到附件源。当我将附件保存到邮件的正文部分时,我的附件源会记录在电子邮件正文中,而不是创建附件。下面是我的来源。

$eol = "\r\n";

$boundary = md5(time());

    $mail = "explame@explame.com";  

$headers  = "From: no-replay@explame.com".$eol;
$headers .= "MIME-Version: 1.0".$eol; //utworzenie headera wiadomosci
$headers .= "Content-type: multipart/alternative; charset=utf-8".$eol;
$headers .= "Message-ID:< TheSystem@".$_SERVER['SERVER_NAME'].">".$eol; 
$headers .= "X-Mailer: PHP v".phpversion().$eol;
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= "Content-Type: multipart/related; boundary=\"".$boundary."\"".$eol; 
$headers .= "--$boundary".$eol;
$headers .= "Content-Type: text/plain; charset=utf-8".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol;
$headers .= "--$boundary--".$eol.$eol;

if ($file != ''){
    $handle = fopen($file['tmp_name'], 'rb');
    $f_content = fread($handle, $file['size']);
    $attachment =  chunk_split(base64_encode($f_content));
    fclose($handle);

    $content .= "--$boundary".$eol;
    $content .= "Content-type: ".$file['type'].'; '.'name="'.$file['name'].'"'.$eol;
    $content .= 'Content-Disposition: attachment; filename="'.$file['name'].'"'.$eol.$eol;
    $content .= "Content-Transfer-Encoding: base64".$eol;
    $content .= $attachment.$eol.$eol;
    $content .= "--$boundary--".$eol.$eol;

    }
mail($mail, 'title', $content, $headers)

我想我什么都试过了,但对我没有用。:(

4

1 回答 1

5

一个非常好的用于发送邮件(尤其是处理附件)的 PHP 库是 phpmailer 类。

你可以在这里找到它:http ://code.google.com/a/apache-extras.org/p/phpmailer/

编辑 - 上面的链接是旧项目,它现在托管在 Github 上并且更经常地维护:https ://github.com/PHPMailer/PHPMailer

以及如何使用它发送附件的示例:

include("class.phpmailer.php");
$mail = new PHPMailer();    
$mail->IsHTML(true);
$mail->SetFrom('from@mydomain.com');
$mail->AddReplyTo('from@mydomain.com'); //set from & reply-to headers
$mail->AddAddress('to@exchangeserver.com'); //set destination address

$mail->Subject="some subject"; //set subject
$mail->Body="some body HTML <br/><br/>"; //set body content

$mail->AddAttachment('filepath', 'filename'); //attach file

$mail->AltBody = "Can't see this message? Please view in HTML\n\n";
$mail->Send();
于 2012-04-05T13:36:06.900 回答