質問

I am trying to implement actionmailer to send out task reminders via email when the show action in the tasks controller runs. When running on local host, my log indicates that the mail is sent, but it doesn't arrive at the destination.

My development.rb:

Emailtest::Application.configure do

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'my_app.com',
    user_name:            ENV['xxx@gmail.com'],
    password:             ENV['xxx'],
    authentication:       'plain',
    enable_starttls_auto: true  }
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # required for heroku
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }

end

My user_mail.rb

class UserMail < ActionMailer::Base
  default from: "bri.lobdell@gmail.com"

  def task_reminder(task)
    @task = task
    @url = 'http://example.com/login'

    mail(to: @task.recipientemail, subject: "You've been sent a reminded")
  end

end

The line from the show view that calls the mail to be delivered

<% UserMail.task_reminder(@task).deliver%>

UPDATE

My task index:

<tbody>
      <% @tasks.each do |task| %>
        <% if current_user.id == task.user_id%>
          <tr>
            <td><%= task.title %></td>
            <td><%= task.description %></td>
            <td><%= task.recipientname %></td>
            <td><%= task.recipientemail %></td>
            <td><%= link_to 'Show', task %></td>
            <td><%= link_to 'Edit', edit_task_path(task) %></td>
            <td><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %></td>
            <td> <%= render 'tasks/send_mail' %></td>

          </tr>
        <%end%>

      <% end %>
    </tbody>

My form: _send_mail.html.erb

<%= form_tag("/tasks/send_mail", method: "post", url: send_mail_path) do  %>
<%= hidden_field_tag :task_id, 'task.id' %>
<%= submit_tag "Remind" %>
<% end %>

Tasks#send_mail

def send_mail
    @task = Task.find(params[:id])
    UserMail.task_reminder(@task).deliver
    redirect_to :back
  end
役に立ちましたか?

解決

To call that line of code in your controller, you can make a form which will point to an action in a controller, and that action will have something like this:

def send_mail
@task = Task.find(params[:task_id])
UserMail.task_reminder(@task).deliver
redirect_to :back
end

You'd also need to enter in your routes file that controller, like so:

post "tasks/send_mail", :to => "tasks#send_mail", :as => "send_mail"

This would be your form (very simple), which would just have a button there. You could just trigger that form on page load:

<%= form_tag("/tasks/send_mail", method: "post", url: send_mail_path) do  %>
<%= hidden_field_tag :task_id, "#{@task.id}" %>
<%= submit_tag "Submit" %>
<% end %>

Hope this helps!

Try adding this to the top of your send_mail method:

if(params.has_key?(:task_id)
redirect_to "/"
end

If it redirects you, then the params are being passed, if not, we have a problem ;)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top