Symfony - 无法在电子邮件中添加图像

Symfony - Unable to add image in an email

我正在做 cron 作业并尝试使用 SwiftMailer 发送电子邮件

在 cron Action 内部,我正在使用 foreach 循环遍历循环并向用户发送电子邮件。

foreach($hostDinners as $key => $hostDinner) {
    //generate email
$message = \Swift_Message::newInstance()
    ->setSubject("This is a demo subject")
    ->setFrom($this->container->getParameter('mailer_user'))
    ->setTo($key)

    ->setBody(
        $this->renderView(
            'AppBundle:emails:cron-job-request-and-invitations.html.twig',
            array(
            'dinners' => $hostDinner
            )
        )
    )
    ->setContentType('text/html');
$this->get('mailer')->send($message);


}

然后我也将数组传递给电子邮件模板,在电子邮件中我再次使用循环来显示电子邮件内容。一切正常,只是我无法显示图像。

{% for dinner in dinners %}
<img style="display: block;" src="{{ absolute_url(asset('uploads/' ~ dinner.guest_profile_image)) }}" alt="image1" class="section-img" height="auto" width="128">
{% endfor %}

更新

当我转储图像变量时 dinner.guest_profile_image 这就是我得到的 http://127.0.0.1:8000/uploads/42c5681b253c4dc6a1d145f744e6c3cd.jpeg 如果我直接在浏览器上访问它我可以看到我需要电子邮件显示的图像。

$message 转储非常大,因为里面有很多 css 和 html 所以我这里是转储的屏幕截图,其中图像标签是

我在这里错过了什么?我需要做什么才能显示图像。

试试 asset_url

<img style="display: block;" src="{{ asset_url('uploads/' ~ dinner.guest_profile_image) }}" alt="...">

或者您可以尝试像这样在 base64 中嵌入图像:

<img style="display: block;" src="data:image/png;base64,iVBORw0KG....(this represent the base64 of your image)..." alt="..."/>

更新 1


foreach($hostDinners as $key => $hostDinner) {

    //Encode img in base64
    foreach ($hostDinner as &$item) {
        $path = $item['guest_profile_image']; //use symfony2 functions to get the abs upload path
        $type = pathinfo($path, PATHINFO_EXTENSION);
        $data = file_get_contents($path);
        $item["guest_profile_image"] = 'data:image/' . $type . ';base64,' . base64_encode($data);
    }
    unset($item); // break the reference with the last element

    //generate email
    $message = \Swift_Message::newInstance()
        ->setSubject("This is a demo subject")
        ->setFrom($this->container->getParameter('mailer_user'))
        ->setTo($key)

        ->setBody(
            $this->renderView(
                'AppBundle:emails:cron-job-request-and-invitations.html.twig',
                array(
                'dinners' => $hostDinner
                )
            )
        )
        ->setContentType('text/html');
    $this->get('mailer')->send($message);
}

并且在视图中:

{% for dinner in dinners %}
      <img style="display: block;" src="{{ dinner.guest_profile_image }}" alt="image..." class="section-img" height="auto" width="128"/>
{% endfor %}

更新 2


在这一行 $item["guest_profile_image"] = 'data:image/' . $type . ';base64,' . base64_encode($data); 中,您拥有需要放入 img 标签的属性 src 中的所有内容(您可以在视图中看到我只打印打印内容数组 src="{{ dinner.guest_profile_image }}")。 alt 属性用于 html 的良好实践,但没有必要 alt attribute

&的官方PHP文档:为了能够在$value&之前的循环中直接修改数组元素.在这种情况下,该值将通过引用分配。

因此,在循环中,我将图像路径从您的数组替换为每个 base64 编码。通过这种方式,您不必修改代码中的任何内容,您的数组的大小与以前相同,但数组中没有图像路径......您可以在数组的那些位置以 base64 解码图像. 有关 & 的更多信息,请参见 Oficial php documentation

看看(img标签上的base64)[https://en.wikipedia.org/wiki/Data_URI_scheme]

希望这个解释能帮到你。