Mandrill API使用send_at发送预定的电子邮件

时间:2015-10-08 00:52:55

标签: ruby email mandrill

文档不太清楚如何使用send_at参数。我正在使用以下功能发送预定的电子邮件。我已经尝试了API文档所需的不同版本的日期时间。

我的所有电子邮件都是立即发送的,而不是预定的。

  def send_member_email_batch(current_user, recipient_emails, recipient_names, subject, body, scheduled)
    m = Mandrill::API.new ENV["MANDRILL_APIKEY"]

    recipient_emails.each_with_index do |recipient, index|
      to_address = (recipient_emails.length > 1) ? [{"email"=>recipient,"name"=>recipient_names[index], "type"=>"to"}] :
            [{"email"=>recipient,
              "name"=>recipient_names[index],
              "type"=>"to"}, 
            {"email"=>current_user.email,
              "name"=>current_user.name,
              "type"=>"cc"}]

      message = { 
        :from_name=> current_user.name,
        :from_email=> current_user.email,
        :to=> to_address,
        :subject=> temp_subject, 
        :html=> temp_body,
        :auto_text=>true,
        :tags=> ["members"],    
        :track_opens=>true,
        :track_clicks=>true,        
        :preserve_recipients => false
      } 
        time_now = DateTime.now.utc
        time_now += (1 + ((5-time_now.wday) % 7))
        time_now = time_now.change({hour: 12, min: 3, sec: 0 }).strftime('%F %T')
        puts time_now #2015-10-10 12:03:00

        send_at = time_now

      #puts "to: #{recipient}, subject = #{temp_subject}, message = #{temp_body}"
      begin
        result = m.messages.send message, send_at
        puts result #email is not scheduled
      rescue Mandrill::Error => e
        puts "A mandrill error occurred: #{e.class} - #{e.message}" 
        next
      end
    end
  end

https://mandrillapp.com/api/docs/messages.ruby.html#method=send

2 个答案:

答案 0 :(得分:1)

找到答案,如果你想使用预定的电子邮件,你必须包括async和ip_pool ..

async = false
ip_pool = "Main Pool"
send_at = "example send_at"
result = mandrill.messages.send message, async, ip_pool, send_at

答案 1 :(得分:-1)

来自mandrill api doc:

此消息应作为YYYY-MM-DD HH:MM:SS格式的UTC时间戳发送。如果您指定过去的时间,则会立即发送消息。预定电子邮件需要支付额外费用,此功能仅适用于余额为正的帐户。

如果我不得不猜测我会猜测send_at = time_now需要成为send_at = time_now.to_s才能使send_at尊重所需的格式。