{"id":2880,"date":"2018-03-16T01:29:02","date_gmt":"2018-03-15T16:29:02","guid":{"rendered":"http:\/\/taitan916.info\/blog\/?p=2880"},"modified":"2024-02-22T13:09:28","modified_gmt":"2024-02-22T04:09:28","slug":"post-2880","status":"publish","type":"post","link":"https:\/\/taitan916.info\/blog\/archives\/2880","title":{"rendered":"Ruby\u3067\u30b5\u30a4\u30c8\u306e\u6b7b\u6d3b\u7ba1\u7406\u3092\u884c\u3044Gmail\u306eSMTP\u30b5\u30fc\u30d0\u7d4c\u7531\u3067\u901a\u77e5\u30e1\u30fc\u30eb\u3092\u9001\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>Ruby\u306e\u52c9\u5f37\u3067\u4f55\u304b\u30c4\u30fc\u30eb\u3092\u4f5c\u308d\u3046\u3068\u8003\u3048\u305f\u3002\u307e\u305f\u3001Rails\u3068\u304b\u306f\u4f7f\u308f\u305a\u7d20\u306eRuby\u3067\u3001\u5c1a\u4e14\u3064Web\u30da\u30fc\u30b8\u3068\u304b\u306f\u7528\u610f\u305b\u305a\u306b\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u304b\u3089\u306e\u307f\u4f7f\u7528\u30c4\u30fc\u30eb\u3092\u4f5c\u3063\u3066\u307f\u305f\u304b\u3063\u305f\u3002\u30b5\u30a4\u30c8\u306e\u6b7b\u6d3b\u7ba1\u7406\u30c4\u30fc\u30eb\u3068\u304b\u826f\u3055\u3052\u3060\u3068\u601d\u3063\u305f\u306e\u3067\u30e1\u30e2\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u74b0\u5883\u3068\u4f5c\u6210\u3059\u308b\u6a5f\u80fd<\/h2>\n<p>Ruby\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306f2.3.1\u3092\u4f7f\u7528\u3002<\/p>\n<p>\u4f5c\u6210\u3059\u308b\u6a5f\u80fd\u3068\u3057\u3066\u306f\u7279\u5b9a\u306eURL\u306b\u63a5\u7d9a\u3092\u8a66\u3057\u307f\u3066\u30b9\u30c6\u30fc\u30bf\u30b9\u30b3\u30fc\u30c9\u304c200\u4ee5\u5916\u306e\u5834\u5408\u306f\u30e1\u30fc\u30eb\u3092\u9001\u4fe1\u3059\u308b\u3068\u3044\u3046\u3082\u306e\u3002\u307e\u305f\u305d\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306fCron\u3067\u81ea\u52d5\u5b9f\u884c\u3059\u308b\u3068\u60f3\u5b9a\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u30bd\u30fc\u30b9<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">require 'open-uri'\r\nrequire 'mail'\r\n\r\ndef notification\r\n    mail = Mail.new\r\n\r\n    options = {\r\n        :address =&gt; \"smtp.gmail.com\",\r\n        :port =&gt; 587,\r\n        :domain =&gt; \"smtp.gmail.com\",\r\n        :user_name =&gt; '\u3053\u3053\u306bGmail\u30a2\u30c9\u30ec\u30b9',\r\n        :password =&gt; '\u3053\u3053\u306b\u30d1\u30b9\u30ef\u30fc\u30c9',\r\n        :authentication =&gt; :plain,\r\n        :enable_starttls_auto =&gt; true\r\n    }\r\n    mail.charset = 'utf-8'\r\n    mail.from \"\u9001\u308a\u5143\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\" \r\n    mail.to \"\u9001\u308a\u5148\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\" \r\n    mail.subject \"\u30e1\u30fc\u30eb\u30bf\u30a4\u30c8\u30eb\"\r\n    mail.body \"\u30e1\u30fc\u30eb\u672c\u6587\"\r\n    mail.delivery_method(:smtp, options)\r\n    mail.deliver\r\n\r\nend\r\n\r\nuri = \"\u30c1\u30a7\u30c3\u30af\u3057\u305f\u3044\u30da\u30fc\u30b8\u306eURL\"\r\nbegin\r\n    status = open(uri).status\r\n    if status[0].to_i != 200\r\n        notification\r\n    end\r\nrescue\r\n    notification\r\nend<\/pre>\n<p>1\u884c\u76ee\u306fRuby\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u300copen-uri\u300d\u3068\u3044\u3046\u3082\u306e\u3092\u8aad\u307f\u8fbc\u3093\u3067\u3044\u308b\u3002file_get_contents\u7684\u306a\u3082\u306e\u307f\u305f\u3044\u3002\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u306f\u4ee5\u4e0b\u3002<\/p>\n<p><a href=\"https:\/\/docs.ruby-lang.org\/ja\/latest\/library\/open=2duri.html\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.ruby-lang.org\/ja\/latest\/library\/open=2duri.html<\/a><\/p>\n<p>2\u884c\u76ee\u306f\u300cmail\u300d\u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u306a\u308b\u3002\u305d\u306e\u307e\u307e\u3060\u3051\u3069\u30e1\u30fc\u30eb\u9001\u4fe1\u306b\u95a2\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u306a\u308b\u3002\u3053\u3061\u3089\u306e\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u306f\u898b\u3064\u304b\u3089\u305a\u3002<\/p>\n<p>\u3042\u3068\u306fURL\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3066\u30b9\u30c6\u30fc\u30bf\u30b9\u30b3\u30fc\u30c9\u304c200\u3058\u3083\u306a\u3044\u3001\u3082\u3057\u304f\u306f\u30a8\u30e9\u30fc\u767a\u751f\u6642\u306f\u300cnotification\u95a2\u6570\u300d\u3092\u547c\u3093\u3067\u30e1\u30fc\u30eb\u9001\u4fe1\u3059\u308b\u3068\u3044\u3046\u5185\u5bb9\u3002<\/p>\n<p>\u95a2\u6570\u306b\u3064\u3044\u3066\u306f\u5b9f\u884c\u524d\u306b\u8a18\u8ff0\u3057\u3066\u304a\u304b\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3002\u4f5c\u6210\u6642\u306f\u5b9f\u884c\u5f8c\u306b\u66f8\u3044\u3066\u3044\u305f\u306e\u3067\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u305f\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u74b0\u5883\u306b\u3088\u3063\u3066\u306f\u30e1\u30fc\u30eb\u304c\u53d7\u4fe1\u3067\u304d\u306a\u3044<\/h2>\n<p>\u3053\u308c\u306fRuby\u306f\u95a2\u4fc2\u306a\u304f\u30e1\u30fc\u30eb\u30b5\u30fc\u30d0\u306e\u554f\u984c\u3060\u3068\u601d\u308f\u308c\u308b\u304c\u3001\u30e1\u30fc\u30eb\u304c\u53d7\u4fe1\u3067\u304d\u305f\u308a\u51fa\u6765\u306a\u304b\u3063\u305f\u308a\u3057\u305f\u3002\u74b0\u5883\u3068\u3057\u3066\u306fsendmail\u3067\u4f55\u3082\u8a2d\u5b9a\u3092\u884c\u308f\u305a\u306b\u8d77\u52d5\u3057\u305f\u3060\u3051\u3002\u30e1\u30fc\u30eb\u30b5\u30fc\u30d0\u8a2d\u5b9a\u306f\u672a\u3060\u306b\u3088\u304f\u5206\u304b\u3089\u306a\u3044\u306e\u3067\u3001Gmail\u306eSMTP\u30b5\u30fc\u30d0\u3092\u4f7f\u3063\u3066\u30e1\u30fc\u30eb\u9001\u4fe1\u3059\u308b\u65b9\u6cd5\u3092\u7528\u3044\u305f\u3002\u4ee5\u4e0b\u304c\u30bd\u30fc\u30b9\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">require 'open-uri'\r\nrequire 'mail'\r\n\r\ndef notification\r\n    mail = Mail.new\r\n\r\n    options = {\r\n        :address =&gt; \"smtp.gmail.com\",\r\n        :port =&gt; 587,\r\n        :domain =&gt; \"smtp.gmail.com\",\r\n        :user_name =&gt; '\u3053\u3053\u306bGmail\u30a2\u30c9\u30ec\u30b9',\r\n        :password =&gt; '\u3053\u3053\u306b\u30d1\u30b9\u30ef\u30fc\u30c9',\r\n        :authentication =&gt; :plain,\r\n        :enable_starttls_auto =&gt; true\r\n    }\r\n    mail.charset = 'utf-8'\r\n    mail.from \"\u9001\u308a\u5143\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\" \r\n    mail.to \"\u9001\u308a\u5148\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\" \r\n    mail.subject \"\u30e1\u30fc\u30eb\u30bf\u30a4\u30c8\u30eb\"\r\n    mail.body \"\u30e1\u30fc\u30eb\u672c\u6587\"\r\n    mail.delivery_method(:smtp, options)\r\n    mail.deliver\r\n\r\nend\r\n\r\nuri = \"\u30c1\u30a7\u30c3\u30af\u3057\u305f\u3044\u30da\u30fc\u30b8\u306eURL\"\r\nbegin\r\n    status = open(uri).status\r\n    if status[0].to_i != 200\r\n        notification\r\n    end\r\nrescue\r\n    notification\r\nend<\/pre>\n<p>&nbsp;<\/p>\n<h2>Cron\u3067\u5b9f\u884c\u3059\u308b<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">vi \/etc\/crontab\r\n*\/1 * * * * root ruby \/aaa\/bbb\/ccc\/check.rb<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u53c2\u8003<\/h2>\n<p><a href=\"https:\/\/qiita.com\/nyamage\/items\/7145159c9a610f99b5d9\" target=\"_blank\" rel=\"noopener\">https:\/\/qiita.com\/nyamage\/items\/7145159c9a610f99b5d9<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ruby\u306e\u52c9\u5f37\u3067\u4f55\u304b\u30c4\u30fc\u30eb\u3092\u4f5c\u308d\u3046\u3068\u8003\u3048\u305f\u3002\u307e\u305f\u3001Rails\u3068\u304b\u306f\u4f7f\u308f\u305a\u7d20\u306eRu ... <\/p>\n","protected":false},"author":1,"featured_media":1768,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[42],"tags":[],"class_list":["post-2880","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/2880","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/comments?post=2880"}],"version-history":[{"count":2,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/2880\/revisions"}],"predecessor-version":[{"id":4890,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/2880\/revisions\/4890"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media\/1768"}],"wp:attachment":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media?parent=2880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/categories?post=2880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/tags?post=2880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}