MyGit

v1.2

the-teacher/rails7-startkit

版本发布时间: 2023-01-29 02:07:39

the-teacher/rails7-startkit最新发布版本:v2.1(2023-09-18 15:15:47)

Rails 7. Startkit

Rails7 StartKit-1 2

Rails 7 Start Kit. Dockerized Rails App with the most popular preinstalled tools.

Great if you want to:

👉 Subscribe to the project to know about most recent updates.

RELEASE 1.2 DETAILS

Screenshot 2023-01-28 at 14 43 32 Screenshot 2023-01-28 at 14 41 58

Rails 7. ActionMailer::Preview and Mailcatcher

This week I've added to Rails 7. Start Kit functionality related to sending and previewing emails. Release 1.2 is here. Details you can find in the PR.

How I did it

Step 1

I wanted to install gem mailcatcher directly to the Rails container in my project. After some attempts I found that mailcatcher uses some old gems that are not compatible with ruby 3.2.

Obviously I just found a docker container and added it to my project.

  # docker/docker-compose.yml
  #
  mailcatcher:
    image: schickling/mailcatcher
    ports:
      - 1080:1080

Step 2

Now I should add launching of this new container in my bin/setup

  # Mailcatcher
  step_info "Launching Mailcatcher Container"
  system('docker compose -f docker/docker-compose.yml up mailcatcher -d')
  wait('to launch Mailcatcher Container')

Step 3

Now, when we have mailcatcher we should set up an address of the SMTP service. 

For development we should modify: config/environments/development.rb

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: "mailcatcher",
    port: 1025
  }

address: "mailcatcher" I use because it is a name of the container in docker/docker-compose.yml

Step 4

It is time to create Rails Mailer and implement our new email letter:

$ rails generate mailer demo

Step 5

In app/mailers/demo_mailer.rb we have to implement our email and default params.

class DemoMailer < ApplicationMailer
  default from: 'demo@rails7startkit.com'

  # DemoMailer.welcome_email.deliver!
  def welcome_email
    mail(
      to: "test@test.com",
      subject: 'Welcome to Rails 7. StartKit'
    )
  end
end

And letter's content: app/views/demo_mailer/welcome_email.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to Rails7. StartKit</h1>
    <p>Thanks for using this project!</p>
  </body>
</html>

Step 6

Time to check our mailer

We go to rails console

$ rails c

In rails console type

DemoMailer.welcome_email.deliver!

We see that email was sent

Step 7

Checking results in mailcatcher. Address: http://localhost:1080

Mailcatcher

Step 8

Also we can implement default ActionMailer::Preview

Just modify: test/mailers/previews/demo_mailer_preview.rb

# Preview all emails at http://localhost:3000/rails/mailers/demo_mailer
class DemoMailerPreview < ActionMailer::Preview
  # Accessible from  http://localhost:3000/rails/mailers/demo_mailer/welcome_email
  def welcome_email
    DemoMailer.welcome_email
  end
end

And now we can see the result

ActionMailer::Preview

Thank you for your interest to Rails 7. Start Kit Star/Subscribe to the project to know about most recent updates.

Happy coding!

相关地址:原始地址 下载(tar) 下载(zip)

查看:2023-01-29发行的版本