0

ここで controller.rb を呼び出した後、1 つのファイル (chart.png) が私の rails app フォルダーに保存されるので、これをどのように取得してメールに添付しますか?

controller.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', 'wb') {|f| f.write chart.read }
  end  
  UserMailer.welcome_email(@imageURL, @mailID).deliver
end

その画像をwelcome_emailメソッドに渡してメールに添付するにはどうすればよいですか? これを解決するために助けが必要ですか?

user_mailer.rb

def welcome_email(imageURL, mailID)
  mail(:to => mailID,
   :subject => "code",
   :body => "Code for the branch "+imageURL+"")
  end
end
4

1 に答える 1

2

If you want to attach it to the email, you'd have to download the image, and then attach it from the file-system.

Creating attachment is easy :

attachments["filename"] = File.read("/path/to/file")

If I were you, I'd just add the image in an image_tag in the body of the email

Edit : I didn't see you were already writing the file.

So here is the complete solution :

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
于 2012-07-31T19:35:03.340 に答える