每天一剂Rails良药之Sending Email With Attachments

今天来看看使用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

你可能感兴趣的:(C++,c,linux,Rails,Gmail)