<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ActiveMailer code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 06 Sep 2008 23:11:38 GMT</pubDate>
    <description>DZone Snippets: ActiveMailer code</description>
    <item>
      <title>Sending a excel database dump to your client via. ActiveMailer &amp; phpMyAdmin</title>
      <link>http://snippets.dzone.com/posts/show/2462</link>
      <description>Send a excel file database dump to a clients using ActiveMailer and phpMyAdmin. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'pp'&lt;br /&gt;require_gem 'actionmailer'&lt;br /&gt;require 'net/https'&lt;br /&gt;&lt;br /&gt;ActionMailer::Base.server_settings = {&lt;br /&gt;  :address =&gt; 'mail.myserver.com',&lt;br /&gt;  :port =&gt; 25,&lt;br /&gt;  :domain=&gt; 'myserver.com',&lt;br /&gt;  :user_name=&gt; 'admin',&lt;br /&gt;  :password=&gt; 'mypassword',&lt;br /&gt;  :authentication=&gt; :login&lt;br /&gt;}&lt;br /&gt;ActionMailer::Base.template_root = 'templates'&lt;br /&gt;ActionMailer::Base.delivery_method = :smtp&lt;br /&gt;&lt;br /&gt;class MyMailer &lt; ActionMailer::Base&lt;br /&gt;  &lt;br /&gt;  def excel ( email_address )&lt;br /&gt;    get_excel&lt;br /&gt;    date         = Time.new.strftime('%m%d%Y')&lt;br /&gt;    @recipients  = email_address&lt;br /&gt;    @subject     = "[myserver.com] Database Dump : #{date}"&lt;br /&gt;    @from        = "admin@myserver.com"&lt;br /&gt;    attachment "application/vnd.ms-excel" do |a|&lt;br /&gt;      a.body = File.read('mydatabase.xls')&lt;br /&gt;      a.filename = "mydatabase-#{date}.xls"&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  private &lt;br /&gt;  &lt;br /&gt;  def get_excel&lt;br /&gt;    http = Net::HTTP.new( 'www.myserver.com', 80 )&lt;br /&gt;     http.use_ssl = false&lt;br /&gt;     http.start do |http|&lt;br /&gt;      request = Net::HTTP::Get.new('/dh_phpmyadmin/mysql.myserver.com/export.php?db=mydatabase&amp;export_type=database&amp;what=htmlexcel&amp;header_comment=&amp;sql_compat=NONE&amp;sql_structure=structure&amp;sql_auto_increment=1&amp;use_backquotes=1&amp;sql_data=data&amp;showcolumns=yes&amp;extended_ins=yes&amp;max_query_size=50000&amp;hexforbinary=yes&amp;sql_type=insert&amp;latex_caption=yes&amp;latex_structure=structure&amp;latex_structure_caption=Structure+of+table+__TABLE__&amp;latex_structure_continued_caption=Structure+of+table+__TABLE__+%28continued%29&amp;latex_structure_label=tab%3A__TABLE__-structure&amp;latex_data=data&amp;latex_showcolumns=yes&amp;latex_data_caption=Content+of+table+__TABLE__&amp;latex_data_continued_caption=Content+of+table+__TABLE__+%28continued%29&amp;latex_data_label=tab%3A__TABLE__-data&amp;latex_replace_null=%5Ctextit%7BNULL%7D&amp;csv_data=csv_data&amp;export_separator=%3B&amp;enclosed=%22&amp;escaped=%5C&amp;add_character=%5Cr%5Cn&amp;csv_replace_null=NULL&amp;excel_data=excel_data&amp;excel_replace_null=NULL&amp;excel_edition=win&amp;htmlexcel_data=htmlexcel_data&amp;htmlexcel_replace_null=NULL&amp;htmlword_structure=structure&amp;htmlword_data=data&amp;htmlword_replace_null=NULL&amp;pdf_data=pdf_data&amp;pdf_report_title=&amp;xml_data=xml_data&amp;asfile=sendit&amp;filename_template=__DB__&amp;remember_template=on&amp;compression=none')&lt;br /&gt;      request.basic_auth 'myusername', 'mypassword'&lt;br /&gt;      response = http.request(request)&lt;br /&gt;      File.open('mydatabase.xls', 'r+'){ |f| &lt;br /&gt;        f &lt;&lt; response.body&lt;br /&gt;      }&lt;br /&gt;    end &lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;  &lt;br /&gt;MyMailer.deliver_excel('client@clientserver.com')&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 24 Aug 2006 01:42:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2462</guid>
      <author>ericgoodwin (Eric Goodwin)</author>
    </item>
  </channel>
</rss>
