Rails 3,Action Mailer,电子邮件内容的附件和验证

问题描述

| 我已经制作了一个简单的表格,用于发送带有附件的电子邮件。问题是我不知道如何使它在邮件程序中工作。到目前为止,我发现的所有教程都涵盖了以下场景:附件的文件已经在服务器的某个位置,而我无法找到有关验证电子邮件内容的任何信息。 因此,我有两个问题要问您: 如何让用户发送带有他们上传的附件的电子邮件? 如何验证用户的输入和附件扩展名? 我的电子邮件表格...
<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)
,它正在添加附件,但文件已损坏,无法打开... 任何想法或链接将不胜感激。     

解决方法

我发现,如果您要发送带有附件的电子邮件,请以这种方式附加...
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触发器进行使用。但是,如果您不想保存任何东西,则可以创建\“ tableless model \”(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+\\-.]+@[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