[英]Is it possible to abort sending an email once an ActionMailer method has been called?
[英]ActionMailer not sending email though rails logs indicate it has been sent
我正在嘗試實現actionmailer,以便在任務控制器中的show動作運行時通過電子郵件發送任務提醒。 在本地主機上運行時,我的日志指示郵件已發送,但未到達目的地。
我的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
我的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
顯示視圖中的該行調用要傳遞的郵件
<% UserMail.task_reminder(@task).deliver%>
更新
我的任務索引:
<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>
我的表格:_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 %>
任務#send_mail
def send_mail
@task = Task.find(params[:id])
UserMail.task_reminder(@task).deliver
redirect_to :back
end
要在控制器中調用該代碼行,可以創建一個指向控制器中某個動作的表單,該動作將具有以下內容:
def send_mail
@task = Task.find(params[:task_id])
UserMail.task_reminder(@task).deliver
redirect_to :back
end
您還需要在路由文件中輸入該控制器,如下所示:
post "tasks/send_mail", :to => "tasks#send_mail", :as => "send_mail"
這就是您的表格(非常簡單),那里只有一個按鈕。 您可以在頁面加載時觸發該表單:
<%= form_tag("/tasks/send_mail", method: "post", url: send_mail_path) do %>
<%= hidden_field_tag :task_id, "#{@task.id}" %>
<%= submit_tag "Submit" %>
<% end %>
希望這可以幫助!
嘗試將其添加到send_mail
方法的頂部:
if(params.has_key?(:task_id)
redirect_to "/"
end
如果它重定向您,則參數將被傳遞,否則,我們將遇到問題;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.