oseifrimpong
oseifrimpong

Reputation: 11

How to send an email with an inline image in Rails 4

I am working on a rails application and want to send an email with an inline image to the email address that the user enters. I have been struggling to do it. I checked online and this is what I did.

When I enter an email in development it gives me an error saying, ActionController::UnknownFormat

This is my ActionMailer Notifier.rb

class Notifier < ActionMailer::Base
default from: "[email protected]"



def enter_email(sending)

@coming = sending
attachments.inline['blank'] = {
:data => File.read("#{Rails.root.to_s + '/app/assets/images/News.png'}"),
:mime_type => "image/png",
:encoding => "base64"
 }
mail :to => @coming.email, :subject => 'InsideSpintex is coming soon'

   end
  end

This is my ApplicationController LandingController.rb

class LandingController < ApplicationController

layout 'landingPage'

def soon

    @coming = Coming.new

end

def after
    render 'after'
end

def save_email
    @coming = Coming.new(soon_params)


    respond_to do |format|
        if @coming.save
            Notifier.enter_email(@coming).deliver
             format.html { render action: 'after' }
            #render 'after'
         else
            render 'soon'

    end
 end
end
#render 'soon'
private
    def soon_params
        params.require(:coming).permit(:email)
    end
end

And this is my View enter_email.html.erb

<p> Hello There,</h3>
    <%= image_tag attachments['News.png'] %> 

Upvotes: 1

Views: 2144

Answers (1)

berislavbabic
berislavbabic

Reputation: 459

Well, I think that you don't need the encoding part in your attachments.inline hash. I was using something like this on a project:

Mailer method:

attachments.inline['logo.png'] = {
  data: File.read(Rails.root.join('app/assets/images/logo.png')),
  mime_type: 'image/png'
}

and you are missing the .url in the view:

<%= image_tag attachments['logo.png'].url %>

Upvotes: 4

Related Questions