使用Ruby on Rails发送基于间隔的电子邮件 [英] Sending emails based on intervals using Ruby on Rails

查看:109
本文介绍了使用Ruby on Rails发送基于间隔的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够以确定的时间间隔向不同的收件人发送一串电子邮件。



我分配给每个联系人这一系列电子邮件称为广告系列,其中Campaign有Email1,Email2等。每个联系人都有一个Contact.start_date。每个电子邮件都有email.days,其中存储自联系人的起始日期以发送电子邮件的天数。



例如:Email1.days = 5,Email2.days = 7,Email3.days = 11。



Contact1.start_date = 4/10/2010; contact2.start_date = 4/08/2010



如果今天是4/15,那么Contact1会收到电子邮件1(4 / 15-4 / 10 = 5天)
如果今天是4/15,那么Contact2收到电子邮件2(4/15 - 4/8 = 7天)。



每天运行什么好的行动使用cron工作,然后遵循这些规则使用ActionMailer发送电子邮件?



注意:问题不在于使用ActionMailer。它是关于做数学以及执行。哪些电子邮件发送给谁?我猜测它与某些版本的Date - Contact [x] .start_date有关,然后与电子邮件[x] .days进行比较,但我不完全清楚如何。谢谢。



我想指导是否使用date.today与time.now。



注意:意图是个人可能需要在一致的基础上安排个人随访。而不必记得什么时候跟踪哪些电子邮件与谁,只是按照预先确定的广告系列并发送给那个人。



所以这不是一个批量邮件 - 这真的是使个人信件的后续自动化。

解决方案

我会使用 DelayedJob (假设您每天不发送大量的电子邮件电子邮件,即每天数千个等等)。

  class Email< ActiveRecord :: Base 
belongs_to:campaign
after_create:schedule_email_dispatch

def schedule_email_dispatch
send_at(campaign.created_at + self.days.days,:send_email)
end

def send_email
end
end

使用耙子任务运行工人:

 耙子作业:工作

每次创建一个新的电子邮件对象时,将延迟的作业项目添加到队列中。在正确的间隔内,电子邮件将由工作人员发送。

  @campaign = Compaign.new(...)
@ campaign.emails.build(:days => 1)
@ campaign.emails.build(:days => 2)
@ campaign.save#now the delay

在上面的示例中,保存广告系列后,将创建两个延迟作业条目。它们在广告系列创建日期后1和2天执行。



此解决方案确保电子邮件大约围绕预期的安排时间发送。在一个基于Cron工作的解决方案中,在cron间隔发生disapaching。如果要使用cron方法,请执行以下操作:

  class Email< ActiveRecord :: Base 
def self.dispatch_emails
#找到要发送的电子邮件
Email.all(:conditions => [created_at< = DATE_SUB(?INTERVAL days DAY) ,
Time.now])
email.send_email
end
end
end

在这个解决方案中,大部分的处理是由DB完成的。



lib / tasks中添加 email.rake 文件目录:

  task:dispatch_emails => :environment do 
Email.dispatch_emails
end

配置cron执行 rake dispatch_emails 定期(在您的情况下(<24小时)


I would like to be able to send a string of emails at a determined interval to different recipients.

I assign to each Contact this series of Emails called a Campaign, where Campaign has Email1, Email2, etc. Each Contact has a Contact.start_date. Each Email has email.days which stores the number of days since a Contact's start-date to send the email.

For example: Email1.days=5, Email2.days=7, Email3.days=11.

Contact1.start_date = 4/10/2010; contact2.start_date = 4/08/2010

IF today is 4/15, then Contact1 receives Email 1 (4/15-4/10 = 5 days) IF today is 4/15, then Contact2 received Email 2 (4/15 - 4/8 = 7 days).

What's a good action to run every day using a cron job that would then follow these rules to send out emails using ActionMailer?

NOTE: The question isn't about using ActionMailer. It is about doing the "math" as well as the execution. Which email to send to whom? I am guessing it has to do with some version of Date - Contact[x].start_date and then compare against email[x].days but I'm not exactly clear how. Thanks.

I'd like guidance on whether to use date.today versus time.now as well.

Note: the intent is that an individual person may need to schedule individual follow-up on a consistent basis. Rather than having to remember when to follow up which email with whom, it would just follow a pre-determined campaign and send for that person.

So it's not a "bulk mail" -- it's really automating the follow-up for individual correspondence.

解决方案

I would use DelayedJob for this ( assuming you are not sending large number of emails emails a day, i.e. 100's of thousands per day etc.)

class Email < ActiveRecord::Base
  belongs_to :campaign
  after_create :schedule_email_dispatch

  def schedule_email_dispatch
    send_at(campaign.created_at + self.days.days, :send_email)
  end

  def send_email
  end
end

Run the workers using the rake task:

rake jobs:work

Every time a new Email object is created a delayed job item is added to the queue. At the correct interval the email will be sent by the worker.

@campaign = Compaign.new(...)
@campaign.emails.build(:days => 1)
@campaign.emails.build(:days => 2)
@campaign.save # now the delay

In the example above, two delayed job entries will be created after saving the campaign. They are executed 1 and 2 days after the creation date of the campaign.

This solution ensures emails are sent approximately around the expected schedule times. In a cron job based solution, disptaching happens at the cron intervals. There can be several hours delay between the intended dispatch time and the actual dispatch time.

If you want to use the cron approach do the following:

class Email < ActiveRecord::Base
  def self.dispatch_emails
    # find the emails due for dispatch
    Email.all(:conditions => ["created_at <= DATE_SUB(?, INTERVAL days DAY)", 
             Time.now]).each do |email|
      email.send_email
    end
  end
end

In this solution, most of the processing is done by the DB.

Add email.rake file in lib/tasks directory:

task :dispatch_emails => :environment do
  Email.dispatch_emails
end

Configure the cron to execute rake dispatch_emails at regular intervals( in your case < 24 hours)

这篇关于使用Ruby on Rails发送基于间隔的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆