Send an email manually of a specific page in rails

2020-08-03 09:10发布

问题:

I have a basic application that gathers savings data for clients each quarter. There is a view that shows this data for each client compared to other client data with charts and graphs. I'm looking for an easy way to implement a button (or something) that allows me to manually send that view to the client in an html email at my discretion. I have only found tutorials showing how to send emails upon registration or confirmation type emails.

回答1:

# app/mailers/user_mailer.rb
class UserMailer < ActionMailer
  def report
    @things = Thing.report_stuff
    mail :to => 'boss@example.com', :from => 'you@example.com', 
      :subject => 'that report you want'
  end
end

# app/views/user_mailer/report.html.erb
<h1>Report</h1>
<% @things.each do |thing| %>
  <%= thing.name %>
<% end %> 

# app/controllers/reports_controller.rb
def create
  UserMailer.report.deliver
  flash[:notice] = 'report sent!'
  redirect_to root_path # or wherever
end

# in a view
<% form_tag(reports_path, :method => :post) do %>
  <% submit_tag 'send report email' %>
<% end %>