{"id":1700,"date":"2008-02-21T20:03:10","date_gmt":"2008-02-21T12:03:10","guid":{"rendered":"http:\/\/ihower.idv.tw\/blog\/archives\/1700"},"modified":"2008-09-29T01:01:21","modified_gmt":"2008-09-28T17:01:21","slug":"rails2-email-%e5%90%8c%e6%99%82%e6%8f%90%e4%be%9b-plain-%e5%8f%8a-html-%e7%89%88%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/1700-rails2-email-%e5%90%8c%e6%99%82%e6%8f%90%e4%be%9b-plain-%e5%8f%8a-html-%e7%89%88%e6%9c%ac","title":{"rendered":"Rails Email \u540c\u6642\u63d0\u4f9b plain \u53ca html \u7248\u672c"},"content":{"rendered":"<p>\u8b93 Email \u540c\u6642\u63d0\u4f9b text\/plain \u8ddf text\/html \u7248\u672c\uff0c\u8b93\u4e0d\u652f\u63f4 HTML \u7684 Email client \u4e5f\u53ef\u4ee5\u986f\u793a plain \u662f\u4e00\u7a2e\u662f\u5177\u6709\u89aa\u548c\u529b\u7684\u4f5c\u6cd5\u3002\u6839\u64da multipart-alternative \u7684\u5b9a\u7fa9\uff0c\u6b63\u78ba\u7684\u683c\u5f0f\u61c9\u8a72\u9577\u9019\u6a23\uff0c\u7528 boundary \u5206\u9694\u6210\u5169\u500b\u90e8\u4efd:<\/p>\n<pre>\r\n   From:  Nathaniel Borenstein &lt;nsb@bellcore.com>\r\n   To: Ned Freed &lt;ned@innosoft.com>\r\n   Subject: Formatted text mail\r\n   MIME-Version: 1.0\r\n   Content-Type: multipart\/alternative; boundary=boundary42\r\n\r\n   --boundary42\r\n\r\n   Content-Type: text\/plain; charset=utf-8\r\n\r\n   ...plain text version of message goes here....\r\n\r\n   --boundary42\r\n   Content-Type: text\/html; charset=utf-8\r\n\r\n   .... html version of same message goes here ...\r\n\r\n   --boundary42--\r\n\r\n<\/pre>\n<p><!--more--><br \/>\n\u4e00\u500b\u65b9\u6cd5\u4f86\u81ea <a href=\"http:\/\/www.pragprog.com\/titles\/fr_rr\">Rails Recipes<\/a>(for Rails 1.1) \u66f8\u4e0a\u7684\u4f5c\u6cd5:<\/p>\n<pre>\r\n\r\n  def signup_notification(user)\r\n    @from        = EXTERNAL_MAIL_SENDER\r\n    @subject     = \"Please activate your new account\"\r\n    @sent_on     = Time.now\r\n    @recipients  = user.email\r\n    @body[:user] = user\r\n\r\n    content_type \"multipart\/alternative\"\r\n    \r\n    part :content_type => \"text\/plain\",\r\n       :body => render_message(\"signup_notification_plain\", @body )\r\n     part :content_type => \"text\/html\",\r\n       :body => render_message(\"signup_notification\", @body )\r\n  end\r\n\r\n<\/pre>\n<p>\u6a94\u6848\u5247\u662f signup_notification_plain.rhtml \u8ddf signup_notification.rhtml<\/p>\n<p>\u4e0d\u904e\u7d93\u904e IRC #elixus \u7684\u670b\u53cb\u53cd\u61c9\uff0c\u5728 mutt \u8ddf thunderbird \u4e0a\u770b\u4e0d\u5230\u6b63\u78ba\u7684 encoding\u3002\u9019\u624d\u4ed4\u7d30\u770b\u4e86 raw email data\uff0c\u7adf\u7136\u51fa\u4f86\u4e86\u4e09\u6bb5 part \u4e14\u7b2c\u4e00\u6bb5\u5c11\u4e86 charset utf-8\u3002\u8a66\u8457\u5e7e\u7a2e\u4e0d\u540c\u5beb\u6cd5\u4ecd\u5f97\u4e0d\u5230\u770b\u8d77\u4f86\u6b63\u78ba\u7684\u683c\u5f0f\uff0cRails Wiki \u4e0a\u7684<a href=\"http:\/\/wiki.rubyonrails.org\/rails\/pages\/HowToSendMimeMultipartEmailsWithActionMailer\">\u4f5c\u6cd5<\/a>\u51fa\u4f86\u7684\u683c\u5f0f\u4e5f\u5f88\u5947\u602a\u3002<\/p>\n<p>\u672c\u4f86\u60f3\u68c4\u5b88 console-based email client \u4e86\uff0c\u53ea\u63d0\u4f9b HTML \u5c31\u597d\uff0c\u4f46\u662f\u5e03\u4e01\u5927\u4eba\u5805\u6301\u8981\u6709 Gracefully Degrading \u554a\u3002\u65bc\u662f\u518d\u53bb\u770b <a href=\"http:\/\/api.rubyonrails.org\/classes\/ActionMailer\/Base.html\">ActionMailer API<\/a> \u767c\u73fe\u5176\u5be6\u6709\u66f4\u7c21\u55ae\u53b2\u5bb3\u7684\u4f5c\u6cd5:<\/p>\n<pre>\r\n  def signup_notification(user)\r\n    @from        = EXTERNAL_MAIL_SENDER\r\n    @subject     = \"Please activate your new account\"\r\n    @sent_on     = Time.now\r\n    @recipients  = user.email\r\n    @body[:user] = user\r\n  end\r\n<\/pre>\n<p>\u6a94\u6848\u5247\u8981\u547d\u540d\u6210 <strong>signup_notification.text.plain.erb<\/strong> \u8ddf <strong>signup_notification.text.html.erb<\/strong>\u3002\u55ef\uff0cRails \u4f9d\u64da\u526f\u6a94\u540d\u6163\u4f8b\u6703\u81ea\u52d5\u5408\u4f75\u6210\u4e00\u5c01\u4fe1\uff0c\u800c\u7121\u9808\u5beb\u660e content_type \u8ddf\u4f7f\u7528 part\u3002<\/p>\n<p>\u6211\u81ea\u5df1\u9084\u6709\u7528<a href=\"http:\/\/www.globalize-rails.org\/globalize\/\">Globalize plugin<\/a>\u63d0\u4f9b\u4e2d\u82f1\u6587\u4e0d\u540c\u7248\u672c\uff0c\u6240\u4ee5\u53c8\u9084\u6709 <strong>signup_notification.zh-TW.text.plain.erb<\/strong> \u53ca <strong>signup_notification.zh-TW.text.html.erb<\/strong>\uff0c\u771f\u662f\u9577\u7684\u526f\u6a94\u540d\u554a :><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8b93 Email \u540c\u6642\u63d0\u4f9b text\/plain \u8ddf text\/html \u7248\u672c\uff0c\u8b93\u4e0d\u652f\u63f4 HTML \u7684 Emai &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/1700-rails2-email-%e5%90%8c%e6%99%82%e6%8f%90%e4%be%9b-plain-%e5%8f%8a-html-%e7%89%88%e6%9c%ac\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Rails Email \u540c\u6642\u63d0\u4f9b plain \u53ca html \u7248\u672c\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,53],"tags":[],"class_list":["post-1700","post","type-post","status-publish","format-standard","hentry","category-programming","category-rails","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-rq","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/comments?post=1700"}],"version-history":[{"count":0,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1700\/revisions"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=1700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=1700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=1700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}