phpで画像をメール送信する

まず、gif画像を用意

添付ファイルのcontent typeは application/octet-stream で良いようです。

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

$to = "hoge@gmail.com";
$subject = "Just the Way You Are";

$filepath = "guitar.gif";
$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";
$message .= "When I see your face
ありのままの君が
愛おしいよ
Just the way you Are\n";
$message .="--" . $boundary . "\n";
$message .="Content-Type:". $mime_type . "; name=\"" .basename($filepath) . "\"\n";
$message .="Content-Disposition: attachment; filename=\"" .basename($filepath) ."\"\n";
$message .="Content-Transfer-Encoding: base64\n";
$message .= "\n";
$message .= chunk_split(base64_encode(file_get_contents($filepath)))."\n";
$message .="--" . $boundary . "--";
 
mb_send_mail($to, $subject, $message, $additional_headers);

おおおお!これやべー

感動した!