原创作者: hideto   阅读:937次   评论:0条   更新时间:2011-05-26    
今天来看看使用Rails发送带附件的邮件
Controller
app/controllers/spam_controller.rb:
class SpamController < ApplicationController
  def spam
    Spammer.deliver_spam_with_attachment(params[:name],
                                         params[:email],
                                         params[:file])
    redirect_to :action => "index"
  end
end


Mailer
app/models/spammer.rb
class Spammer < ActionMailer::Base
  def spam_with_attachment(name, email, file)
    @subject    = 'Have a Can of Spam!'
    @body       = {:name => name}
    @recipients = email
    @from       = 'spam@chadfowlercom'
    unless file.blank?
      attachment :body => file.read, :filename => file.original_filename
    end
  end
end


Views
app/views/spam/index.rhtml
<%= form_tag({:action => "spam"}, :multipart => true) %>
  <label for="name">Name of recipient:</label>
  <%= text_field_tag "name" %><br/>
  <label for="email">Email address to send to:</label>
  <%= text_field_tag "email" %><br/>
  <label for="file">File to upload:</label>
  <%= text_field_tag "file" %><br/>
  <%= submit_tag "Spam!" %>
<% end_form_tag %>


app/views/spammer/spam_with_attachment.rhtml
Hey <%= @name %>,
I thought you'd appreciate this file.

Regards,
Chad
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics