【问题标题】:Trouble Rendering QR code in Email using Rails ActionMailer使用 Rails ActionMailer 无法在电子邮件中呈现二维码
【发布时间】:2017-12-30 03:08:59
【问题描述】:

我目前能够使用 ActionMailer 生成 QR 码,但我无法在电子邮件中实际发送该 QR 码。元素正在正确发送,但图像已损坏,仅显示 alt。我可以在我的 ActionMailer 预览中看到 QR 码,但由于某种原因,在发送/接收电子邮件时它没有通过。

我正在使用 barby 在我的 CompanyLeadRsvpTicketModel 中创建 QR 码。

这是我模型中的代码:

def generate_qr(text)

  require 'barby'
  require 'barby/barcode'
  require 'barby/barcode/qr_code'
  require 'barby/outputter/png_outputter'

  barcode = Barby::QrCode.new(text, level: :q, size: 5)
  base64_output = Base64.encode64(barcode.to_png({ xdim: 5 }))
  return "data:image/png;base64,#{base64_output}"

end

这是我认为的代码:

<h1>Hi <%= @company_lead_rsvp_ticket.company_lead[0].first_name%>! Here is your RSVP ticket for the <%=@event.title%>!</h1><h2>Please bring this ticket to check in for the event.</h2><%=image_tag("#{@company_lead_rsvp_ticket.generate_qr(@company_lead_rsvp_ticket.otp_secret_key)}", :alt => "qrcode")%><p><%= @company_lead_rsvp_ticket.confirmation %></p>

【问题讨论】:

    标签: ruby-on-rails ruby qr-code actionmailer


    【解决方案1】:

    我找到了另一个与此类似的question,最终使用google charts api 使用google-qr gem

    它真的很容易使用并且像一个魅力一样工作!

    在我的模型中:

    def generate_qr(text)
    require 'google-qr'
    chart = GoogleQR.new(:data => "#{text}", :size => "500x500", :margin => 4, :error_correction => "L")
    return chart.to_s end
    

    在我看来:

    <%=image_tag("#{@company_lead_rsvp_ticket.generate_qr(@company_lead_rsvp_ticket.otp_secret_key)}", :alt => "qrcode")%> <br/>
    

    google-qr 接受文本输入,用于生成二维码并使用安全 URL 托管它。您所要做的就是获取google-qr 生成的数据并将其转换为字符串,以便将其用作图像源。

    最好的部分是,如果您尝试在浏览器中直接访问此 URL,您会收到一个巨大的 400 错误并且您看不到 QR 码。

    【讨论】:

      猜你喜欢
      • 2011-08-11
      • 1970-01-01
      • 2012-07-13
      • 2013-06-17
      • 2018-07-26
      • 2014-12-31
      • 2020-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多