论坛首页 Ruby版 rails

ActionMailer发送邮件附件错误

浏览 410 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2008-05-12 关键字: actionmailer
使用ActionMailer发送邮件附件时一直处于长时间请求状态,邮件无法发送。
  filename = "D:\\ruby\\rails_apps\\sg\\public\\play.jpg"	
  file = Hash.new()
  file['filename'] = Pathname.new(filename).basename
  file['mimetype'] = MIME::Types.type_for(filename).to_s
  File.open(filename,"rb") { |fp| file['attachment'] << fp.read() }
  attachment(:content_type => file['mimetype'], :filename => file['filename'], :body => file['attachment'])

但是如果我是用下面的代码来测试的话,邮件可以发送,附件也可以发送,但是附件打不开,发送的邮件有问题。
  attachment(:content_type => "image/jpeg", :body => File.read("public/play.jpg"))

各位有遇到这种问题吗?能够给我提点建议,谢谢!
   
最后更新时间:2008-05-12
我把整段代码发出来吧,这样可以理解我的问题所在:
require 'mime/types'
class Mailer < ActionMailer::Base

  def sent(mail, sent_at = Time.now)
		self.raise_delivery_errors = true
		self.smtp_settings = { 
		  :address        => mail.smtp.address , 
		  :port           => mail.smtp.port, 
		  :domain         => mail.smtp.domain, 
		  :authentication => mail.smtp.authentication,
		  :user_name      => mail.smtp.user_name, 
		  :password       => mail.smtp.password, 
		}
		
    @subject    = mail.subject.strip!  #string 邮件标题
    @body       = { :mail => mail.mail.rstrip } #hash 变量值将传递给模板
    @recipients = mail.to #array or string 收件人地址 单个邮件格式:email@site.com or name <email@site.com>,eg:["andy@pragprog.com", "Dave Thomas <dave@pragprog.com>"]
    @from       = mail.from	#array or string 发件人地址 格式跟收件人一样,该地址应该与smtp配置的用户名和域名保持一致
    @sent_on    = sent_at #date 指定邮件日期,如果没有指定,将使用当前的日期和时间
    @headers   	= {} #hash 指定头信息
    @cc					= "" #array or string 抄送目标
    @bcc				= "" #array or string 暗送目标
    
    #发送html邮件请开启下面这个选项:
    @content_type = "text/html"
    
    #分部分发送
    #text/html
		#part :content_type => "text/html", :body => render_message("sent_as_html", :mail => mail)
		
		#text/plain
		#part "text/plain" do |p|
		#	p.body = render_message("sent_as_plain", :mail => mail)
		#	p.transfer_encoding = "utf-8"
		#end
		
  	#附件发送
  	att("#{RAILS_ROOT}/public/play.jpg")
	
    #mail.attachments.each do |att|
			#@attachment   :body => File.read("public/attachment/url/" + att.id.to_s + "/" + att['url']), :filename => att['url']
    #end
    
  end

private
	def att(filename)
  	file = Hash.new()
  	file['filename'] = File.basename(filename)
  	file['mimetype'] = MIME::Types.type_for(filename).to_s
  	file['attachment'] = File.read(filename)
		attachment(:content_type => file['mimetype'], :filename => file['filename'], :body => file['attachment'])    
		file = nil
	end

end
   
0 请登录后投票
最后更新时间:2008-05-14
怎么我发错了一个帖子,版主封了我两天不能发帖?
问题到现在还是没有解决,查找了很多列子,发现在这个方面可以正常发送bmp的小附件:
  	mail.attachments.each do |attach|
  		content_type = case File.extname(attach.filename)
			when ".jpg" ; "image/jpg"
			when ".png" ; "image/x-png"
			when ".gif" ; "image/gif"
			when ".pdf" ; "application/pdf"
			when ".doc" ; "application/msword"
			when ".gz" ; "application/x-gzip"
			when ".tar.gz" ; "application/x-tgz"
			when ".tar" ; "application/x-tar"
			when ".zip" ; "application/zip"
			when ".txt" ; "text/plain"
			when ".xls" ; "application/vnd-ms-excel"
			else ; "application/octet-stream"
  		end
  		@data=""
			File.open("#{RAILS_ROOT}/public/files/#{mail.id}/#{attach.filename}","rb").each {|read| @data<<read}
			attachment :content_type => content_type, :filename => File.basename(attach.filename), :body=>@data
  	end

发送出去终于可以显示了,但是发现如果发送多个附件时,还是有问题,提交后页面一直处于请求状态,邮件还是无法发送出去。
   
0 请登录后投票
论坛首页 Ruby版 rails

跳转论坛:
JavaEye推荐