Mandrill API sending scheduled emails with send_at

别说谁变了你拦得住时间么 提交于 2020-06-27 22:49:00

问题


The documentation isn't very clear how to use the send_at parameter. I am sending a scheduled email using the function below. I have tried different versions of the datetime required by the API documentation.

All my emails are being send right away and not scheduled.

  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


回答1:


Found the answer, you have to include async and ip_pool if you want to use scheduled emails..

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



回答2:


from the mandrill api doc:

when this message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. If you specify a time in the past, the message will be sent immediately. An additional fee applies for scheduled email, and this feature is only available to accounts with a positive balance.

If I would have to guess I would guess that send_at = time_now needs to become send_at = time_now.to_s to make send_at respect the format needed.



来源:https://stackoverflow.com/questions/33004850/mandrill-api-sending-scheduled-emails-with-send-at

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