本文介绍了通过Action Mailer Rails 3.1自动发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须每周向所有用户发送有关最新情况的电子邮件。我正在使用ActionMailer来完成其他邮件发送任务,但是我不知道如何使每周的电子邮件自动化。

I have to send weekly emails to all the user about the latest things happening. I am using ActionMailer to accomplish other mailing task however I have no clue how to automate the weekly emails.

更新

我发现宝石可以用来安排cron作业。我想这可以用来发送我打算发送的每周电子邮件。仍在寻找如何使其与ActionMailer一起使用的方法,一旦找到解决方案便会更新

I found whenever gem which could be used to schedule cron jobs. I guess this could be used to send weekly emails which I intend to. Still looking how to make it work with ActionMailer will update once I find the solution

更新2

到目前为止,这是我使用 gem:-

This is what I have done so far using whenever gem:-

in schedule.rb

in schedule.rb

every 1.minute do
    runner "User.weekly_update", :environment => 'development'
end

in users_mailer.rb

in users_mailer.rb

def weekly_mail(email)
    mail(:to => email, :subject => "Weekly email from footyaddicts")
end

users.rb

def self.weekly_update
    @user = User.all
    @user.each do |u|
      UsersMailer.weekly_mail(u.email).deliver
    end
end

如果我尝试从控制台运行User.weekly_update,我将能够收到邮件。我正在开发模式下使用rvm进行测试。我检查了crontab文件,并找到了正确的文件。

If i try to run User.weekly_update from the console I am able to get the mails. I am testing in development mode and using rvm. I checked my crontab file and it has got the right stuff.

但是我没有从应用程序中自动收到任何邮件。有什么线索可能是什么问题吗?

However I am not getting any mails automatically from the app. Any clue what might be wrong?

谢谢

推荐答案

好因此,无论什么时候使用gem都是一个路径问题,而当我安装另一个版本的ruby时就产生了这个问题。

OK so it turns out to be a path issue with whenever gem, and the problem was created when I installed another version of ruby.

在我的机器中,新的ruby版本安装在/ usr / local / bin / ruby​​中。在我的Rails应用程序中,我必须转到文件脚本/ rails,并用#!/ usr / local / bin / ruby​​替换#!/ usr / bin / env ruby​​。

In my machine the new ruby version is installed in /usr/local/bin/ruby. In my rails app I had to go to the file script/rails and replace #!/usr/bin/env ruby with #!/usr/local/bin/ruby.

我通过访问显示此错误消息的cron.log文件发现了这一点:-/ usr / bin / env:ruby:没有这样的文件或目录

I found this out by visiting cron.log file which showed this error message :- /usr/bin/env: ruby: No such file or directory

一个cron.log文件记录cron错误,这就是我在问题中编写的schedule.rb代码中所做的事情:-

I made a cron.log file to log the cron error this is what I did in my schedule.rb code written in the question :-

every 2.minutes do
    runner "User.weekly_update", :environment => 'development', :output => 'log/cron.log'
end

我现在收到定期邮件。

这篇关于通过Action Mailer Rails 3.1自动发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:07