I posted all this in the wiki here, but in case it changes and simply as documentation for myself, I am reposting it here. Much of it is pieced together from various places.
= Installation =
The gem merb-mailer is already installed as part of the standard merb installation via sudo gem install merb. See http://github.com/wycats/merb/tree/master
But if you are missing it, do:
1 sudo gem install merb-mailer
It will install mailfactory gem and mime-types that mailfactory depends on.
= Configuration =
Assuming you created an app using ‘merb-gen app’, merb-mailer will already be listed in config/dependencies.rb. If you started your app using ‘merb-gen core’ then you will need to include merb-mailer as a dependency like the following:
In init.rb, put this at the top
1 require 'config/dependencies.rb'
In config/dependencies.rb
1 # dependencies are generated using a strict version, don't forget to edit the dependency versions when upgrading. 2 merb_gems_version = "1.0" 3 dm_gems_version = "0.9.6" 4 5 # For more information about each component, please read http://wiki.merbivore.com/faqs/merb_components 6 dependency "merb-mailer", merb_gems_version
In init.rb configure for sendmail (see below for other config options like smtp). Alternatively, you can put this in development.rb if your production.rb email setup might use smtp instead or vice versa. Development.rb and production.rb will override the init.rb file.
1 Merb::BootLoader.after_app_loads do 2 # This will get executed after your app's classes have been loaded. 3 Merb::Mailer.config = {:sendmail_path => '/usr/sbin/sendmail'} 4 Merb::Mailer.delivery_method = :sendmail 5 end
(Type “which sendmail” to verify where sendmail is located on your machine.)
See http://merbivore.com/documentation/current/doc/rdoc/merb-mailer-1.0/index.html for documented config options.
= Using Merb mailers =
Merb separates out its mailers into its only little MVC so things work a little differently than you might be used to with Rails. But it’s for the better.
== First, setup the mailer. ==
1 merb-gen mailer contact
1 merb-gen mailer contactThis will generate a mailer controller and a mailer view under app/mailers
Open up app/mailers/contact_mailer.rb and make it look like the following:
1 class ContactMailer < Merb::MailController 2 3 def notify 4 @user = params[:user] 5 render_mail 6 end 7 8 endEdit app/mailers/views/contact_mailer/notify.text.erb
1 Tall tube socks with red stripes. The right sock has a large whole near the big toe.== Now put the mailer to use inside a controller ==
Generate a static controller1 merb-gen controller staticCreate an index action and contact action for the static controller
1 class Static < Application 2 3 # ...and remember, everything returned from an action 4 # goes to the client... 5 def index 6 render 7 end 8 9 def contact 10 send_mail(ContactMailer, :notify, { 11 :from => 'yourself@email.com', 12 :to => "someoneelse@gmail.com", 13 :subject => "Free gym socks. Used!" 14 }) 15 16 render 17 end 18 endMake sure your routes are setup properly in router.rb
1 Merb::Router.prepare do 2 match('/index').to(:controller => "static", :action => "index").name(:index) 3 match('/contact').to(:controller => 'static', :action =>'contact').name(:contact) 4 match('/').to(:controller => 'static', :action =>'index') 5 endMake sure your views (static/index.html.erb and static/contact.html.erb) are setup to some degree
1 # index.html 2 <h2>Home page</h2> 3 4 # contact.html 5 <h2>Contacto!</h2>Navigate to http://localhost:4000/contact and it should work!
See http://github.com/wycats/merb/tree/master/merb-mailer for additional details.
It you have problems make sure postfix is running with sudo postfix stop and then sudo postfix start). You should also see the email get sent in your merb development server output. It should look something like the following:
1 merb : worker (port 4000) ~ notify sent to someoneelse@gmail.com about =?utf-8?Q?Free_gym_socks._Used!?== Creating a contact form =
This assumes you’ve followed the above instructions so far. This also assumes usage with datamapper.Create a contact model
1 merb-gen model contactEdit models/contact.rb to look like the following:
1 class Contact 2 include DataMapper::Resource 3 4 property :id, Serial 5 property :name, String, :nullable => false 6 property :email, String, :nullable => false 7 property :message, String, :nullable => false 8 9 validates_format :email, :as => :email_address 10 11 endEdit the static controller’s contact action. This renders the contact page unless the form is submitted using request.post? Then the contact information is saved with datamapper (but not to the database) and our ContactMailer notify action is used to send the mailer.
1 # static.rb controller 2 def contact 3 if request.post? 4 @contact = Contact.new(params[:contact]) 5 if @contact.valid? 6 send_mail(ContactMailer, :notify, { 7 :from => @contact.email, 8 :to => 'scott@scottmotte.com', 9 :subject => "New message from contact form" 10 }, { :contact => @contact}) 11 render "Thank you. Your message has been sent." 12 else 13 render :contact 14 end 15 else 16 @contact = Contact.new 17 render 18 end 19 endEdit the contact action’s view at views/static/contact.html.erb
1 <h2>Contact</h2> 2 <%= error_messages_for @contact %> 3 <%= form_for @contact, :action => url('contact') do %> 4 <p><%= text_field :name, :label => 'Name' %></p> 5 <p><%= text_field :email, :label => 'Email' %></p> 6 <p><%= text_area :message, :label => 'Message' %></p> 7 <%= submit 'Send' %> 8 <% end =%>Your contact form should now send emails and do validations if items aren’t filled in. We still have to pass the values to the emailed message though so let’s take a look at our mailers/contact_mailer.rb and its prospective view to do this.
Edit contact_mailer.rb
1 class ContactMailer < Merb::MailController 2 3 def notify 4 # use params[] passed to this controller to get data 5 @contact = params[:contact] 6 render_mail 7 end 8 9 endEdit notify.text.erb
1 Name: <%= @contact.name %> 2 3 Email: <%= @contact.email %> 4 5 Message: 6 7 <%= @contact.message %>That’s it. You’re done. You could also customize things a bit here, by creating a global from email in devlopment.rb and production.rb by putting something like ‘SITE_EMAIL = “you@domain.com”’ at the end of those files.
![]()
Spitfire Sky | github | archives | resume