<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -88,6 +88,7 @@ if ARGV[0] # path/to/people.csv or path/to/groups.csv
           end
         end
         agent.push
+        agent.send_notification
         puts &quot;Completed at #{Time.now.strftime('%m/%d/%Y %I:%M %p')}\n\n&quot;
       else
         puts &quot;Nothing to push\n\n&quot;</diff>
      <filename>bin/update_onebody</filename>
    </modified>
    <modified>
      <diff>@@ -1,6 +1,11 @@
 site:       http://localhost:3000
 user_email: admin@example.com
 user_key:   dafH2KIiAcnLEr5JxjmX2oveuczq0R6u7Ijd329DtjatgdYcKp
+notifications:
+  from_email: noreply@crccministries.com
+  to_email:   youremail@example.com
+  host:       mailserver
+  port:       25
 #converter:
 #  name: ACS
 #  visible:</diff>
      <filename>example.yml</filename>
    </modified>
    <modified>
      <diff>@@ -1,6 +1,11 @@
 site:       http://localhost:3000
 user_email: admin@example.com
 user_key:   dafH2KIiAcnLEr5JxjmX2oveuczq0R6u7Ijd329DtjatgdYcKp
+notifications:
+  from_email: noreply@crccministries.com
+  to_email:   youremail@example.com
+  host:       mailserver
+  port:       25
 converter:
   name: ACS
   visible:</diff>
      <filename>example_using_converter.yml</filename>
    </modified>
    <modified>
      <diff>@@ -4,6 +4,7 @@ require 'fastercsv'
 require 'highline/import'
 require 'activeresource'
 require 'digest/sha1'
+require 'net/smtp'
 
 HighLine.track_eof = false
 </diff>
      <filename>lib/onebody-updateagent.rb</filename>
    </modified>
    <modified>
      <diff>@@ -116,6 +116,7 @@ class UpdateAgent
   
   # use ActiveResource to create/update records on remote end
   def push
+    @errors = []
     puts 'Updating remote end...'
     index = 0
     print &quot;#{resource.name} 0/0\r&quot;; STDOUT.flush
@@ -123,7 +124,9 @@ class UpdateAgent
       response = resource.post(:batch, {}, records.to_xml)
       statuses = Hash.from_xml(response.body)['records']
       statuses.select { |s| s['status'] == 'error' }.each do |status|
+        record = data_by_id[status['legacy_id']]
         puts &quot;#{status['legacy_id']}: #{status['error']}&quot;
+        @errors &lt;&lt; {:record =&gt; record, :error =&gt; status['error']}
       end
       index += records.length
       print &quot;#{resource.name} #{index}/#{@create.length + @update.length}\r&quot;; STDOUT.flush
@@ -132,6 +135,32 @@ class UpdateAgent
     puts
   end
   
+  def send_notification
+    if n = @options['notifications']
+      puts 'Sending notification...'
+      if @errors and @errors.any?
+        subject = 'OneBody UpdateAgent Errors'
+        body = &quot;There was one or more errors running UpdateAgent:\n\n&quot;
+        @errors.each do |error|
+          body &lt;&lt; error[:error] + &quot;\n&quot;
+          body &lt;&lt; error[:record].inspect + &quot;\n\n&quot;
+        end
+        body &lt;&lt; &quot;\n#{@create.length} create(s) were attempted.\n#{@update.length} update(s) were attempted.&quot;
+      else
+        subject = 'OneBody UpdateAgent Success'
+        body = &quot;OneBody UpdateAgent completed without any errors.\n&quot;
+        body &lt;&lt; &quot;\n#{@create.length} create(s) were completed.\n#{@update.length} update(s) were completed.&quot;
+      end
+      Net::SMTP.start(n['host'], n['port'].to_i) do |smtp|
+        smtp.send_message(
+          &quot;From: #{n['from_email']}\nTo: #{n['to_email']}\nSubject: #{subject}\n\n#{body}&quot;,
+          n['from_email'],
+          n['to_email']
+        )
+      end
+    end
+  end
+  
   def data_by_id
     @data_by_id ||= begin
       by_id = {}</diff>
      <filename>lib/updateagent/updateagent.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>24a80df9adbc6594d7aae1b0489417a66fe96d15</id>
    </parent>
  </parents>
  <author>
    <name>Tim Morgan</name>
    <email>tim@timmorgan.org</email>
  </author>
  <url>http://github.com/seven1m/onebody-updateagent/commit/63d8ff08056ab6a771c0daabd333dd63452c9751</url>
  <id>63d8ff08056ab6a771c0daabd333dd63452c9751</id>
  <committed-date>2009-09-30T12:42:35-07:00</committed-date>
  <authored-date>2009-09-30T05:14:42-07:00</authored-date>
  <message>Add 'notifications' section to config file. Sends email upon completion.</message>
  <tree>fadf7a3fe8667727c0611161ec3ef0a2395e1e2d</tree>
  <committer>
    <name>Tim Morgan</name>
    <email>tim@timmorgan.org</email>
  </committer>
</commit>
