3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

phpでzip圧縮、パスワード付与、メール送信

Last updated at Posted at 2020-06-16

zip圧縮

function zip($zipname, $file, $password) {
        $zip = new ZipArchive;
        $zip->open($zipname , ZipArchive::CREATE|ZipArchive::OVERWRITE);
        // パスワード設定
        $zip->setPassword($password);
        $zip->addFile($file);
        $zip->setEncryptionName($file, ZipArchive::EM_AES_256);
        $zip->close();
}

zip("a.zip", "sample.txt", "uoGhie6s");

メール送信

function sendMail( $to=null, $subject=null, $text=null, $file=null){

        //初期化
        $res = false;

        //日本語の使用宣言
        mb_language("ja");
        mb_internal_encoding("UTF-8");

        if( $to === null || $subject === null || $text === null ) {
                return false;
        }

        // 送信元の設定
        $sender_email = 'from@localhost';

        // ヘッダー設定
        $header = '';
        $header .= "From: ".$sender_email."\n";
        $header .= "Content-Type: multipart/mixed;boundary=\"__BOUNDARY__\"\n";

        // テキストメッセージを記述
        $body = "--__BOUNDARY__\n";
        $body .= "Content-Type: text/plain; charset=\"ISO-2022-JP\"\n\n";
        $body .= $text . "\n";
        $body .= "--__BOUNDARY__\n";

        // ファイルを添付
        $body .= "Content-Type: application/octet-stream; name=\"{$file}\"\n";
        $body .= "Content-Disposition: attachment; filename=\"{$file}\"\n";
        $body .= "Content-Transfer-Encoding: base64\n";
        $body .= "\n";
        $body .= chunk_split(base64_encode(file_get_contents($file)));
        $body .= "--__BOUNDARY__--";

        //メール送信
        $res = mb_send_mail( $to, $subject, $body, $header);

        return $res;
}

$result = sendMail("to@localhost", "subject", "bodyです", "a.zip");
3
5
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?