ErrorException:警告:标题可能不包含多个标题,检测到新行

I am having trouble redirecting after a certain function that sends emails! My function is:

public function emailAction($emails, $url)
{

    foreach($emails as $email)
    {
        $message = \Swift_Message::newInstance()
            ->setSubject('Updates in Symfony Blog')
            ->setFrom(array('blog@symfonyblog.com' => 'Symfony Blog'))
            ->setTo($email["email"])
            ->setBody(
            $this->renderView(
                'NEWSBlogBundle:Default:email.txt.twig',
                array('url' => $url)
            )
        )
        ;
        $this->get('mailer')->send($message);
    }

    return $this->redirect($this->newpostAction());
    //return $this->redirect($this->generateUrl('NEWSBlogBundle_homepage'));

}

It sends the emails OK, but then when redirecting, gives the error:

ErrorException: Warning: Header may not contain more than a single header, new line detected in C:\path\to\webroot\Symfony\app\cache\dev\classes.php line 3899

  1. in C:\path\to\webroot\Symfony\app\cache\dev\classes.php line 3899
  2. at ErrorHandler->handle('2', 'Header may not contain more than a single header, new line detected', 'C:\path\to\webroot\Symfony\app\cache\dev\classes.php', '3899', array('this' => object(RedirectResponse), 'name' => 'Location', 'values' => array('/app_dev.php/blog', object(ResponseHeaderBag), ' Redirecting to /app_dev.php/blog Redirecting to /app_dev.php/blog. ', '1.0', '302', 'Found', null), 'value' => object(ResponseHeaderBag)))
  3. at header('Location: Cache-Control: no-cache Date: Wed, 17 Jul 2013 11:56:17 GMT Location: /app_dev.php/blog ', false) in C:\path\to\webroot\Symfony\app\cache\dev\classes.php line 3899
  4. at Response->sendHeaders() in C:\path\to\webroot\Symfony\app\cache\dev\classes.php line 3914
  5. at Response->send() in C:\path\to\webroot\Symfony\web\app_dev.php line 27

while I haven't changed anything about any "headers" (and don't really know what it is about!). My app_dev.php is:

<?php

use Symfony\Component\HttpFoundation\Request;

if (isset($_SERVER['HTTP_CLIENT_IP'])
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
    || !in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1'))
) {
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';

$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

I tried two different ways of redirecting both (through routing and directly calling the controller) of which give the same error, whilst the pages load if I jusr type in their url!

I have NO idea what this error is and how I should fix it!

To be detail, your problem was the return value of $this->newpostAction(). Usally it's a Response object. But a redirect expects an uri/route, which can be sent to the client via the Location header. A redirect is always a client side turn around. But you can use ->forward('AcmeBundle:Demo:index') to forward internally to another controller action. In this case the URL on the client is not changed.

You trigger a sanity check of Symfony. When sending headers with header(), theoretically you could send plenty of headers with one function call. Symfony prevents you from accidentally doing it when passing something with a newline character as the target URL.