DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Sending A Excel Database Dump To Your Client Via. ActiveMailer & PhpMyAdmin

08.23.2006
| 3812 views |
  • submit to reddit
        Send a excel file database dump to a clients using ActiveMailer and phpMyAdmin. 


require 'rubygems'
require 'pp'
require_gem 'actionmailer'
require 'net/https'

ActionMailer::Base.server_settings = {
  :address => 'mail.myserver.com',
  :port => 25,
  :domain=> 'myserver.com',
  :user_name=> 'admin',
  :password=> 'mypassword',
  :authentication=> :login
}
ActionMailer::Base.template_root = 'templates'
ActionMailer::Base.delivery_method = :smtp

class MyMailer < ActionMailer::Base
  
  def excel ( email_address )
    get_excel
    date         = Time.new.strftime('%m%d%Y')
    @recipients  = email_address
    @subject     = "[myserver.com] Database Dump : #{date}"
    @from        = "admin@myserver.com"
    attachment "application/vnd.ms-excel" do |a|
      a.body = File.read('mydatabase.xls')
      a.filename = "mydatabase-#{date}.xls"
    end
  end
  
  private 
  
  def get_excel
    http = Net::HTTP.new( 'www.myserver.com', 80 )
     http.use_ssl = false
     http.start do |http|
      request = Net::HTTP::Get.new('/dh_phpmyadmin/mysql.myserver.com/export.php?db=mydatabase&export_type=database&what=htmlexcel&header_comment=&sql_compat=NONE&sql_structure=structure&sql_auto_increment=1&use_backquotes=1&sql_data=data&showcolumns=yes&extended_ins=yes&max_query_size=50000&hexforbinary=yes&sql_type=insert&latex_caption=yes&latex_structure=structure&latex_structure_caption=Structure+of+table+__TABLE__&latex_structure_continued_caption=Structure+of+table+__TABLE__+%28continued%29&latex_structure_label=tab%3A__TABLE__-structure&latex_data=data&latex_showcolumns=yes&latex_data_caption=Content+of+table+__TABLE__&latex_data_continued_caption=Content+of+table+__TABLE__+%28continued%29&latex_data_label=tab%3A__TABLE__-data&latex_replace_null=%5Ctextit%7BNULL%7D&csv_data=csv_data&export_separator=%3B&enclosed=%22&escaped=%5C&add_character=%5Cr%5Cn&csv_replace_null=NULL&excel_data=excel_data&excel_replace_null=NULL&excel_edition=win&htmlexcel_data=htmlexcel_data&htmlexcel_replace_null=NULL&htmlword_structure=structure&htmlword_data=data&htmlword_replace_null=NULL&pdf_data=pdf_data&pdf_report_title=&xml_data=xml_data&asfile=sendit&filename_template=__DB__&remember_template=on&compression=none')
      request.basic_auth 'myusername', 'mypassword'
      response = http.request(request)
      File.open('mydatabase.xls', 'r+'){ |f| 
        f << response.body
      }
    end 
  end
  
end
  
MyMailer.deliver_excel('client@clientserver.com')