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

发布于 2024-11-10 06:40:08 字数 1937 浏览 3 评论 0原文

我制作了一个简单的表单来发送带有附件的电子邮件。问题是我不知道如何让它在邮件程序中工作。到目前为止,我找到的所有教程都涵盖了附件文件已经位于服务器某处并且我无法找到有关验证电子邮件内容的任何内容的情况。

所以,我有两个问题要问你:

  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) 并且它正在添加附件,但文件已损坏且无法删除打开...

任何想法或链接将不胜感激。

I've made a simple form for sending emails with attachments. The problem is that I don't know how to make it work in the mailer. All tutorials that I've found so far are covering the scenario when the file for the attachment is already somewhere one the server and I wasn't able to find anything about validating email contents.

So, I've got 2 questions for you:

  1. How can I let users send emails with attachments uploaded by them?
  2. How can I validate user's inputs and extensions of attachments?

My email form...

<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' %>

and my mailer...

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

I came up with attachments[file.original_filename] =File.read(file.path) and it's adding attachments but the files are coruppted and cannot be opened...

Any thoughts or links would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还如梦归 2024-11-17 06:40:08

我发现如果你想在rails中发送带有附件的电子邮件,你需要以这种方式附加它...

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+\-.]+@[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分钟。您对如何加快这一过程有什么建议吗?也许您可以推荐其他提供商或解决方案?

附言。不要忘记检查“客户端验证”Railscasts #263

I've found that if you want to send email with attachment in rails, you need to attach it that way...

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

So the whole mailer method might look like this...

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

As for the validations, there are two methods that I've found. First one is quite obvious. You have to create a table in your database and validate form's inputs in model just like you always do. If you do it that way than everything will be saved in your database. It has some advantages, for exmple: you can use it as an archive or for some kind of statistics about your clients in your app or even put it through some sql triggers. However, if you don't want to save anything than you can create "tableless model" (Railscasts #193 and original Codetunes). All you have to do is to place this code at the begining of your model:

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

Than you have to list your columns...

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

And after that you can place your model's code...

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..."

Right now, I'm using Gmail for sending emails but it's quite slooow. It takes about 2 minutes to send a short message with 2Kb test attachment. Have you got any suggestions about how to speed up this process? Maybe you can recomend another provider or solution?

PS. Don't forget to check out 'client side validations' Railscasts #263

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文