rubyonjets/jets

Unable to send emails containing link_to from the console or jobs

Closed this issue · 3 comments

Checklist

  • Upgrade Jets: Are you using the latest version of Jets? This allows Jets to fix issues fast. There's a jets upgrade command that makes this a simple task. There's also an Upgrading Guide: http://rubyonjets.com/docs/upgrading/
  • Reproducibility: Are you reporting a bug others will be able to reproduce and not asking a question. If you're unsure or want to ask a question, do so on https://community.rubyonjets.com
  • Code sample: Have you put together a code sample to reproduce the issue and make it available? Code samples help speed up fixes dramatically. If it's an easily reproducible issue, then code samples are not needed. If you're unsure, please include a code sample.

My Environment

Software Version
Operating System macOS Catalina 10.15.7
Jets 2.3.18
Ruby 2.5.8

Expected Behaviour

Attempting to send an email containing a link_to element inside the htlm.erb file successfully sends via console or job.

Current Behavior

ActionView::Template::Error (undefined method 'host' for nil:NilClass) is raised.
Error is originating from lib/jets/overrides/rails/common_methods.rb:5:in 'add_stage_name'.
https://github.com/boltops-tools/jets/blob/master/lib/jets/overrides/rails/common_methods.rb#L5

Step-by-step reproduction instructions

  1. Run the following commands
jets new mail_test --database=postgresql --mode job
jets generate mailer TestMailer test_mail
  1. Uncomment the following line in development.rb
config.action_mailer.raise_delivery_errors = false
  1. Ensure the generated mail can be delivered
jets c
TestMailer::test_mail.deliver_now
Output
irb(main):001:0> TestMailer::test_mail.deliver_now
D, [2021-01-18T16:11:33.073542 #76937] DEBUG -- : TestMailer#test_mail: processed outbound mail in 160.8ms
I, [2021-01-18T16:11:33.081777 #76937]  INFO -- : Delivered mail 600534a512d80_12c893fe43f838e6c8340@MacBook-Pro.local.mail (8.1ms)
D, [2021-01-18T16:11:33.081837 #76937] DEBUG -- : Date: Mon, 18 Jan 2021 16:11:33 +0900
From: from@example.com
To: to@example.org
Message-ID: <600534a512d80_12c893fe43f838e6c8340@MacBook-Pro.local.mail>
Subject: Test mail
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_600534a511c02_12c893fe43f838e6c833a2";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_600534a511c02_12c893fe43f838e6c833a2
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Test#test_mail

Hi, find me in app/views/test_mailer/test_mail.text.erb


----==_mimepart_600534a511c02_12c893fe43f838e6c833a2
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
      /* Email styles need to be inline */
    </style>
  </head>

  <body>
    <h1>Test#test_mail</h1>

<p>
  Hi, find me in app/views/test_mailer/test_mail.html.erb
</p>

  </body>
</html>

----==_mimepart_600534a511c02_12c893fe43f838e6c833a2--

=> #<Mail::Message:70249607803460, Multipart: true, Headers: <Date: Mon, 18 Jan 2021 16:11:33 +0900>, <From: from@example.com>, <To: to@example.org>, <Message-ID: <600534a512d80_12c893fe43f838e6c8340@MacBook-Pro.local.mail>>, <Subject: Test mail>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_600534a511c02_12c893fe43f838e6c833a2"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>
  1. Inside test_mail.html.erb add a link_to element
<%= link_to(
  "I am a link",
  "https://www.google.com/",
  style: "text-decoration: none;", target: :blank
) %>
  1. Enter the jets console and attempt to send again
jets c
TestMailer::test_mail.deliver_now
Error Log
irb(main):001:0> TestMailer::test_mail.deliver_now
D, [2021-01-18T16:17:46.055432 #77300] DEBUG -- : TestMailer#test_mail: processed outbound mail in 71.6ms
Traceback (most recent call last):
       16: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/actionview-6.1.1/lib/action_view/renderer/template_renderer.rb:71:in `block in render_with_layout'
       15: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/actionview-6.1.1/lib/action_view/renderer/template_renderer.rb:56:in `block in render_template'
       14: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/activesupport-6.1.1/lib/active_support/notifications.rb:203:in `instrument'
       13: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/activesupport-6.1.1/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
       12: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/activesupport-6.1.1/lib/active_support/notifications.rb:203:in `block in instrument'
       11: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/actionview-6.1.1/lib/action_view/renderer/template_renderer.rb:61:in `block (2 levels) in render_template'
       10: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/actionview-6.1.1/lib/action_view/template.rb:152:in `render'
        9: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/actionview-6.1.1/lib/action_view/template.rb:345:in `instrument_render_template'
        8: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/activesupport-6.1.1/lib/active_support/notifications.rb:205:in `instrument'
        7: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/actionview-6.1.1/lib/action_view/template.rb:154:in `block in render'
        6: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/actionview-6.1.1/lib/action_view/base.rb:247:in `_run'
        5: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/actionview-6.1.1/lib/action_view/base.rb:247:in `public_send'
        4: from /.../Projects/test/app/views/test_mailer/test_mail.html.erb:7:in `___sers_tyler__rojects_test_app_views_test_mailer_test_mail_html_erb___2030083256069031912_70185381396740'
        3: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/actionview-6.1.1/lib/action_view/helpers/url_helper.rb:202:in `link_to'
        2: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/jets-3.0.2/lib/jets/overrides/rails/url_helper.rb:21:in `url_for'
        1: from /.../.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/jets-3.0.2/lib/jets/overrides/rails/common_methods.rb:5:in `add_stage_name'
ActionView::Template::Error (undefined method `host' for nil:NilClass)

Note:

The same error occurs if the mailer is invoked from a job once deployed to AWS Lambda.

Code Sample

See the reproduction instructions. Only one comment is uncommented and a single block of code added to a generated html.erb

The relevant code that is erroring:
https://github.com/boltops-tools/jets/blob/master/lib/jets/overrides/rails/common_methods.rb#L5

Solution Suggestion

I am not so familiar with Jets, but am assuming this is happening due to the fact that the mail is being sent not via a request from a controller, but via the console or a job.

Commenting out the following line enables me to send emails from the console/job.

Jets::Controller::Stage.add(request.host, url)

@tongueroo
For the time being, I am needing to fork the repository and introduce the following changes.

jets/overrides/rails/common_methods.rb

def add_stage_name(url)
  request ? Jets::Controller::Stage.add(request.host, url) : url
end

I have no idea if this is the "correct" way to go about fixing the issue, but it is working as intended for me.

This is no longer an issue I have reset password links in my app i throughly tested @tongueroo , this can be closed

Hovering over my confirm link you can see the url:
Screenshot 2023-10-31 at 5 12 56 PM

This is possible in Jets 5

https://blog.boltops.com/2023/12/05/jets-5-improvements-galore/

It was tested as part of testing Kingsman

https://docs.rubyonjets.com/docs/auth/kingsman/

URL helpers are available