amtest
amtest

Reputation: 700

image attachment in mail with rails3

Here i need to attach one image with mail, the passing image like this

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

class UserMailer < ActionMailer::Base
 default :from => "[email protected]"

 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

here i got some attachment error. Have any changes in this attachment?

thanks

Upvotes: 0

Views: 441

Answers (2)

Muhammad Sannan Khalid
Muhammad Sannan Khalid

Reputation: 3137

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

where type* is the type of attached file in your case it will be('image/png')

Upvotes: 0

Aditya Sanghi
Aditya Sanghi

Reputation: 13433

i think you have a URL i.e a string which File.read cannot 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

The above should do the trick i think.

Upvotes: 2

Related Questions