1

我的 Symfony2 应用程序中有一条生成 png 图像的路线。该路线有效,并且有 Twig 模板在运行时使用和嵌入此 png 图像。

现在我正在尝试将相同的图像嵌入到我想用 Swiftmailer 发送的电子邮件中。但是当我这样做时,它会导致这个“内部服务器错误”:

ErrorException:警告:fopen(http://localhost/app/web/app_dev.php/autograph/15.png):打开流失败:HTTP请求失败!HTTP/1.0 500 内部服务器错误

如果我删除动态生成的图像,则邮件将正确发送。另一个带有固定 url 的图像也嵌入到电子邮件中,并且工作正常(这是公司徽标)。

这就是我包含 PNG 图像的方式(这只是我的控制器操作的一个片段):

  $auth_url = 
  $this
    ->container
    ->get('router')
    ->generate('autograph_route', array('id' => $id), true);
  $auth_img = \Swift_Image::fromPath($auth_url);
  $auth_cid = $message->embed($auth_img);

  $message
   ->setSubject($subject)
   ->setFrom($data['fromAddress'])
   ->setTo($data['toAddress'])
   ->setBody($this->renderView('mailBundle:Mail:mail.html.twig', 
     array
     (
      'data' => $data,
      'logo_cid' => $logo_cid,
      'autograph_cid' => $auth_cid
     )
    ))
    ->setContentType("text/html");

然后在我的 Twig 模板中包含这样的图像:

<img src="{{ logo_cid }}"/> <!-- works -->
<img src="{{ autograph_cid }}"/> <!-- causes error -->

在浏览器中键入生成的 url(“http://localhost/app/web/app_dev.php/autograph/15.png”),使动态生成的图像正确显示。标题设置为:“image/png”。

如何使图像也出现在我的邮件中?

添加:

当且仅当签名图像包含在邮件中时,错误日志会显示两个严重错误:

request.CRITICAL: UnexpectedValueException: The Response content must be a string or object implementing __toString(), "boolean" given. (uncaught exception) at C:\xampp\htdocs\myapp\app\cache\dev\classes.php line 3947 [] []

request.CRITICAL: Exception thrown when handling an exception (Twig_Error_Runtime: An exception has been thrown during the compilation of a template ("Node "1" does not exist for Node "Twig_Node".") in "TwigBundle::layout.html.twig".) [] []
4

0 回答 0