使用Rails ActionMailer在电子邮件中渲染QR代码

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

我目前能够使用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 <%[email protected]%>!</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个回答
1
投票

我找到了另一个类似于这个的question,最终用google-qr gem利用google charts api

它真的很容易使用,就像一个魅力!

在我的模型中:

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

最好的部分是,如果您尝试在浏览器中直接访问此URL,则会出现400大错误,并且您无法看到QR代码。

© www.soinside.com 2019 - 2024. All rights reserved.