0

Here after called the controller.rb, one file(chart.png) will save in my rails app folder, so how to take this and will attach with mail?

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

how can i pass that image into welcome_email method for attaching with the mail? need some help to solve this?

user_mailer.rb

def welcome_email(imageURL, mailID)
  mail(:to => mailID,
   :subject => "code",
   :body => "Code for the branch "+imageURL+"")
  end
end
amtest
  • 690
  • 1
  • 6
  • 26

1 Answers1

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
Anthony Alberto
  • 10,325
  • 3
  • 34
  • 38