![](/img/trans.png)
[英]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.