Add attachment to email using action Mailer - Rails 4 -


i have book model has title, page_no , description. there model image.

class book < activerecord::base     has_many :images, dependent: :destroy end   class image < activerecord::base   belongs_to :book end 

i have used paperclips upload images.i need create button in book's show page should send book's attributes along images in email.for have configured action mailer as:

rails g mailer bookmailer 

and created app/views/book_mailer/booklist_email.html.erb

<!doctype html> <html>   <head>     <meta content='text/html; charset=utf-8' http-equiv='content-type' />   </head>   <body>     <h1>welcome student library</h1>     <p>       book: <%= @book.title %>     </p>     <p>       page number: <%= @book.page_number %>     </p>     <p>       description <%= @book.description %>     </p>     <p>       active <%= @book.active %>     </p>     <p>       please visit again !!     </p>   </body> </html> 

also placed button in book's show page as(views/books/show.html.erb)

<%= content_tag(:button, "send email", :type=>:submit)%> 

booklist_email.html.erb

class bookmailer < applicationmailer     default from: "unauthorized.testing@gmail.com"      def booklist_email(book, email_id_to)         @book = book         mail(to: email_id_to, subject: 'list of books images')     end end 

now how should trigger email after clicking button book show pages should send email user email should given @ time of pressing send email button book show page. let me know if need provide more information. confused how should proceed. please help

you first need create method in bookmailer if it's not there, parameters want send user:

class bookmailer < activemailer::base   def booklist_email(email, title)     mail(from: 'me@me.com', to: email, subject: title)   end 

then deliver mail:

bookmailer.booklist_email(@user.email, 'hello customer!').deliver 

Comments