0

PHPを使用して添付ファイルを電子メールで送信していますが、電子メールクライアント(他の電子メール添付ファイルを含む)を除いてすべて期待どおりに機能します。クリックするだけで、外部アプリケーションを起動してファイルを表示するか、少なくともプログラムを選択して表示するオプションがあります。添付ファイルをクリックしても何も起こらないため、これは表示されません。ダウンロードして表示できますが、期待どおりに機能します。

ヘッダーに何かが足りないかどうか知りたかった。

これが私の関数です(クラス内にあります):

public function mail() {
    if(!empty($this->attachment)) {
        $filename   = empty($this->attachment_filename) ? basename($this->attachment) : $this->attachment_filename;
        $path       = dirname($this->attachment);
        $mailto     = $this->to;
        $from_mail  = $this->from;
        $from_name  = $this->from_name;
        $replyto    = $this->reply_to;
        $subject    = $this->subject;
        $message    = $this->message;

        $file       = $path.'/'.$filename;
        $file_size  = filesize($file);
        $handle     = fopen($file, "r");
        $content    = fread($handle, $file_size);
        fclose($handle);
        $content    = chunk_split(base64_encode($content));
        $uid        = md5(uniqid(time()));
        $name       = basename($file);

        $mime_type  = $this->getMimeType($file); // function returns the MIME type

        $header  = "From: ".$from_name." <".$from_mail.">\r\n";
        $header .= "Reply-To: ".$replyto."\r\n";
        $header .= "MIME-Version: 1.0\r\n";
        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
        $header .= "This is a multi-part message in MIME format.\r\n";
        $header .= "--".$uid."\r\n";
        $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $header .= $message."\r\n\r\n";
        $header .= "--".$uid."\r\n";
        $header .= "Content-Type: ".$mime_type."; name=\"".$filename."\"\r\n";
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
        $header .= $content."\r\n\r\n";
        $header .= "--".$uid."--";

        return (mail($mailto, $subject, "", $header) ? true : false);
    } else {
        $header  = "From: ".($this->from_name)." <".($this->from).">\r\n";
        $header .= "Reply-To: ".($this->reply_to)."\r\n";

        return (mail($this->to, $this->subject, $this->message, $header) ? true : false);
    }
}

私がそれをどのように呼んでいるのか(これは機能し、期待どおりに添付ファイル付きの電子メールを送信します)

$sendit = new MailAttachment(
    $to, 
    $subject, 
    $message, 
    $excel_report,
    basename($excel_report)
);

if(!$sendit->mail()) {
    return 'Error';
}
4

1 に答える 1

0

さて、すべてが期待どおりに機能していることがわかりました。メールクライアントの問題はファイル拡張子です。

  • 拡張子が.xlsのファイルは、電子メールクライアントからダブルクリックすると開きます。
  • 拡張子が.xlsxのファイルは、電子メールクライアントからダブルクリックしても開かないため、ダウンロードして開く必要があります。

これが誰かを助けることを願っています。

于 2011-02-22T14:45:26.807 に答える