【问题标题】:send mail via php to microsoft exchange通过php发送邮件到microsoft exchange
【发布时间】:2013-11-15 21:09:03
【问题描述】:

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

$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)

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

【问题讨论】:

  • 你真的应该使用像 SwiftMailer 这样的库。发送 HTML 电子邮件是一个已解决的问题。
  • 梨邮包会让你的生活轻松很多。 pear.php.net/package/Mail

标签: php email


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-12-28
    • 2013-11-12
    • 2020-01-06
    • 2013-06-05
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    相关资源
    最近更新 更多