0

添付ファイル付きのメールをMicrosoftExchangeServerに送信する機能があります。私の問題は、添付ファイルを追加したいときに、メッセージ部分全体が添付ファイルのソースにテキストを追加していることです。メールの本文セクションに添付ファイルを保存すると、添付ファイルを作成する代わりに、添付ファイルのソースが電子メールの本文に書き留められます。以下は私の情報源です。

$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 に答える