Generate and mail qrcode without saving to local storage

Dear

Is it possible to generate a qr code(png) and mail it without saving the png to disk?

Can anybody help please. I can’t find a answer anywhere. Or I’m thinking wrong about it ?

I will let you figure out libs to do this, but your steps are:

  • generate qr code with lib to binary
  • base64 the binary
  • create html mail
  • insert img tag using the base64 string as data
  • victory

Good luck!

1 Like

Ty

You do this without an attachment?

      |> attachment(
        Swoosh.Attachment.new(
          {:data, invitation_qr_code_binary},
          filename: "qrcode.png",
          content_type: "image/png",
          type: :inline)
      )

Tried with and without attachment. I guess attachment is needed for html too?
But I see a strange preview in Swoosh’s mailbox.

preview:

  defp deliverwithattachment(recipient, subject, txtbody, htmlbody, qr_code_binary) do
    email =
      new()
      |> to(recipient)
      |> from({"something", "something@example.co"})
      |> subject(subject)
      |> text_body(txtbody)
      |> html_body(htmlbody)
      |> attachment(
        Swoosh.Attachment.new(
          {:data, qr_code_binary},
          filename: "qr.png",
          content_type: "image/png",
          type: :inline)
      )

    with {:ok, _metadata} <- Mailer.deliver(email) do
      {:ok, email}
    end
  end

def deliver_qr_email(email, url) do
    # :png64 or :jpg64
    {:ok, %Qrusty.QR{encoded_data: binary_64}} = Qrusty.qr(url, :png64, width: 200, height: 200)

    deliverwithattachment(email, "subj", """
       text only message, look in attachments...
    """,~s|
    <h2>Hallo #{email},</h2>
    <br/>
    <br/>

    <a href={"data:image/png;base64, " <> binary_64} download="qr.png">
      <img src={"data:image/png;base64, "<> binary_64}></img>
    </a>

    <img src="cid:qr.png">

    <br/>
    <br/>
    |,binary_64)
  end

Does anyone see anything wrong with this?

I don’t know if this is what you need:GitHub - bajankristof/qrcode: An Erlang library to generate QR codes.

update:
There was some bug somewhere. I could see the mails in the local mailbox but in production they wouldn’t send.

I’ve removed html_body and attachment and now :prod is sending again. I’ll try to add bit by bit again.
update2:
attachment was/is buggy for some reason

No need for attachments in the html version. I should look for a way to attach it though, but not right now

defp deliver(recipient, subject, txtbody, htmlbody) do
    email =
      new()
      |> to(recipient)
      # TODO
      |> from({"example", "example@example.com"})
      |> subject(subject)
      |> html_body(htmlbody)
      |> text_body(txtbody)

    with {:ok, _metadata} <- Mailer.deliver(email) do
      {:ok, email}
    end
  end
def deliver_type_something(email, url) do
    {:ok, %Qrusty.QR{encoded_data: binary_64}} = Qrusty.qr(url, :png64, width: 200, height: 200, ec: :m)

    deliver(
      email,
      "some subject",
      """

      ==============================

      Hallo #{email},

      #{url}

      ==============================
      """,~s|
        <h2>Hallo #{email},</h2>

        <img src="data:image/png;base64,#{binary_64}">
        
        #{url}
      |)
end
1 Like

Exactly what I meant, good work!

yes, but it isn’t good enough. People with @gmail can’t see it in the gmail app.