{"id":449,"date":"2013-09-27T13:38:51","date_gmt":"2013-09-27T13:38:51","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=449"},"modified":"2024-09-30T07:56:31","modified_gmt":"2024-09-30T07:56:31","slug":"create-a-mailer-in-rails-4","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/create-a-mailer-in-rails-4\/","title":{"rendered":"Create a mailer in Rails 4"},"content":{"rendered":"<h2>Create a mailer in Rails 4<\/h2>\n<p>Hello party people and polar bears! You were probably wondering how it is you send email using Ruby on Rails. Here&#8217;s how you create an <code>automated email system<\/code> that sends a confirmation email to each new user that registers to your app:<!--more--><\/p>\n<p>1. First off, in <code>bash<\/code>, you must generate your mailer, which uses the Rails generate command (pretty much the same way you generate a <code>controller<\/code> or a <code>model<\/code>). Assuming you&#8217;ve already created your Rails project, to create the mailer you must simply do the following:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 nums:false lang:ruby decode:true\">cd ~\/my_rails_project\r\n\r\nrails generate mailer welcome_mailer\r\n\r\nbundle install<\/pre>\n<p>2. The next thing to do would be to setup an SMTP transport for your mailer. This is done in\u00a0<code>\/config\/initializers\/setup_mail.rb<\/code>. If you don&#8217;t have that file in your Rails project, you should go ahead and create it.<\/p>\n<p>This is how your\u00a0<code>setup_mail.rb<\/code>\u00a0file should look like:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:1 lang:ruby decode:true\" title=\"\/config\/initializers\/setup_mail.rb\">ActionMailer::Base.delivery_method = :smtp\r\nActionMailer::Base.smtp_settings = {\r\n:address =&gt; \"smtp.servername.com\",\r\n:port =&gt; 587,\r\n:domain =&gt; \"whatever.org\",\r\n:user_name =&gt; \"username\",\r\n:password =&gt; \"password\",\r\n:authentication =&gt; \"plain\",\r\n:enable_starttls_auto =&gt; true\r\n}<\/pre>\n<p>3. Then we&#8217;d need to tell Rails the specifics the email you want to send, like who to send it to, where does it come from, as well as the actual Subject and Content.<\/p>\n<p>We do that by creating a method in the recently created mailer, which we&#8217;ll name registration_confirmation:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:1 lang:ruby decode:true\" title=\"\/app\/mailers\/welcome_mailer.rb\">class WelcomeMailer &lt; ActionMailer::Base\r\n\r\ndef registration_confirmation(user)\r\n\r\nmail :to =&gt; user, :from =&gt; \"email@domain.com\", :subject =&gt; \"Subject line\"\r\n\r\nend\r\nend<\/pre>\n<p>4. Assuming you want the email to have some content as well, we can now go ahead and create the HTML and Plain text versions of your email. That&#8217;s done in <code>\/views\/welcome_mailer\/registration_confirmation.html.erb<\/code> and\u00a0<code>\/views\/welcome_mailer\/registration_confirmation.text.erb<\/code> (you&#8217;ll have to create these files):<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 lang:ruby decode:true\">Hello, &lt;%= @user.name %&gt;\r\n\r\n\r\nThank you for registering!<\/pre>\n<p>Now, to actually make use of the Mailer, in the specific action (associated to a controller) you want the mailer used on &#8211; you simply need to add:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 lang:ruby decode:true\">WelcomeMailer.registration_confirmation(@user).deliver<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Create a mailer in Rails 4 Hello party people and polar bears! You were probably wondering how it is you [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84],"tags":[127,166,204,214],"yst_prominent_words":[302,351,426,545,798,1013,1051,1440],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/449"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=449"}],"version-history":[{"count":3,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/449\/revisions"}],"predecessor-version":[{"id":133244,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/449\/revisions\/133244"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=449"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}