2012-07-31 2 views
0

에서 하나의 이미지를 첨부 한 레일즈에서 ruby ​​메일 보내기 ruby ​​on controller.rb 다음에 하나의 파일 (chart.png)이 내 레일 앱 폴더에 저장됩니다. 우편으로 첨부 할 것인가?google-Chart-API

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 

답변

2

, 당신은 이미지를 다운로드 한 다음 파일 시스템에서 첨부해야 할 것이다.

만들기 부착이 용이 : 내가 당신이라면

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

가, 난 그냥 이메일의 본문에 IMAGE_TAG에

편집 이미지를 추가 할 것입니다 : 당신이 있었다 보지 않았다 이미 파일을 쓰고있어.

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 
+0

감사 :

그래서 여기에 완벽한 솔루션입니다. heroku와 함께 작동하면 어떤 문제가 있습니까? – amtest

+0

heroku에 익숙하지 않습니다. 그냥/tmp 폴더에 쓸 수 있는지 확인하십시오. 그렇지 않은 경우 다른 액세스 가능한 폴더로 전환하십시오! –

+0

나는 그것을 실행중인 레일 앱 폴더로 옮길 수 있습니까? 또는 그것을 메모리에 저장하는 방법? – amtest