`
hideto
  • 浏览: 2677310 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • Ruby
阅读更多
今天来看看使用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
分享到:
评论
6 楼 blackanger 2007-05-28  
谢谢。。。
5 楼 hideto 2007-05-28  
我是直接在config/environment.rb里配置smtp:
  config.action_mailer.smtp_settings = { 
    :address => "mail.blueboxgrid.com", 
    :port => 25, 
    :domain => "acceptable.tv", 
    :authentication => :login, 
    :user_name => "xxx@acceptable.tv", 
    :password => "xxxx" 
  }
4 楼 hideto 2007-05-28  
没用过msmtp,可能你的msmtp配置也有问题
3 楼 blackanger 2007-05-28  
但是信件确是没有收到,这个错误不是IO读取配置文件抛出的吗?
2 楼 hideto 2007-05-28  
“Broken pipe/一会发送成功,一会报错”,我想应该是连接不稳定,把gmail的smtp换成别的试试吧
1 楼 blackanger 2007-05-28  
你好,我可以问个问题吗?我是小菜鸟,我在用msmtp的时候,是在linux下,我的environment.rb配置:
ActionMailer::Base.raise_delivery_errors = true 
  
ActionMailer::Base.perform_deliveries = true 
ActionMailer::Base.default_charset = "utf-8" 
        
              
ActionMailer::Base.delivery_method = :msmtp 
              
module ActionMailer 
    class Base 
        def perform_delivery_msmtp(mail) 
            IO.popen("/usr/local/msmtp/bin/msmtp -t -C /usr/local/msmtp/bin/.msmtprc -a gmail --", "w") do |sm| 
                sm.puts(mail.encoded.gsub(/\r/, '')) 
                sm.flush 
            end 
        end 
    end 
end 


我的msmtp配置:
account gmail  
host smtp.gmail.com  
auth on  
user XXXx@gmail.com  
password XXXXX  
tls on  
port 587 
tls_starttls on  
from XXXx@gmail.com  
maildomain gmail.com  
account default : gmail  
点了发送按钮一会发送成功,一会就报错,不知道什么原因。信件是收不到。希望帮我解决一下,谢谢了。
Errno::EPIPE in Send emailController#sendemail

Broken pipe

RAILS_ROOT: script/../config/..
Application Trace | Framework Trace | Full Trace

config/environment.rb:77:in `write'
config/environment.rb:77:in `puts'
config/environment.rb:77:in `perform_delivery_msmtp'
config/environment.rb:76:in `popen'
config/environment.rb:76:in `perform_delivery_msmtp'
app/controllers/send_email_controller.rb:15:in `sendemail'
-e:4:in `load'
-e:4

相关推荐

    Agile Web Development with Rails 4

    Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...

    Agile+Web+Development+with+Rails中文版.pdf

    《Agile Web Development with Rails》是一本经典的Rails开发指南,中文版的出版使得更多的中国开发者能够深入理解并应用敏捷开发方法与Ruby on Rails框架。这本书是Rails开发者的必备参考资料,它详细介绍了如何...

    Rails上的API:使用Rails构建REST APIAPIs on Rails: Building REST APIs with Rails

    Rails以其简洁优雅的语法、高效的开发速度以及良好的社区支持而闻名,这使得它成为构建现代API的理想选择之一。 ### 一、什么是RESTful API REST(Representational State Transfer)是一种软件架构风格,用于定义...

    Agile Web Development with Rails

    《Agile Web Development with Rails》(敏捷Web开发:Ruby on Rails)这本书,作为Rails开发新手的教材,强调了敏捷开发方法,并以其帮助开发者建立起一个实用的Web应用。从给出的文件信息来看,这本书正在编写过程...

    Agile Web Development With Ruby On Rails第一及第二版

    《Agile Web Development With Ruby On Rails》是两本广受欢迎的书籍,主要涵盖了使用Ruby on Rails框架进行敏捷Web开发的知识。这本书的第1版和第2版分别详细讲解了如何运用敏捷开发方法来构建高效、可扩展且易于...

    Agile Web Development with Rails (PDF)

    Rails作为一款基于Ruby语言的Web应用框架,自诞生之初就秉持着敏捷开发的理念。它强调代码的简洁性、易读性和快速迭代能力,这些特性与敏捷开发的原则不谋而合。通过Rails,开发者可以迅速构建功能丰富的Web应用,...

    Agile Web Development with Rails 3nd Edition Beta.pdf

    通过上述分析,我们可以看到,《敏捷Web开发与Rails》第三版不仅是一本关于Rails框架的技术书籍,更是一部指导开发者如何运用敏捷方法论进行Web开发的经典之作。无论对于初学者还是经验丰富的开发者而言,本书都具有...

    agile web development with rails 5(英文电子书)

    agile web development with rails 5(英文电子书).............................................................................................................................................................

    Agile Web Development with Rails 4th edition(敏捷Web开发与Rails:程序指南 第四版)

    1. **ActiveRecord**: ActiveRecord是Rails的核心组件之一,负责处理数据库交互。在第四版中,对ActiveRecord的查询接口进行了优化,提供了更丰富的查询方法,如`pluck`, `exists?`, 和 `includes`,这些都极大地...

    Agile Web Development with Rails,Fourth Edition 第四版

    在Rails 3中,最重要的更新之一是Action Pack的整合,它将Action Controller和Action View合并,简化了MVC(模型-视图-控制器)架构。这一变化减少了代码重复,提高了代码的可读性和可维护性。同时,ActiveRecord的...

    [Michael Hartl] Ruby on Rails Tutorial

    Learn Web Development with Rails Clear EPUB version in English, Second Edition “The author is clearly an expert at the Ruby language and the Rails framework, but more than that, he is a working ...

    Rails 101 入门电子书

    《Rails 101 入门电子书》是一本非常适合初学者直接入门的书籍,它由xdite编写并出版于2014年6月10日。本书主要针对的是希望学习Ruby on Rails框架的读者,特别是那些想要从零开始掌握这项技术的新手。 #### 二、...

Global site tag (gtag.js) - Google Analytics