You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 11使用Gmail SMTP发送邮件时二维码不显示求助

问题描述

我使用Gmail SMTP结合Laravel 11实现邮件内动态生成二维码,最初二维码始终显示空白,尝试过相关解决方案后仍无法正常显示,目前二维码仅能以附件形式生成,恳请提供可正常扫描的二维码格式建议。


相关代码

ConfirmAttendance 类代码

class ConfirmAttendance extends Mailable
{
    use Queueable, SerializesModels;

    private $name;
    private $id;

    public function __construct($name, $id)
    {
        $this->name = $name;
        $this->id = $id;
    }

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Ticket Information!',
        );
    }

    public function content(): Content
    {
        $qrCodeAsPng = QrCode::format('png')->size(500)->generate($this->id);
        $qrCodeBase64 = base64_encode($qrCodeAsPng);

        return new Content(
            view: 'mail.confirm-attendance',
            with: [
                'name' => $this->name,
                'id' => $this->id,
                'qrCodeBase64' => $qrCodeBase64,
            ],
        );
    }

    public function attachments(): array
    {
        $qrCodeAsPng = QrCode::format('png')->size(500)->generate($this->id);
        $attachmentName = 'qr_code.png';

        return [
            \Illuminate\Mail\Mailables\Attachment::fromData(
                fn() => $qrCodeAsPng,
                $attachmentName,
                'image/png'
            ),
        ];
    }
}

Blade 模板代码

<p>Dear {{ $name }},</p>

<p>Congratulations! We're thrilled to have you join us for this exciting event!</p>

<h3>Your Ticket QR Code:</h3>

<div>
<img src="data:image/png;base64,{{ $qrCodeBase64 }}" alt="QR Code" />
</div>

<p>Ticket ID: {{ $id }}</p>

<br />

解决方案建议

1. 改用嵌入式附件替代Base64

多数邮件客户端(包括Gmail)对Base64内嵌图片支持有限,改用Laravel的嵌入式附件能更稳定显示:

修改 ConfirmAttendance 类

public function content(): Content
{
    // 生成二维码并转为嵌入式附件
    $qrCodeEmbed = \Illuminate\Mail\Mailables\Attachment::fromData(
        fn() => QrCode::format('png')->size(500)->generate($this->id),
        'qr_code.png',
        'image/png'
    )->asEmbedded();

    return new Content(
        view: 'mail.confirm-attendance',
        with: [
            'name' => $this->name,
            'id' => $this->id,
            'qrCodeEmbed' => $qrCodeEmbed,
        ],
    );
}

// 移除原attachments方法中的二维码附件,避免重复
public function attachments(): array
{
    return [];
}

修改 Blade 模板

<div>
    <img src="{{ $qrCodeEmbed->cid() }}" alt="Ticket QR Code" />
</div>

2. 验证二维码生成逻辑

  • 确认$this->id不为空或无效值,可临时添加dd($this->id)检查内容
  • 确保二维码依赖包(如simplesoftwareio/simple-qrcode)已正确安装,执行composer require simplesoftwareio/simple-qrcode确保依赖正常

3. 检查Gmail SMTP配置

确保.env中的配置正确:

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=你的Gmail邮箱
MAIL_PASSWORD=你的应用专用密码
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=你的Gmail邮箱
MAIL_FROM_NAME="${APP_NAME}"

注意:Gmail需开启两步验证后生成应用专用密码,不能使用普通登录密码。


内容的提问来源于stack exchange,提问作者shiro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 06:55:22