Ruby on rails rails3邮件中的图像附件

Ruby on rails rails3邮件中的图像附件,ruby-on-rails,ruby,actionmailer,Ruby On Rails,Ruby,Actionmailer,在这里,我需要附上一个邮件图像,像这样传递图像 **imageurl** = "https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=%22hai%22&choe=UTF-8" class UserMailer < ActionMailer::Base default :from => "mail@example.com" def welcome_email(**imageurl**,bna

在这里,我需要附上一个邮件图像,像这样传递图像

**imageurl** = "https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=%22hai%22&choe=UTF-8"

class UserMailer < ActionMailer::Base
 default :from => "mail@example.com"

 def welcome_email(**imageurl**,bname,mailid)
  **attachments['image.png'] = File.read(imageURL)**
  mail(:to => mailid,
  :subject => "Code for "+bname+"",
  :body => "code for bname" )
  end
 end
end
**imageurl**=”https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=%22hai%22&choe=UTF-8"
类UserMailer”mail@example.com"
def欢迎电子邮件(**imageurl**,bname,mailid)
**附件['image.png']=File.read(imageURL)**
邮件(:to=>mailid,
:subject=>“+bname+”的代码,
:body=>“bname代码”)
结束
结束
结束
这里我有一些附件错误。附件有什么变化吗


谢谢

我想您有一个URL,即File.read无法读取的字符串

require 'open-uri'
class UserMailer < ActionMailer::Base
  def welcome_email(image_url,bname,mailid)
    attachments['image.png'] = open(URI.parse(image_url))
    ...
  end
end
需要“打开uri”
类UserMailer
我认为上面的方法应该可以做到。

需要“openuri”
require 'open-uri'
class UserMailer < ActionMailer::Base
  def welcome_email(image_url,bname,mailid)
    mail.attachments[image.png] = { :mime_type => type*, :content => open(URI.parse(image_url)}
    ...
  end
end
类UserMailertype*,:content=>open(URI.parse(image\u url)} ... 结束 结束

其中type*是您案例中附加文件的类型,它将是('image/png')

是,这将发送一封带有image.png附件的邮件。但是当我打开或下载该图像时,该图像为空。?,从URI.parse()打开该图像有任何问题吗?使用Rails控制台。打开控制台并键入“require open URI”然后键入“URI.parse”('google chart url'),并确保获得URI对象。然后执行“s=open()”以确保可以按预期下载图像。如果没有获得imageish字符串,则会出现问题。分解步骤并进行调试。当我这样使用时,需要“open URI”=>true URI.parse(')=>s=open('))=>#这个URL有什么问题吗?太好了!你得到了StringIO对象!这意味着你可以从远程URL获取一些东西。StringIO很好!现在你可以做了。点击这个对象看看它包含什么,甚至可以做什么。在它上面键入内容,看看它还有什么!这将再次发送一封带有image.png附件的邮件。但是当我打开or下载该图像,该图像为空。?def welcome_email(imageURL,branchNAME,mailID)encoded_content=open(URI.parse(imageURL))attachments['QR.png']={:mime_type=>“image/png”,“content=>encoded_content}mail(:to=>mailID,:subject=>“QR from”+branchNAME+“”,:body=>“分支的QR”+branchNAME+“”)结束您对问题所做的编辑不会对您有所帮助;它们不是实质性的改进,