Delayed Jobs and Action Mailer

江枫思渺然 提交于 2019-12-01 19:03:45
Shun Takeda

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 => "youremail@gmail.com",
: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.

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: "from@example.com"

  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: 'your@gmail.com',
                password: 'yourpassword'
            }

    mail(to: 'no-reply@gmail.com', 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' :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!