2012-04-05 8 views
0

添付ファイル付きのメールをmicrosoft exchange serverに送る機能があります。私の問題は、添付ファイルを追加するときは、メッセージ全体が添付ファイルのソースにテキストを追加することです。添付ファイルをメールの本文セクションに保存すると、添付ファイルを作成するのではなく、添付ファイルのソースが電子メール本文に書き込まれます。 Beloweは私の情報源です。php経由でメールをMicrosoft Exchangeに送る

$eol = "\r\n"; 

$boundary = md5(time()); 

    $mail = "[email protected]"; 

$headers = "From: [email protected]".$eol; 
$headers .= "MIME-Version: 1.0".$eol; //utworzenie headera wiadomosci 
$headers .= "Content-type: multipart/alternative; charset=utf-8".$eol; 
$headers .= "Message-ID:< [email protected]".$_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

あなたは本当にSwiftMailerのようなライブラリを使うべきです。 HTML電子メールを送信することは解決された問題です。 – ceejayoz

+0

梨のメールパッケージはあなたの人生を楽にします。 http://pear.php.net/package/Mail – Zac

答えて

4

(特に添付ファイルを扱うための)メールを送信するために本当に良いPHP libにはphpmailerのクラスです

あなたがそれをここに見つけることができます:http://code.google.com/a/apache-extras.org/p/phpmailer/

EDIT - 上記のリンクが古いのです。プロジェクトは、それが今のGithub上でホストされ、より定期的に維持されています:https://github.com/PHPMailer/PHPMailer

と添付ファイルを送信するためにそれを使用する方法の例:

include("class.phpmailer.php"); 
$mail = new PHPMailer();  
$mail->IsHTML(true); 
$mail->SetFrom('[email protected]'); 
$mail->AddReplyTo('[email protected]'); //set from & reply-to headers 
$mail->AddAddress('[email protected]'); //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(); 
+0

ありがとうございました!私はこのプロジェクトについて知りませんでした。 :) – Krystian

関連する問題