Dan Benjamin
Dan Benjamin

Reputation: 900

Delayed Jobs and Action Mailer

I am having trouble with implementing delayed jobs with my ActionMailer: Before Delayed Job Implementation:

class NotificationsMailer < ActionMailer::Base

  default :from => "[email protected]"
  default :to => "[email protected]"

  def new_message(message)
    @message = message
    mail(:subject => "[Company Notification] #{message.subject}")
  end

end

and called it using this line (it worked perfectly fine):

NotificationsMailer.new_message(@message).deliver

After the Delayed Job implementation all i did was change the deliver line to:

NotificationsMailer.delay.new_message(@message)

In addition, I started the jobs queue using

rake jobs:work

I can see the objects in the database if the job is closed and i can see they get popped after i start the worker but nothing happens (no email sent).

Update - Other Delayed Tasks (not related to mail) are working fine.

Can anyone help a newbie?

Thanks in advance!!

Upvotes: 4

Views: 9321

Answers (2)

Shun Takeda
Shun Takeda

Reputation: 216

The first place I would look is the smtp settings in your environments, check to make sure that is correct:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:authentication => 'plain',
:enable_starttls_auto => true,
:user_name => "[email protected]",
:password => "yourpassword"
}
config.action_mailer.default_charset = "utf-8"
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

I use an old ruby, 1.8.7 and rails 2.3.8, so check to make sure the syntax is correct as well.

Upvotes: 1

Dhiraj
Dhiraj

Reputation: 1119

I spend much time for sending email using delayed jobs, finally it works.

In Gem File

gem 'delayed_job'
gem 'delayed_job_active_record'

In Controller

 def dtest

    UserMailer.delay.welcome_email
    render json:"Succes"
    return
 end

In mailer

class UserMailer < ActionMailer::Base
  default from: "[email protected]"

  def welcome_email
    ActionMailer::Base.delivery_method = :smtp 
        ActionMailer::Base.smtp_settings = {
                address: "smtp.gmail.com", 
                port: 587, 
                domain: 'gmail.com',
                Authentication: "plain", 
                enable_starttls_auto: true, 
                user_name: '[email protected]',
                password: 'yourpassword'
            }

    mail(to: '[email protected]', subject: 'Background Email Test').deliver

  end

end

After this I just start rails server and start job work

rake jobs:work

I hope, It will help you guys and Email sending will work fine using 'delayed-jobs' :)

Upvotes: 5

Related Questions