通过php发送邮件到microsoft exchange

时间:2012-04-05 13:29:38

标签: php email

我有向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)

我想我尝试了一切,但对我来说没有任何作用。 :(

1 个答案:

答案 0 :(得分:4)

用于发送邮件(尤其是处理附件)的非常好的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();