Ruby on rails 在RubyonRails中发送邮件,附带一个来自GoogleChartAPI的图像
在这里调用controller.rb后,一个文件(chart.png)将保存在我的rails应用程序文件夹中,那么如何获取该文件并将其附加到邮件中呢 控制器.rbRuby on rails 在RubyonRails中发送邮件,附带一个来自GoogleChartAPI的图像,ruby-on-rails,ruby,open-uri,Ruby On Rails,Ruby,Open Uri,在这里调用controller.rb后,一个文件(chart.png)将保存在我的rails应用程序文件夹中,那么如何获取该文件并将其附加到邮件中呢 控制器.rb def mail @imageURL = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=5&choe=UTF-8" open(@imageURL) do |chart| File.open('chart.png', 'w
def mail
@imageURL = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=5&choe=UTF-8"
open(@imageURL) do |chart|
File.open('chart.png', 'wb') {|f| f.write chart.read }
end
UserMailer.welcome_email(@imageURL, @mailID).deliver
end
我如何将该图像传递到欢迎电子邮件方法中以附加邮件?需要一些帮助来解决这个问题吗
user\u mailer.rb
def welcome_email(imageURL, mailID)
mail(:to => mailID,
:subject => "code",
:body => "Code for the branch "+imageURL+"")
end
end
如果要将其附加到电子邮件,则必须下载图像,然后从文件系统附加 创建附件很容易:
attachments["filename"] = File.read("/path/to/file")
如果我是你,我会在邮件正文的图像标签中添加图像
编辑:我没看到你已经在写文件了
下面是完整的解决方案:
def mail
@imageURL = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=5&choe=UTF-8"
path_image = "/tmp/chart-#{@imageUrl.hash}.png" #Avoid filename collision
open(@imageURL) do |chart|
File.open(path_image, 'wb') {|f| f.write chart.read }
end
UserMailer.welcome_email(@imageURL,@mailID, path_image).deliver
File.delete(path_image)
end
def welcome_email(imageURL,mailID, path_image)
attachments["charts.png"] = File.read(path_image)
mail(:to => mailID,
:subject => "code",
:body => "Code for the branch "+imageURL+"")
end
不熟悉heroku。只需检查是否可以在文件夹/tmp中写入。如果没有,请切换到其他可访问文件夹!我可以将其移动到我的running rails应用程序文件夹中吗?或者如何将其存储在内存中?不确定将其存储在内存中是什么意思,但在开发中测试它,并使用它