0

I am creating a gem, to send notitifications

Notifications are sent to user using his email address, so to send notification am going to send text-email to user.

So how to send email from gem? as I need to inherit the class from Actionmailer::Base

Module ABC
  class notify < ActionMailer::Base
    def send_mail
    end
  end
end

Do I need to add any gem in my gemspec file? rails or actionmailer gem? Thanks in advance

Swati
  • 842
  • 10
  • 26
  • `ActionMailer` is self sufficient and perfect for the task. Why do you want to create a gem? – shivam Nov 30 '14 at 11:37
  • Its a notification to 3rd party, and am sending notification to them.. so want to send email them, hence adding gem for internal purpose.. It would be helpful if you can help me to solve this doubt – Swati Nov 30 '14 at 11:43

1 Answers1

0

I still cannot understand your motive behind creating a gem. I would however have done something like this: Create app/model/notify.rb

class notify < ActionMailer::Base
  default :from => "notify@example.com", :content_type => "text/html"
  def send_mail(user)
    @user = user
    mail(:to => @user.email, :subject => "Notification mail from xyz")
  end
end

Now create a mailer view app/views/notify/send_mail.erb

Hi @user.name,
Here is your notification.
Thanks

This can now be called from anywhere in your code. All you have to do is:

Notify.send_mail(user).deliver

You can keep on adding different methods and their template view and send them as

Notify.your_method(user).deliver
shivam
  • 16,048
  • 3
  • 56
  • 71
  • Tried this, but getting error uninitialized constant ABC::ActionMailer (NameError) its not getting from where to refer ActionMailer (ABC is my module name).. do we need to add any depedency in gemspec? – Swati Nov 30 '14 at 12:04
  • have you configured your actionmailer in config/application.rb? – shivam Nov 30 '14 at 12:06
  • Sorry, but I don't have any config-folder in my gem structure.. all I have is lib folder, gemspec, gemfile etc. – Swati Nov 30 '14 at 12:09
  • read this: http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views – shivam Nov 30 '14 at 12:20
  • found a partial solution here http://stackoverflow.com/questions/4951310/actionmailer-3-without-rails need to dig in more.. @shivam Thanks for help – Swati Nov 30 '14 at 12:53