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");