RSSで取得した複数の画像をmb_send_mailで自動送信

画像もforeachで回します。

$rss = simplexml_load_file('https://jp.techcrunch.com/feed/');

mb_language("Japanese");
mb_internal_encoding("UTF-8");

$to = "notwatch@gmail.com";
$date = date("m月d日");
$subject = "今日のTech Chrunch (" . $date.")";

$mime_type = "application/octet-stream";

$boundary = "__BOUNDARY__";

$additional_headers = "Content-Type: multipart/mixed;boundary=\"".$boundary."\"\n";
$message = "--" . $boundary . "\n";
$message .= "Content-Type: text/plain; charset=\"ISO-2022-JP\"\n\n";
foreach($rss->channel->item as $value){
	$message .= $value->title ."\n";
	$message .= $value->link . "\n";
}
$message .="--" . $boundary . "\n";

$i = 0;
foreach($rss->channel->item as $value){
	$url = $value->children('media', true)->thumbnail->attributes()->url;
	$data = file_get_contents($url);
	$pass = "img/tech".$i.".jpg";
	file_put_contents($pass, $data);
	$message .="Content-Type:". $mime_type . "; name=\"" .basename($pass) . "\"\n";
	$message .="Content-Disposition: attachment; filename=\"" .basename($pass) ."\"\n";
	$message .="Content-Transfer-Encoding: base64\n";
	$message .= "\n";
	$message .= chunk_split(base64_encode(file_get_contents($pass)))."\n";
	$message .="--" . $boundary . "\n";
	$i++;	
}

mb_send_mail($to, $subject, $message, $additional_headers);

あれ? これいいじゃん。