{"id":7941,"date":"2019-12-05T23:47:25","date_gmt":"2019-12-05T14:47:25","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=7941"},"modified":"2023-05-27T22:54:38","modified_gmt":"2023-05-27T13:54:38","slug":"%e3%80%90ruby-on-rails%e3%80%91%e3%80%8caction-mailer%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=7941","title":{"rendered":"\u3010Ruby on Rails\u3011\u300cAction Mailer\u300d\u306b\u3064\u3044\u3066"},"content":{"rendered":"<p>Action Mailer\u306fRails\u3067\u306e\u30e1\u30fc\u30eb\u9001\u4fe1\u6a5f\u80fd\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<\/p>\n<h2>\u30e1\u30a4\u30e9\u30fc\u3092\u4f5c\u308b\u3002<\/h2>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u30e1\u30a4\u30e9\u30fc\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">rails g mailer \u30e1\u30a4\u30e9\u30fc\u540d<\/pre>\n<p>\u3059\u308b\u3068app\/mailers\u306bApplicationMailer\u3092\u7d99\u627f\u3057\u305f\u30e1\u30a4\u30e9\u30fc\u30af\u30e9\u30b9\u304c\u4f5c\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">class \u30e1\u30a4\u30e9\u30fc\u540d &lt; ApplicationMailer\r\nend<\/pre>\n<h2>\u30e1\u30fc\u30eb\u9001\u4fe1\u7528\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f5c\u6210<\/h2>\n<p>\u5f15\u6570\u306e\u30e2\u30c7\u30eb\u540d\u306f\u3001\u7279\u306b\u5fc5\u9808\u3067\u306f\u306a\u304f\u30e2\u30c7\u30eb\u306e\u4e2d\u306b\u542b\u307e\u308c\u308bemail\u30a2\u30c9\u30ec\u30b9\u5b9b\u3092\u8a2d\u5b9a\u3057\u305f\u308a\u30e1\u30fc\u30eb\u672c\u6587\u306b\u53ef\u5909\u7684\u306a\u5185\u5bb9\u3092\u57cb\u3081\u8fbc\u3093\u3060\u308a\u3068\u3044\u3046\u7528\u9014\u7b49\u306b\u4f7f\u308f\u308c\u305f\u308a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">class \u30e1\u30a4\u30e9\u30fc\u540d &lt; ApplicationMailer\r\n  def \u4efb\u610f\u306e\u30e1\u30bd\u30c3\u30c9\u540d(\u30e2\u30c7\u30eb\u540d)\r\n    @\u30e2\u30c7\u30eb\u540d = \u30e2\u30c7\u30eb\u540d\r\n    mail(\r\n      subject: '\u30e1\u30fc\u30eb\u30bf\u30a4\u30c8\u30eb',\r\n      to: '\u5b9b\u5148\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9',\r\n  \u3000  from: '\u9001\u4fe1\u5143\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9'\r\n    )\r\n  end\r\nend<\/pre>\n<p>from\u306f\u3069\u306e\u30e1\u30fc\u30eb\u3082\u540c\u3058\u306b\u3057\u305f\u3044\u3068\u3044\u3046\u3053\u3068\u304c\u666e\u901a\u3060\u3068\u601d\u3044\u307e\u3059\u306e\u3067\u4e0b\u8a18\u306e\u3088\u3046\u306b\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u591a\u3044\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">class TaskMailer &lt; ApplicationMailer\r\n  default from: '\u9001\u4fe1\u5143\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9'\r\n  \u2025\r\nend<\/pre>\n<h2>\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3002<\/h2>\n<p>\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u306f2\u7a2e\u985e\u3042\u308a\u307e\u3059\u3002\u3069\u3061\u3089\u304b\u306e\u5f62\u5f0f\u3092\u6307\u5b9a\u3057\u3066\u30e1\u30fc\u30eb\u9001\u4fe1\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u304c\u3001\u7279\u306b\u5f62\u5f0f\u3092\u6307\u5b9a\u3057\u306a\u304b\u3063\u305f\u5834\u5408\u306f<span style=\"color: #ff0000;\"><strong>\u30de\u30eb\u30c1\u30d1\u30fc\u30c8(\u57fa\u672c\u306fHTML\u3067\u3001\u74b0\u5883\u7684\u306b\u7121\u7406\u3067\u3042\u308c\u3070text\u5f62\u5f0f)<\/strong><\/span>\u306e\u30e1\u30fc\u30eb\u9001\u4fe1\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30c6\u30ad\u30b9\u30c8\u5f62\u5f0f<\/li>\n<li>HTML\u5f62\u5f0f<\/li>\n<\/ul>\n<h3>\u30c6\u30ad\u30b9\u30c8\u5f62\u5f0f<\/h3>\n<p>\u4e0b\u8a18\u306e\u5834\u6240\u306b\u30d5\u30a1\u30a4\u30eb\u3092\u65b0\u898f\u4f5c\u6210\u3057\u3066<\/p>\n<pre class=\"lang:default decode:true\">app\/views\/\u30e1\u30a4\u30e9\u30fc\u30af\u30e9\u30b9\u306b\u5b9a\u7fa9\u3057\u305f\u4efb\u610f\u306e\u30e1\u30bd\u30c3\u30c9\u540d.text.slim<\/pre>\n<h3>HTML\u5f62\u5f0f<\/h3>\n<pre class=\"lang:default decode:true \">app\/views\/\u30e1\u30a4\u30e9\u30fc\u30af\u30e9\u30b9\u306b\u5b9a\u7fa9\u3057\u305f\u4efb\u610f\u306e\u30e1\u30bd\u30c3\u30c9\u540d.html.slim<\/pre>\n<h2>\u30e1\u30fc\u30eb\u9001\u4fe1\u51e6\u7406\u3092\u8a18\u8ff0\u3059\u308b\u3002(\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306b\u3066\uff09<\/h2>\n<p>\u4e0b\u8a18\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">\u30e1\u30fc\u30e9\u30fc\u540d.\u30e1\u30bd\u30c3\u30c9(@\u30e2\u30c7\u30eb).deliver_now<\/pre>\n<h3>deliver_now<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>\u5373\u6642\u9001\u4fe1<\/strong><\/span>\u3059\u308b\u30e1\u30bd\u30c3\u30c9<\/p>\n<h3>deliver_later<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>Active Job\u3068\u9023\u643a\u3057\u3066\u975e\u540c\u671f\u306b\u30e1\u30fc\u30eb\u9001\u4fe1\u3092\u884c\u3048\u308b\u30e1\u30bd\u30c3\u30c9<\/strong><\/span>\u3067\u3059\u3002\u4f8b\u3048\u3070\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308c\u30703\u5206\u5f8c\u306b\u30e1\u30fc\u30eb\u3092\u9001\u4fe1\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">\u30e1\u30fc\u30e9\u30fc\u540d.\u30e1\u30bd\u30c3\u30c9(@\u30e2\u30c7\u30eb).deliver_later(wait: 3.minutes)<\/pre>\n<h2>\u52d5\u4f5c\u78ba\u8a8d<\/h2>\n<p>\u307e\u305a\u306fmailcatcher(SMTP\u30b5\u30fc\u30d0\u3092\u7acb\u3066\u3066\u9001\u4fe1\u3055\u308c\u305f\u30e1\u30fc\u30eb\u3092\u30d6\u30e9\u30a6\u30b6\u3067\u78ba\u8a8d\u3067\u304d\u308b\u6a5f\u80fd\uff09\u3068\u3044\u3046gem\u304c\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">gem install mailcatcher<\/pre>\n<p>\u6b21\u306bdevelopment\u74b0\u5883\u5411\u3051\u306e\u8a2d\u5b9a\u3092\u884c\u3044\u307e\u3059\u3002config\/environments\/production.rb\u306b\u4e0b\u8a18\u8a2d\u5b9a\u3092\u8ffd\u8a18\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">  # Don't care if the mailer can't send.\r\n  config.action_mailer.raise_delivery_errors = false\r\n  config.action_mailer.delivery_method = :smtp\u3000\u2605\u8ffd\u52a0\r\n  config.action_mailer.smtp_settings = { address: '127.0.0.1', port: 1025}\u3000\u2605\u8ffd\u52a0<\/pre>\n<p>Rails\u306e\u30b5\u30fc\u30d0\u30fc\u3092\u518d\u8d77\u52d5\u3057\u3001\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066mailcatcher\u3092\u8d77\u52d5\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">mailcatcher<\/pre>\n<p>\u30e1\u30fc\u30eb\u9001\u4fe1\u6a5f\u80fd\u3092\u5b9f\u969b\u306bRails\u3067\u52d5\u304b\u3057\u305f\u5f8c\u300chttp:\/\/127.0.0.1:1080\u300d\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u4e0b\u8a18\u306e\u3088\u3046\u306bhtml\u5f62\u5f0f\u3068text\u5f62\u5f0f\u53cc\u65b9\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u304c\u60f3\u5b9a\u3069\u304a\u308a\u9001\u4fe1\u3055\u308c\u3066\u3044\u308b\u304b\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\uff08\u4eee\u306b\u5b58\u5728\u3057\u3066\u3044\u306a\u3044\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u5b9b\u3067\u3082\u9001\u4fe1\u304c\u53ef\u80fd\u3067\u3059\u3002\uff09<\/strong><\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-7960\" src=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2019\/12\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2019-12-07-7.45.03.png\" alt=\"\" width=\"1272\" height=\"852\" srcset=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2019\/12\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2019-12-07-7.45.03.png 1272w, http:\/\/www.code-magagine.com\/wp-content\/uploads\/2019\/12\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2019-12-07-7.45.03-300x201.png 300w, http:\/\/www.code-magagine.com\/wp-content\/uploads\/2019\/12\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2019-12-07-7.45.03-768x514.png 768w, http:\/\/www.code-magagine.com\/wp-content\/uploads\/2019\/12\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2019-12-07-7.45.03-1024x686.png 1024w\" sizes=\"(max-width: 1272px) 100vw, 1272px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"Action Mailer\u306fRails\u3067\u306e\u30e1\u30fc\u30eb\u9001\u4fe1\u6a5f\u80fd\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002 \u30e1\u30a4\u30e9\u30fc\u3092\u4f5c\u308b\u3002 \u4e0b\u8a18\u306e\u3088\u3046\u306b\u30e1\u30a4\u30e9\u30fc\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002 rails g mailer \u30e1\u30a4\u30e9\u30fc\u540d \u3059\u308b\u3068app\/ma [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[36],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7941"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7941"}],"version-history":[{"count":20,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7941\/revisions"}],"predecessor-version":[{"id":20530,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7941\/revisions\/20530"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7941"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}