When you change the header of the page, the whole page is rendered according to that header. In the case of using something like emails, you have a few options if you want to keep it single-paged.
Your first option is to use base64 encoding.
//create the image, $im
//omit the header settings
ob_start();
    imagejpeg($im);
    $data = ob_get_contents();
ob_end_clean();
echo '<img src="data:image/jpeg;base64,'.base64_encode($data).'">';
Here we place an output buffer to stop anything from going to the client, and store it in our variable $data. Then we just encode it using base64_encode and output to the page the way that the browser can interpret it.
There's also a get method that you can use.
if(isset($_GET['email'])) {
    header("Content-type: image/jpeg");
    //create the image, $im
    imagejpeg($im);
    imagedestroy($im);
    die;
}
echo '<img src="?email">';
Really, we're just sending an email input and processing with an if statement.