通过action mailer Rails 3.1自动发送邮件
我必须每周向所有用户发送有关最新发生的事情的电子邮件。我正在使用 ActionMailer 来完成其他邮件任务,但是我不知道如何自动化每周发送电子邮件。
更新
我发现 whenever gem 可用于安排 cron 作业。我想这可以用来发送我打算每周发送的电子邮件。仍在寻找如何使其与 ActionMailer 一起使用,一旦我找到解决方案
更新 2,
这就是我迄今为止使用 每当 gem:-
in Schedule.rb
every 1.minute do
runner "User.weekly_update", :environment => 'development'
end
in users_mailer.rb
def weekly_mail(email)
mail(:to => email, :subject => "Weekly email from footyaddicts")
end
in 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 文件,它有正确的内容。
但是我没有从应用程序自动收到任何邮件。知道可能出了什么问题吗?
谢谢,
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.
Update
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
Update 2
This is what I have done so far using whenever gem:-
in schedule.rb
every 1.minute do
runner "User.weekly_update", :environment => 'development'
end
in users_mailer.rb
def weekly_mail(email)
mail(:to => email, :subject => "Weekly email from footyaddicts")
end
in users.rb
def self.weekly_update
@user = User.all
@user.each do |u|
UsersMailer.weekly_mail(u.email).deliver
end
end
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?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,事实证明这是一个与 gem 相关的路径问题,当我安装另一个版本的 ruby 时,问题就出现了。
在我的机器中,新的 ruby 版本安装在 /usr/local/bin/ruby 中。在我的 Rails 应用程序中,我必须转到文件 script/rails 并将 #!/usr/bin/env ruby 替换为 #!/usr/local/bin/ruby。
我通过访问 cron.log 文件发现了这一点,该文件显示了此错误消息:- /usr/bin/env: ruby: No such file or directory
我创建了一个 cron.log 文件来记录 cron 错误,这就是我在我的文件中所做的问题中写入的 Schedule.rb 代码:-
我现在收到定期邮件。
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.
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.
I found this out by visiting cron.log file which showed this error message :- /usr/bin/env: ruby: No such file or directory
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 :-
I am getting periodic mails now.
您似乎尚未配置 ActionMailer 设置。
首先从控制台检查日志,邮件进程是否正常工作(粘贴您的日志)。
如果是,则执行以下步骤。
将其添加到您的 gemfile 中。
运行
在 config/environments/development.rb 文件中写入这些配置设置,
根据用户名和密码添加您的工作密码/电子邮件。
不要忘记重新启动服务器。
It seems like you haven't configured ActionMailer settings.
First check out the logs from console, whether the mailing process is working(paste your logs).
If yes then do following steps.
add this in your gemfile.
run
write these configuration setting in your config/environments/development.rb file
add your working password/email against user_name and password.
Don't forget to restart server.