在电子邮件中显示运行时二维码图像,而不存储在 yii2 PHP 中

问题描述 投票:0回答:1

我使用了一个扩展在 yii2 PHP 中生成二维码 (https://github.com/tecnickcom/tc-lib-barcode)

通过文档,我遵循了这一点。

// instantiate the barcode class
$barcode = new \Com\Tecnick\Barcode\Barcode();

// generate a barcode
$bobj = $barcode->getBarcodeObj(
    'QRCODE,H',                     // barcode type and additional comma-separated parameters
    'https://tecnick.com',          // data string to encode
    -4,                             // bar width (use absolute or negative value as multiplication factor)
    -4,                             // bar height (use absolute or negative value as multiplication factor)
    'black',                        // foreground color
    array(-2, -2, -2, -2)           // padding (use absolute or negative values as multiplication factors)
    )->setBackgroundColor('white'); // background color

// output the barcode as HTML div (see other output formats in the documentation and examples)
echo $bobj->getHtmlDiv();

我有一个生成二维码的要求,并且我们必须通过电子邮件发送该二维码。
电子邮件代码已准备就绪,因为我们已映射 图像标签来显示图像。如果我们传递任何存储的图像路径,则图像将显示并且电子邮件代码工作正常。
但我在电子邮件中显示二维码时遇到了挑战。我已经通过 Postman 执行了上述代码,并且显示了二维码,但电子邮件上没有显示。

 $bobj->getHtmlDiv();

除了这个功能我也尝试过。

 $bobj->getPng();
 $bobj->getSvg();

但不工作

谁能告诉我们如何渲染它?
或者有其他易于使用和实现的扩展吗?

尝试过功能

  $result = $bobj->getHtmlDiv();
  $result = $bobj->getPng();
 $result = $bobj->getSvg();

但无法通过电子邮件发送。

还尝试在文件对象中传递以下代码的结果。静态图像仅出现在邮递员中,而不是文件对象中。

file_get_contents()

也尝试过,但无法解决。

php email yii2 qr-code
1个回答
0
投票

首先,如果您想嵌入或附加条形码图像,您必须使用

getPngData()
方法而不是
getPng()
。这是因为
getPng()
不只是简单地返回图像数据,而是设置标题并直接输出结果。

使用

getPngData()
获取图像数据后,您可以使用
yii\mail\MessageInterface::embedContent()
将图像嵌入到邮件中,而无需将其保存在任何地方。

假设您已经在

$bobj
属性中拥有条形码对象,那么在邮件模板中您可以执行以下操作:

<img src="<?= $message->embedContent(
    $bobj->getPngData(),
    [
        'fileName' => 'barcode.png',
        'contentType' => 'image/png',
    ]
); ?>" alt="barcode">
© www.soinside.com 2019 - 2024. All rights reserved.