1

How can I insert my gravatar image in an email that I want to send out with Ruby on Rails? According to Gravatar I need to insert the following code into my Ruby on Rails app:

image_src = "http://www.gravatar.com/avatar/#{hash}"

Hash is essentially an encoded string for an email address. Hash is equal to:

hash = Digest::MD5.hexdigest(email_address)

I created a method called "gravatarurl", which you can see below:

def gravatarurl
  hash = Digest::MD5.hexdigest("myemail@google.com")
  puts "http://www.gravatar.com/avatar/#{hash}"
end

Now, the code that I have in the mailer is:

def emailwithphoto
  @user1 = User.find(1)

  mail(to: "#{@user1.email}",
    from: "MNC@mydomain.com",
    subject: "Attempting to include image in this email")
end

In file app/views/image_mailer/emailwithphoto.rb I have the code:

<img alt="" src="<%= @user1.gravatarurl %>" class="mcnImage">

If I hard code my gravatar url into the file "emailwithphoto.rb" then I can see my gravatar photo, but I need the email to be dynamic.

What am I missing or not seeing? Thank you in advance for your help!

Mauricio
  • 127
  • 1
  • 9

2 Answers2

1

I changed the code in method gravatarurl. I changed "put" and replaced it with "return. This is the new code for method gravatarurl:

def gravatarurl
  hash = Digest::MD5.hexdigest("myemail@google.com")
  return "http://www.gravatar.com/avatar/#{hash}"
end

Now I can use the Gravatar image in emails.

Mauricio
  • 127
  • 1
  • 9
0

You should have gravatarurl in the user model and then call @user1.gravatarurl and not @image1.gravatarurl

Zepplock
  • 28,655
  • 4
  • 35
  • 50
  • Hello @Zepplock, I wrote the wrong code in my question. When I put "@user1.gravatarurl", the email did not send me the image of my Gravatar. – Mauricio Aug 27 '15 at 20:43