2011-05-26 107 views
0

我製作了一個簡單的發送附件郵件的表單。問題是我不知道如何使它在郵件程序中工作。到目前爲止,我發現的所有教程都覆蓋了該場景,當附件的文件已經在服務器的某個位置時,我無法找到任何有關驗證電子郵件內容的信息。Rails 3,操作郵件程序,電子郵件內容的附件和驗證

所以,我要告訴你2個問題:

  1. 我怎樣才能讓用戶發送電子郵件,與他們上傳的附件?
  2. 如何驗證用戶的輸入和附件擴展?

我的電子郵件的形式......

<div id="form_wrapper"> 
    <%= form_for(:kontakt, :url => {:action => 'kontakt'}, :html => { :multipart => true }, :remote=>true) do |f| %> 
    <ul> 
    <li> 
     <%= f.label(:email) %> 
     <%= f.text_field(:email) %> 
    </li> 
    <li> 
     <%= f.label(:content) %> 
     <%= f.text_area(:content, :size => "42x7") %> 
    </li> 
    <li> 
     <%= f.label(:preview, :class=>:preview)%> 
     <%= f.file_field :preview %> 
    </li> 
    </ul> 
    <%= image_submit_tag("blank.gif",:id=>"send_email", :class=>"action submit") %> 
    <%= link_to("Reset", {:controller=>'frontend',:action => 'index'},:remote => true, :class => 'action reset') %> 
<% end %> 
</div> 
<%= javascript_include_tag 'jquery.form.js','jquery.rails','jquery.remotipart' %> 

和我的郵件...

class Contact < ActionMailer::Base 
    default :from => "[email protected]" 
    def wiadomosc(email) 
    @content=email[:content] 
    file=email[:preview] 
    attachments[file.original_filename] =File.read(file.path) 
    mail(
     :subject=>"www.XXXXXX.pl - you've got a new message", 
     :reply_to =>"[email protected]", 
     :to => email[:email] 
    ) 
    end 
end 

我想出了attachments[file.original_filename] =File.read(file.path)和它的添加附件但文件coruppted並不能打開...

任何想法或鏈接將不勝感激。

回答

7

我發現,如果你想在軌附件發送電子郵件,您需要連接這種方式......

attachments[file.original_filename] = File.open(file.path, 'rb'){|f| f.read} 

所以整個郵件的方法可能是這樣的......

def message(email) 
    @content=email[:content] 
    unless email[:email_attachment].nil? 
    file=email[:email_attachment] 
    attachments[file.original_filename] = File.open(file.path, 'rb'){|f| f.read} 
    end 
    mail(
    :subject=>"www.XXXXXX.pl - you've got a new message", 
    :reply_to =>"[email protected]", 
    :to => email[:email] 
) 
end 

至於驗證,有兩種方法,我找到了。第一個很明顯。您必須在您的數據庫中創建一個表格,並像以往一樣驗證表單在模型中的輸入。如果你這樣做,那麼一切都將保存在你的數據庫中。它具有一些優點,例如:可以將它用作歸檔文件或用於應用程序中關於客戶端的某種統計信息,甚至可以通過某些sql觸發器進行處理。但是,如果您不想保存任何內容,則可以創建「無表模型」(Railscasts #193和原始Codetunes)。所有你需要做的是把這段代碼在你的模型的開頭:

def self.columns() @columns ||= []; end 

def self.column(name, sql_type = nil, default = nil, null = true) 
    columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null) 
end 

比你要列出你列...

column :name, :string 
column :company, :string 
column :email, :string 
column :content, :text 
column :type_of_question, :string 
column :email_attachment, :string 

之後,你可以將你的模型代碼...

has_attached_file :email_attachment 

EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
TYPES = ['application/zip', 'multipart/x-zip', 'application/x-zip-compressed'] 

validates :name, :presence => {:message => "Blah blah blah"} 
validates :email, :presence => {:message => "Blah blah blah"}, 
        :format=>{:unless=> Proc.new{|s| s.email.nil?|| s.email.empty? }, 
          :with => EMAIL_REGEX, :message => "Blah blah blah"} 
validates :content, :presence => {:message => "Blah blah blah"} 
validates_inclusion_of :type_of_question, :in => ["Blah1", "Blah2", "Blah3"], 
         :message => "%{value} is not on the list of types" 
validates_attachment_content_type :email_attachment, :content_type => TYPES, 
            :message => "The attachment has wrong extension..." 

現在,我使用Gmail發送電子郵件,但它很流利。用2Kb測試附件發送短消息大約需要2分鐘。你有沒有關於如何加快這個過程的建議?也許你可以推薦其他提供商或解決方案?

PS。不要忘記檢查'客戶端驗證'Railscasts #263