How to Include Actionmailer Class in Rake Task

How to include ActionMailer class in rake task?

I have the following and it is working:

# in lib/tasks/mailme.rake
task :mailme => :environment do
UserMailer.test_email(1).deliver
end

And

# in app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
def test_email(user_id)
@user = User.find_by_id user_id

if (@user)
to = @user.email

mail(:to => to, :subject => "test email", :from => "default_sender@foo.bar") do |format|
format.text(:content_type => "text/plain", :charset => "UTF-8", :content_transfer_encoding => "7bit")
end
end
end
end

ActionMailer loop on rake task send an extra mail every time it runs

Your UserMailer class should not call deliver by itself.

Instead your rake task should call deliver.

So remove deliver from the UserMailer class and place the logic into your rake task:

task daily: [:environment] do  
apps.each do |rec||
UserMailer.apps_remainder_email(rec).deliver
end
end

ActionMailer in Sneakers Rake Task

For now I have switched from using a Sneakers worker that is constantly taking messages from the queue, to a scheduled rake task that takes all available messages in the queue. I don't know why the Action Mailer performs so differently in the Sneakers worker versus the Rake Task, but this is a decent workaround and there is plenty of documentation on creating a mailer rake task using Rufus. If anyone has any insight please post it as I'm still curious how to get the Sneakers worker to use the Action Mailer.

Heroku rake task not loading ActionMailer class

Heroku finally figured out what was going on. I originally created my Notifier class file named as "Notifier" (capitalized first letter) instead of "notifier". I realized the mistake and manually changed the filename, however I had already did a git commit. Well since Max OS X is case insensitive for filenames, changing the capitalization to lowercase didn't commit to git. So everything worked locally on my Macbook, and showed that the filename was correct, but deploying to Heroku pushed a improperly named file. Even worse is that it only was affected under the rake tasks.

I don't think I would have ever figured this out without Heroku.

ActionMailer w/ Custom Rake Task

Your mailer file name does not match with the mailer class name as per Rails convention.

Mailer class should be placed in this path:

app/mailers/license_expire_mailer.rb

Rake Task w/ ActionMailer - Undefined Method Error

I figured it out. I was trying to use agent_card and didn't need it. Once I removed it, it works just fine!

Run mailer only after rake task is finished

Unless you execute rake from inside the same Ruby machine as your mailer (which I doubt,) they do not share the environment, even if you were trying to use the global variable (setting local variables has zero effect at all in any case, even inside your last two methods in rake those are different variables.)

What you need is to write .pid file and check it’s existence on the file system, or use any other external source of truth, like redis, or even DB.

Ruby on Rails - Get user email from database in a Rake Task

Find the user(s) that need the email and iterate over them. Surely you're indicating which users need the email in the database somehow, so you'll just want to query all those user records, iterate over them, and then send the email.

task :send_warn_course, [:user_email] => :environment do |t, args|
MailCourseWarn.where(needs_warned: true).each do |user|
MailCourseWarnMailer.course_available(user).deliver!
end
end


Related Topics



Leave a reply



Submit