-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoloading_and_reloading_constants.html
987 lines (913 loc) · 48.1 KB
/
autoloading_and_reloading_constants.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Action Mailer Basics — Ruby on Rails 指南</title>
<meta name="description" content="Ruby on Rails 指南:系統學習 Rails(Rails 4.2 版本)" >
<meta name="keywords" content="Ruby on Rails Guides 指南 中文 學習 免費 網路 Web 開發" >
<meta name="author" content="http://git.io/G_R1sA">
<meta property="fb:admins" content="1340181291">
<meta property="og:title" content="Action Mailer Basics — Ruby on Rails 指南" >
<meta property="og:site_name" content="Ruby on Rails 指南">
<meta property="og:image" content="http://rails.ruby.tw/images/rails_guides_cover.jpg">
<meta property="og:url" content="http://rails.ruby.tw/">
<meta property="og:type" content="article">
<meta property="og:description" content="Ruby on Rails 指南:系統學習 Rails(Rails 4.2 版本)">
<link rel="stylesheet" href="stylesheets/application.css">
<link href="http://fonts.googleapis.com/css?family=Noto+Sans:400,700|Noto+Serif:700|Source+Code+Pro" rel="stylesheet">
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon">
</head>
<body class="guide">
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/zh-TW/sdk.js#xfbml=1&appId=837401439623727&version=v2.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<script type="text/javascript">
window.twttr=(function(d,s,id){var t,js,fjs=d.getElementsByTagName(s)[0];if(d.getElementById(id)){return}js=d.createElement(s);js.id=id;js.src="https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);return window.twttr||(t={_e:[],ready:function(f){t._e.push(f)}})}(document,"script","twitter-wjs"));
</script>
<div id="topNav">
<div class="wrapper">
<strong class="more-info-label">更多內容 <a href="http://rubyonrails.org/">rubyonrails.org:</a></strong>
<span class="red-button more-info-button">
更多內容
</span>
<ul class="more-info-links s-hidden">
<li class="more-info"><a href="http://rubyonrails.org/">綜覽</a></li>
<li class="more-info"><a href="http://rubyonrails.org/download">下載</a></li>
<li class="more-info"><a href="http://rubyonrails.org/deploy">部署</a></li>
<li class="more-info"><a href="https://github.com/rails/rails">原始碼</a></li>
<li class="more-info"><a href="http://rubyonrails.org/screencasts">影片</a></li>
<li class="more-info"><a href="http://rubyonrails.org/documentation">文件</a></li>
<li class="more-info"><a href="http://rubyonrails.org/community">社群</a></li>
<li class="more-info"><a href="http://weblog.rubyonrails.org/">Blog</a></li>
</ul>
</div>
</div>
<div id="header">
<div class="wrapper clearfix">
<h1><a href="index.html" title="回首頁">Guides.rubyonrails.org</a></h1>
<ul class="nav">
<li><a class="nav-item" href="index.html">首頁</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">指南目錄</a>
<div id="guides" class="clearfix" style="display: none;">
<hr>
<dl class="L">
<dt>起步走</dt>
<dd><a href="getting_started.html">Rails 起步走</a></dd>
<dt>Models</dt>
<dd><a href="active_record_basics.html">Active Record 基礎</a></dd>
<dd><a href="active_record_migrations.html">Active Record 遷移</a></dd>
<dd><a href="active_record_validations.html">Active Record 驗證</a></dd>
<dd><a href="active_record_callbacks.html">Active Record 回呼</a></dd>
<dd><a href="association_basics.html">Active Record 關聯</a></dd>
<dd><a href="active_record_querying.html">Active Record 查詢</a></dd>
<dt>Views</dt>
<dd><a href="layouts_and_rendering.html">Rails 算繪與版型</a></dd>
<dd><a href="form_helpers.html">Action View 表單輔助方法</a></dd>
<dt>Controllers</dt>
<dd><a href="action_controller_overview.html">Action Controller 綜覽</a></dd>
<dd><a href="routing.html">Rails 路由:深入淺出</a></dd>
</dl>
<dl class="R">
<dt>深入了解</dt>
<dd><a href="active_support_core_extensions.html">Active Support 核心擴展</a></dd>
<dd><a href="i18n.html">Rails 國際化 API</a></dd>
<dd><a href="action_mailer_basics.html">Action Mailer 基礎</a></dd>
<dd><a href="active_job_basics.html">Active Job 基礎</a></dd>
<dd><a href="security.html">Rails 安全指南</a></dd>
<dd><a href="debugging_rails_applications.html">除錯 Rails 應用程式</a></dd>
<dd><a href="configuring.html">Rails 應用程式設定</a></dd>
<dd><a href="command_line.html">Rake 任務與 Rails 命令列工具</a></dd>
<dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
<dd><a href="working_with_javascript_in_rails.html">在 Rails 使用 JavaScript</a></dd>
<dd><a href="constant_autoloading_and_reloading.html">Constant Autoloading and Reloading</a></dd>
<dt>擴充 Rails</dt>
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
<dd><a href="generators.html">客製與新建 Rails 產生器</a></dd>
<dd><a href="rails_application_templates.html">Rails 應用程式模版</a></dd>
<dt>貢獻 Ruby on Rails</dt>
<dd><a href="contributing_to_ruby_on_rails.html">貢獻 Ruby on Rails</a></dd>
<dd><a href="api_documentation_guidelines.html">API 文件準則</a></dd>
<dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南準則</a></dd>
<dt>維護方針</dt>
<dd><a href="maintenance_policy.html">維護方針</a></dd>
<dt>發佈記</dt>
<dd><a href="upgrading_ruby_on_rails.html">升級 Ruby on Rails</a></dd>
<dd><a href="4_2_release_notes.html">Ruby on Rails 4.2 發佈記</a></dd>
<dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 發佈記</a></dd>
<dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 發佈記</a></dd>
<dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 發佈記</a></dd>
<dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 發佈記</a></dd>
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 發佈記</a></dd>
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 發佈記</a></dd>
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 發佈記</a></dd>
<dt>Rails 指南翻譯術語</dt>
<dd><a href="translation_terms.html">翻譯術語</a></dd>
</dl>
</div>
</li>
<li><a class="nav-item" href="//github.com/docrails-tw/guides">貢獻翻譯</a></li>
<li><a class="nav-item" href="contributing_to_ruby_on_rails.html">貢獻</a></li>
<li><a class="nav-item" href="credits.html">致謝</a></li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">指南目錄</option>
<optgroup label="起步走">
<option value="getting_started.html">Rails 起步走</option>
</optgroup>
<optgroup label="Models">
<option value="active_record_basics.html">Active Record 基礎</option>
<option value="active_record_migrations.html">Active Record 遷移</option>
<option value="active_record_validations.html">Active Record 驗證</option>
<option value="active_record_callbacks.html">Active Record 回呼</option>
<option value="association_basics.html">Active Record 關聯</option>
<option value="active_record_querying.html">Active Record 查詢</option>
</optgroup>
<optgroup label="Views">
<option value="layouts_and_rendering.html">Rails 算繪與版型</option>
<option value="form_helpers.html">Action View 表單輔助方法</option>
</optgroup>
<optgroup label="Controllers">
<option value="action_controller_overview.html">Action Controller 綜覽</option>
<option value="routing.html">Rails 路由:深入淺出</option>
</optgroup>
<optgroup label="深入了解">
<option value="active_support_core_extensions.html">Active Support 核心擴展</option>
<option value="i18n.html">Rails 國際化 API</option>
<option value="action_mailer_basics.html">Action Mailer 基礎</option>
<option value="active_job_basics.html">Active Job 基礎</option>
<option value="security.html">Rails 安全指南</option>
<option value="debugging_rails_applications.html">除錯 Rails 應用程式</option>
<option value="configuring.html">Rails 應用程式設定</option>
<option value="command_line.html">Rake 任務與 Rails 命令列工具</option>
<option value="asset_pipeline.html">Asset Pipeline</option>
<option value="working_with_javascript_in_rails.html">在 Rails 使用 JavaScript</option>
<option value="constant_autoloading_and_reloading.html">Constant Autoloading and Reloading</option>
</optgroup>
<optgroup label="擴充 Rails">
<option value="rails_on_rack.html">Rails on Rack</option>
<option value="generators.html">客製與新建 Rails 產生器</option>
<option value="rails_application_templates.html">Rails 應用程式模版</option>
</optgroup>
<optgroup label="貢獻 Ruby on Rails">
<option value="contributing_to_ruby_on_rails.html">貢獻 Ruby on Rails</option>
<option value="api_documentation_guidelines.html">API 文件準則</option>
<option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南準則</option>
</optgroup>
<optgroup label="維護方針">
<option value="maintenance_policy.html">維護方針</option>
</optgroup>
<optgroup label="發佈記">
<option value="upgrading_ruby_on_rails.html">升級 Ruby on Rails</option>
<option value="4_2_release_notes.html">Ruby on Rails 4.2 發佈記</option>
<option value="4_1_release_notes.html">Ruby on Rails 4.1 發佈記</option>
<option value="4_0_release_notes.html">Ruby on Rails 4.0 發佈記</option>
<option value="3_2_release_notes.html">Ruby on Rails 3.2 發佈記</option>
<option value="3_1_release_notes.html">Ruby on Rails 3.1 發佈記</option>
<option value="3_0_release_notes.html">Ruby on Rails 3.0 發佈記</option>
<option value="2_3_release_notes.html">Ruby on Rails 2.3 發佈記</option>
<option value="2_2_release_notes.html">Ruby on Rails 2.2 發佈記</option>
</optgroup>
<optgroup label="Rails 指南翻譯術語">
<option value="translation_terms.html">翻譯術語</option>
</optgroup>
</select>
</li>
</ul>
</div>
</div>
</div>
<hr class="hide">
<div id="feature">
<div class="wrapper">
<h2>Action Mailer Basics</h2><p>This guide provides you with all you need to get started in sending and
receiving emails from and to your application, and many internals of Action
Mailer. It also covers how to test your mailers.</p><p>After reading this guide, you will know:</p>
<ul>
<li>How to send and receive email within a Rails application.</li>
<li>How to generate and edit an Action Mailer class and mailer view.</li>
<li>How to configure Action Mailer for your environment.</li>
<li>How to test your Action Mailer classes.</li>
</ul>
<div id="subCol">
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
<ol class="chapters">
<li><a href="#introduction">Introduction</a></li>
<li>
<a href="#sending-emails">Sending Emails</a>
<ul>
<li><a href="#walkthrough-to-generating-a-mailer">Walkthrough to Generating a Mailer</a></li>
<li><a href="#auto-encoding-header-values">Auto encoding header values</a></li>
<li><a href="#complete-list-of-action-mailer-methods">Complete List of Action Mailer Methods</a></li>
<li><a href="#mailer-views">Mailer Views</a></li>
<li><a href="#action-mailer-layouts">Action Mailer Layouts</a></li>
<li><a href="#generating-urls-in-action-mailer-views">Generating URLs in Action Mailer Views</a></li>
<li><a href="#sending-multipart-emails">Sending Multipart Emails</a></li>
<li><a href="#sending-emails-with-dynamic-delivery-options">Sending Emails with Dynamic Delivery Options</a></li>
<li><a href="#sending-emails-without-template-rendering">Sending Emails without Template Rendering</a></li>
</ul>
</li>
<li><a href="#receiving-emails">Receiving Emails</a></li>
<li><a href="#action-mailer-callbacks">Action Mailer Callbacks</a></li>
<li><a href="#using-action-mailer-helpers">Using Action Mailer Helpers</a></li>
<li>
<a href="#action-mailer-configuration">Action Mailer Configuration</a>
<ul>
<li><a href="#example-action-mailer-configuration">Example Action Mailer Configuration</a></li>
<li><a href="#action-mailer-configuration-for-gmail">Action Mailer Configuration for Gmail</a></li>
</ul>
</li>
<li><a href="#mailer-testing">Mailer Testing</a></li>
<li><a href="#intercepting-emails">Intercepting Emails</a></li>
</ol>
</div>
</div>
</div>
<div id="container">
<div class="wrapper">
<div id="mainCol">
<h3 id="introduction">1 Introduction</h3><p>Action Mailer allows you to send emails from your application using mailer classes
and views. Mailers work very similarly to controllers. They inherit from
<code>ActionMailer::Base</code> and live in <code>app/mailers</code>, and they have associated views
that appear in <code>app/views</code>.</p><h3 id="sending-emails">2 Sending Emails</h3><p>This section will provide a step-by-step guide to creating a mailer and its
views.</p><h4 id="walkthrough-to-generating-a-mailer">2.1 Walkthrough to Generating a Mailer</h4><h5 id="create-the-mailer">2.1.1 Create the Mailer</h5><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ bin/rails generate mailer UserMailer
create app/mailers/user_mailer.rb
create app/mailers/application_mailer.rb
invoke erb
create app/views/user_mailer
create app/views/layouts/mailer.text.erb
create app/views/layouts/mailer.html.erb
invoke test_unit
create test/mailers/user_mailer_test.rb
create test/mailers/previews/user_mailer_preview.rb
</pre>
</div>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "[email protected]"
layout 'mailer'
end
# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
end
</pre>
</div>
<p>As you can see, you can generate mailers just like you use other generators with
Rails. Mailers are conceptually similar to controllers, and so we get a mailer,
a directory for views, and a test.</p><p>If you didn't want to use a generator, you could create your own file inside of
app/mailers, just make sure that it inherits from <code>ActionMailer::Base</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class MyMailer < ActionMailer::Base
end
</pre>
</div>
<h5 id="edit-the-mailer">2.1.2 Edit the Mailer</h5><p>Mailers are very similar to Rails controllers. They also have methods called
"actions" and use views to structure the content. Where a controller generates
content like HTML to send back to the client, a Mailer creates a message to be
delivered via email.</p><p><code>app/mailers/user_mailer.rb</code> contains an empty mailer:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class UserMailer < ApplicationMailer
end
</pre>
</div>
<p>Let's add a method called <code>welcome_email</code>, that will send an email to the user's
registered email address:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class UserMailer < ApplicationMailer
default from: '[email protected]'
def welcome_email(user)
@user = user
@url = 'http://example.com/login'
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
end
</pre>
</div>
<p>Here is a quick explanation of the items presented in the preceding method. For
a full list of all available options, please have a look further down at the
Complete List of Action Mailer user-settable attributes section.</p>
<ul>
<li>
<code>default Hash</code> - This is a hash of default values for any email you send from
this mailer. In this case we are setting the <code>:from</code> header to a value for all
messages in this class. This can be overridden on a per-email basis.</li>
<li>
<code>mail</code> - The actual email message, we are passing the <code>:to</code> and <code>:subject</code>
headers in.</li>
</ul>
<p>Just like controllers, any instance variables we define in the method become
available for use in the views.</p><h5 id="create-a-mailer-view">2.1.3 Create a Mailer View</h5><p>Create a file called <code>welcome_email.html.erb</code> in <code>app/views/user_mailer/</code>. This
will be the template used for the email, formatted in HTML:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, <%= @user.name %></h1>
<p>
You have successfully signed up to example.com,
your username is: <%= @user.login %>.<br>
</p>
<p>
To login to the site, just follow this link: <%= @url %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
</pre>
</div>
<p>Let's also make a text part for this email. Not all clients prefer HTML emails,
and so sending both is best practice. To do this, create a file called
<code>welcome_email.text.erb</code> in <code>app/views/user_mailer/</code>:</p><div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
Welcome to example.com, <%= @user.name %>
===============================================
You have successfully signed up to example.com,
your username is: <%= @user.login %>.
To login to the site, just follow this link: <%= @url %>.
Thanks for joining and have a great day!
</pre>
</div>
<p>When you call the <code>mail</code> method now, Action Mailer will detect the two templates
(text and HTML) and automatically generate a <code>multipart/alternative</code> email.</p><h5 id="calling-the-mailer">2.1.4 Calling the Mailer</h5><p>Mailers are really just another way to render a view. Instead of rendering a
view and sending out the HTTP protocol, they are just sending it out through the
email protocols instead. Due to this, it makes sense to just have your
controller tell the Mailer to send an email when a user is successfully created.</p><p>Setting this up is painfully simple.</p><p>First, let's create a simple <code>User</code> scaffold:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ bin/rails generate scaffold user name email login
$ bin/rake db:migrate
</pre>
</div>
<p>Now that we have a user model to play with, we will just edit the
<code>app/controllers/users_controller.rb</code> make it instruct the <code>UserMailer</code> to deliver
an email to the newly created user by editing the create action and inserting a
call to <code>UserMailer.welcome_email</code> right after the user is successfully saved.</p><p>Action Mailer is nicely integrated with Active Job so you can send emails outside
of the request-response cycle, so the user doesn't have to wait on it:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class UsersController < ApplicationController
# POST /users
# POST /users.json
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
# Tell the UserMailer to send a welcome email after save
UserMailer.welcome_email(@user).deliver_later
format.html { redirect_to(@user, notice: 'User was successfully created.') }
format.json { render json: @user, status: :created, location: @user }
else
format.html { render action: 'new' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
end
</pre>
</div>
<div class="note"><p>Active Job's default behavior is to execute jobs ':inline'. So, you can use
<code>deliver_later</code> now to send emails, and when you later decide to start sending
them from a background job, you'll only need to set up Active Job to use a queueing
backend (Sidekiq, Resque, etc).</p></div><p>If you want to send emails right away (from a cronjob for example) just call
<code>deliver_now</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class SendWeeklySummary
def run
User.find_each do |user|
UserMailer.weekly_summary(user).deliver_now
end
end
end
</pre>
</div>
<p>The method <code>welcome_email</code> returns a <code>ActionMailer::MessageDelivery</code> object which
can then just be told <code>deliver_now</code> or <code>deliver_later</code> to send itself out. The
<code>ActionMailer::MessageDelivery</code> object is just a wrapper around a <code>Mail::Message</code>. If
you want to inspect, alter or do anything else with the <code>Mail::Message</code> object you can
access it with the <code>message</code> method on the <code>ActionMailer::MessageDelivery</code> object.</p><h4 id="auto-encoding-header-values">2.2 Auto encoding header values</h4><p>Action Mailer handles the auto encoding of multibyte characters inside of
headers and bodies.</p><p>For more complex examples such as defining alternate character sets or
self-encoding text first, please refer to the
<a href="https://github.com/mikel/mail">Mail</a> library.</p><h4 id="complete-list-of-action-mailer-methods">2.3 Complete List of Action Mailer Methods</h4><p>There are just three methods that you need to send pretty much any email
message:</p>
<ul>
<li>
<code>headers</code> - Specifies any header on the email you want. You can pass a hash of
header field names and value pairs, or you can call <code>headers[:field_name] =
'value'</code>.</li>
<li>
<code>attachments</code> - Allows you to add attachments to your email. For example,
<code>attachments['file-name.jpg'] = File.read('file-name.jpg')</code>.</li>
<li>
<code>mail</code> - Sends the actual email itself. You can pass in headers as a hash to
the mail method as a parameter, mail will then create an email, either plain
text, or multipart, depending on what email templates you have defined.</li>
</ul>
<h5 id="adding-attachments">2.3.1 Adding Attachments</h5><p>Action Mailer makes it very easy to add attachments.</p>
<ul>
<li>
<p>Pass the file name and content and Action Mailer and the
<a href="https://github.com/mikel/mail">Mail gem</a> will automatically guess the
mime_type, set the encoding and create the attachment.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
</pre>
</div>
</li>
</ul>
<p> When the <code>mail</code> method will be triggered, it will send a multipart email with
an attachment, properly nested with the top level being <code>multipart/mixed</code> and
the first part being a <code>multipart/alternative</code> containing the plain text and
HTML email messages.</p><div class="note"><p>Mail will automatically Base64 encode an attachment. If you want something
different, encode your content and pass in the encoded content and encoding in a
<code>Hash</code> to the <code>attachments</code> method.</p></div>
<ul>
<li>
<p>Pass the file name and specify headers and content and Action Mailer and Mail
will use the settings you pass in.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
encoded_content = SpecialEncode(File.read('/path/to/filename.jpg'))
attachments['filename.jpg'] = {
mime_type: 'application/x-gzip',
encoding: 'SpecialEncoding',
content: encoded_content
}
</pre>
</div>
</li>
</ul>
<div class="note"><p>If you specify an encoding, Mail will assume that your content is already
encoded and not try to Base64 encode it.</p></div><h5 id="making-inline-attachments">2.3.2 Making Inline Attachments</h5><p>Action Mailer 3.0 makes inline attachments, which involved a lot of hacking in pre 3.0 versions, much simpler and trivial as they should be.</p>
<ul>
<li>
<p>First, to tell Mail to turn an attachment into an inline attachment, you just call <code>#inline</code> on the attachments method within your Mailer:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def welcome
attachments.inline['image.jpg'] = File.read('/path/to/image.jpg')
end
</pre>
</div>
</li>
<li>
<p>Then in your view, you can just reference <code>attachments</code> as a hash and specify
which attachment you want to show, calling <code>url</code> on it and then passing the
result into the <code>image_tag</code> method:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
<p>Hello there, this is our image</p>
<%= image_tag attachments['image.jpg'].url %>
</pre>
</div>
</li>
<li>
<p>As this is a standard call to <code>image_tag</code> you can pass in an options hash
after the attachment URL as you could for any other image:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
<p>Hello there, this is our image</p>
<%= image_tag attachments['image.jpg'].url, alt: 'My Photo', class: 'photos' %>
</pre>
</div>
</li>
</ul>
<h5 id="sending-email-to-multiple-recipients">2.3.3 Sending Email To Multiple Recipients</h5><p>It is possible to send email to one or more recipients in one email (e.g.,
informing all admins of a new signup) by setting the list of emails to the <code>:to</code>
key. The list of emails can be an array of email addresses or a single string
with the addresses separated by commas.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class AdminMailer < ActionMailer::Base
default to: Proc.new { Admin.pluck(:email) },
from: '[email protected]'
def new_registration(user)
@user = user
mail(subject: "New User Signup: #{@user.email}")
end
end
</pre>
</div>
<p>The same format can be used to set carbon copy (Cc:) and blind carbon copy
(Bcc:) recipients, by using the <code>:cc</code> and <code>:bcc</code> keys respectively.</p><h5 id="sending-email-with-name">2.3.4 Sending Email With Name</h5><p>Sometimes you wish to show the name of the person instead of just their email
address when they receive the email. The trick to doing that is to format the
email address in the format <code>"Full Name <email>"</code>.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def welcome_email(user)
@user = user
email_with_name = %("#{@user.name}" <#{@user.email}>)
mail(to: email_with_name, subject: 'Welcome to My Awesome Site')
end
</pre>
</div>
<h4 id="mailer-views">2.4 Mailer Views</h4><p>Mailer views are located in the <code>app/views/name_of_mailer_class</code> directory. The
specific mailer view is known to the class because its name is the same as the
mailer method. In our example from above, our mailer view for the
<code>welcome_email</code> method will be in <code>app/views/user_mailer/welcome_email.html.erb</code>
for the HTML version and <code>welcome_email.text.erb</code> for the plain text version.</p><p>To change the default mailer view for your action you do something like:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class UserMailer < ApplicationMailer
default from: '[email protected]'
def welcome_email(user)
@user = user
@url = 'http://example.com/login'
mail(to: @user.email,
subject: 'Welcome to My Awesome Site',
template_path: 'notifications',
template_name: 'another')
end
end
</pre>
</div>
<p>In this case it will look for templates at <code>app/views/notifications</code> with name
<code>another</code>. You can also specify an array of paths for <code>template_path</code>, and they
will be searched in order.</p><p>If you want more flexibility you can also pass a block and render specific
templates or even render inline or text without using a template file:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class UserMailer < ApplicationMailer
default from: '[email protected]'
def welcome_email(user)
@user = user
@url = 'http://example.com/login'
mail(to: @user.email,
subject: 'Welcome to My Awesome Site') do |format|
format.html { render 'another_template' }
format.text { render text: 'Render text' }
end
end
end
</pre>
</div>
<p>This will render the template 'another_template.html.erb' for the HTML part and
use the rendered text for the text part. The render command is the same one used
inside of Action Controller, so you can use all the same options, such as
<code>:text</code>, <code>:inline</code> etc.</p><h4 id="action-mailer-layouts">2.5 Action Mailer Layouts</h4><p>Just like controller views, you can also have mailer layouts. The layout name
needs to be the same as your mailer, such as <code>user_mailer.html.erb</code> and
<code>user_mailer.text.erb</code> to be automatically recognized by your mailer as a
layout.</p><p>In order to use a different file, call <code>layout</code> in your mailer:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class UserMailer < ApplicationMailer
layout 'awesome' # use awesome.(html|text).erb as the layout
end
</pre>
</div>
<p>Just like with controller views, use <code>yield</code> to render the view inside the
layout.</p><p>You can also pass in a <code>layout: 'layout_name'</code> option to the render call inside
the format block to specify different layouts for different formats:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class UserMailer < ApplicationMailer
def welcome_email(user)
mail(to: user.email) do |format|
format.html { render layout: 'my_layout' }
format.text
end
end
end
</pre>
</div>
<p>Will render the HTML part using the <code>my_layout.html.erb</code> file and the text part
with the usual <code>user_mailer.text.erb</code> file if it exists.</p><h4 id="generating-urls-in-action-mailer-views">2.6 Generating URLs in Action Mailer Views</h4><p>Unlike controllers, the mailer instance doesn't have any context about the
incoming request so you'll need to provide the <code>:host</code> parameter yourself.</p><p>As the <code>:host</code> usually is consistent across the application you can configure it
globally in <code>config/application.rb</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.action_mailer.default_url_options = { host: 'example.com' }
</pre>
</div>
<p>Because of this behavior you cannot use any of the <code>*_path</code> helpers inside of
an email. Instead you will need to use the associated <code>*_url</code> helper. For example
instead of using</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
<%= link_to 'welcome', welcome_path %>
</pre>
</div>
<p>You will need to use:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
<%= link_to 'welcome', welcome_url %>
</pre>
</div>
<p>By using the full URL, your links will now work in your emails.</p><h5 id="generating-urls-with-url-for">2.6.1 generating URLs with <code>url_for</code>
</h5><p><code>url_for</code> generate full URL by default in templates.</p><p>If you did not configure the <code>:host</code> option globally make sure to pass it to
<code>url_for</code>.</p><div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
<%= url_for(host: 'example.com',
controller: 'welcome',
action: 'greeting') %>
</pre>
</div>
<h5 id="generating-urls-with-named-routes">2.6.2 generating URLs with named routes</h5><p>Email clients have no web context and so paths have no base URL to form complete
web addresses. Thus, you should always use the "_url" variant of named route
helpers.</p><p>If you did not configure the <code>:host</code> option globally make sure to pass it to the
url helper.</p><div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
<%= user_url(@user, host: 'example.com') %>
</pre>
</div>
<h4 id="sending-multipart-emails">2.7 Sending Multipart Emails</h4><p>Action Mailer will automatically send multipart emails if you have different
templates for the same action. So, for our UserMailer example, if you have
<code>welcome_email.text.erb</code> and <code>welcome_email.html.erb</code> in
<code>app/views/user_mailer</code>, Action Mailer will automatically send a multipart email
with the HTML and text versions setup as different parts.</p><p>The order of the parts getting inserted is determined by the <code>:parts_order</code>
inside of the <code>ActionMailer::Base.default</code> method.</p><h4 id="sending-emails-with-dynamic-delivery-options">2.8 Sending Emails with Dynamic Delivery Options</h4><p>If you wish to override the default delivery options (e.g. SMTP credentials)
while delivering emails, you can do this using <code>delivery_method_options</code> in the
mailer action.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class UserMailer < ApplicationMailer
def welcome_email(user, company)
@user = user
@url = user_url(@user)
delivery_options = { user_name: company.smtp_user,
password: company.smtp_password,
address: company.smtp_host }
mail(to: @user.email,
subject: "Please see the Terms and Conditions attached",
delivery_method_options: delivery_options)
end
end
</pre>
</div>
<h4 id="sending-emails-without-template-rendering">2.9 Sending Emails without Template Rendering</h4><p>There may be cases in which you want to skip the template rendering step and
supply the email body as a string. You can achieve this using the <code>:body</code>
option. In such cases don't forget to add the <code>:content_type</code> option. Rails
will default to <code>text/plain</code> otherwise.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class UserMailer < ApplicationMailer
def welcome_email(user, email_body)
mail(to: user.email,
body: email_body,
content_type: "text/html",
subject: "Already rendered!")
end
end
</pre>
</div>
<h3 id="receiving-emails">3 Receiving Emails</h3><p>Receiving and parsing emails with Action Mailer can be a rather complex
endeavor. Before your email reaches your Rails app, you would have had to
configure your system to somehow forward emails to your app, which needs to be
listening for that. So, to receive emails in your Rails app you'll need to:</p>
<ul>
<li><p>Implement a <code>receive</code> method in your mailer.</p></li>
<li><p>Configure your email server to forward emails from the address(es) you would
like your app to receive to <code>/path/to/app/bin/rails runner
'UserMailer.receive(STDIN.read)'</code>.</p></li>
</ul>
<p>Once a method called <code>receive</code> is defined in any mailer, Action Mailer will
parse the raw incoming email into an email object, decode it, instantiate a new
mailer, and pass the email object to the mailer <code>receive</code> instance
method. Here's an example:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class UserMailer < ApplicationMailer
def receive(email)
page = Page.find_by(address: email.to.first)
page.emails.create(
subject: email.subject,
body: email.body
)
if email.has_attachments?
email.attachments.each do |attachment|
page.attachments.create({
file: attachment,
description: email.subject
})
end
end
end
end
</pre>
</div>
<h3 id="action-mailer-callbacks">4 Action Mailer Callbacks</h3><p>Action Mailer allows for you to specify a <code>before_action</code>, <code>after_action</code> and
<code>around_action</code>.</p>
<ul>
<li><p>Filters can be specified with a block or a symbol to a method in the mailer
class similar to controllers.</p></li>
<li><p>You could use a <code>before_action</code> to populate the mail object with defaults,
delivery_method_options or insert default headers and attachments.</p></li>
<li><p>You could use an <code>after_action</code> to do similar setup as a <code>before_action</code> but
using instance variables set in your mailer action.</p></li>
</ul>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class UserMailer < ApplicationMailer
after_action :set_delivery_options,
:prevent_delivery_to_guests,
:set_business_headers
def feedback_message(business, user)
@business = business
@user = user
mail
end
def campaign_message(business, user)
@business = business
@user = user
end
private
def set_delivery_options
# You have access to the mail instance,
# @business and @user instance variables here
if @business && @business.has_smtp_settings?
mail.delivery_method.settings.merge!(@business.smtp_settings)
end
end
def prevent_delivery_to_guests
if @user && @user.guest?
mail.perform_deliveries = false
end
end
def set_business_headers
if @business
headers["X-SMTPAPI-CATEGORY"] = @business.code
end
end
end
</pre>
</div>
<ul>
<li>Mailer Filters abort further processing if body is set to a non-nil value.</li>
</ul>
<h3 id="using-action-mailer-helpers">5 Using Action Mailer Helpers</h3><p>Action Mailer now just inherits from <code>AbstractController</code>, so you have access to
the same generic helpers as you do in Action Controller.</p><h3 id="action-mailer-configuration">6 Action Mailer Configuration</h3><p>The following configuration options are best made in one of the environment
files (environment.rb, production.rb, etc...)</p>
<table>
<thead>
<tr>
<th>Configuration</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>logger</code></td>
<td>Generates information on the mailing run if available. Can be set to <code>nil</code> for no logging. Compatible with both Ruby's own <code>Logger</code> and <code>Log4r</code> loggers.</td>
</tr>
<tr>
<td><code>smtp_settings</code></td>
<td>Allows detailed configuration for <code>:smtp</code> delivery method:<ul>
<li>
<code>:address</code> - Allows you to use a remote mail server. Just change it from its default <code>"localhost"</code> setting.</li>
<li>
<code>:port</code> - On the off chance that your mail server doesn't run on port 25, you can change it.</li>
<li>
<code>:domain</code> - If you need to specify a HELO domain, you can do it here.</li>
<li>
<code>:user_name</code> - If your mail server requires authentication, set the username in this setting.</li>
<li>
<code>:password</code> - If your mail server requires authentication, set the password in this setting.</li>
<li>
<code>:authentication</code> - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of <code>:plain</code>, <code>:login</code>, <code>:cram_md5</code>.</li>
<li>
<code>:enable_starttls_auto</code> - Set this to <code>false</code> if there is a problem with your server certificate that you cannot resolve.</li>
</ul>
</td>
</tr>
<tr>
<td><code>sendmail_settings</code></td>
<td>Allows you to override options for the <code>:sendmail</code> delivery method.<ul>
<li>
<code>:location</code> - The location of the sendmail executable. Defaults to <code>/usr/sbin/sendmail</code>.</li>
<li>
<code>:arguments</code> - The command line arguments to be passed to sendmail. Defaults to <code>-i -t</code>.</li>
</ul>
</td>
</tr>
<tr>
<td><code>raise_delivery_errors</code></td>
<td>Whether or not errors should be raised if the email fails to be delivered. This only works if the external email server is configured for immediate delivery.</td>
</tr>
<tr>
<td><code>delivery_method</code></td>
<td>Defines a delivery method. Possible values are:<ul>
<li>
<code>:smtp</code> (default), can be configured by using <code>config.action_mailer.smtp_settings</code>.</li>
<li>
<code>:sendmail</code>, can be configured by using <code>config.action_mailer.sendmail_settings</code>.</li>
<li>
<code>:file</code>: save emails to files; can be configured by using <code>config.action_mailer.file_settings</code>.</li>
<li>
<code>:test</code>: save emails to <code>ActionMailer::Base.deliveries</code> array.</li>
</ul>See <a href="http://api.rubyonrails.org/classes/ActionMailer/Base.html">API docs</a> for more info.</td>
</tr>
<tr>
<td><code>perform_deliveries</code></td>
<td>Determines whether deliveries are actually carried out when the <code>deliver</code> method is invoked on the Mail message. By default they are, but this can be turned off to help functional testing.</td>
</tr>
<tr>
<td><code>deliveries</code></td>
<td>Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful for unit and functional testing.</td>
</tr>
<tr>
<td><code>default_options</code></td>
<td>Allows you to set default values for the <code>mail</code> method options (<code>:from</code>, <code>:reply_to</code>, etc.).</td>
</tr>
</tbody>
</table>
<p>For a complete writeup of possible configurations see the
<a href="configuring.html#configuring-action-mailer">Configuring Action Mailer</a> in
our Configuring Rails Applications guide.</p><h4 id="example-action-mailer-configuration">6.1 Example Action Mailer Configuration</h4><p>An example would be adding the following to your appropriate
<code>config/environments/$RAILS_ENV.rb</code> file:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.action_mailer.delivery_method = :sendmail
# Defaults to:
# config.action_mailer.sendmail_settings = {
# location: '/usr/sbin/sendmail',
# arguments: '-i -t'
# }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: '[email protected]'}
</pre>
</div>
<h4 id="action-mailer-configuration-for-gmail">6.2 Action Mailer Configuration for Gmail</h4><p>As Action Mailer now uses the <a href="https://github.com/mikel/mail">Mail gem</a>, this
becomes as simple as adding to your <code>config/environments/$RAILS_ENV.rb</code> file:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: '<username>',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true }
</pre>
</div>
<h3 id="mailer-testing">7 Mailer Testing</h3><p>You can find detailed instructions on how to test your mailers in the
<a href="testing.html#testing-your-mailers">testing guide</a>.</p><h3 id="intercepting-emails">8 Intercepting Emails</h3><p>There are situations where you need to edit an email before it's
delivered. Fortunately Action Mailer provides hooks to intercept every
email. You can register an interceptor to make modifications to mail messages
right before they are handed to the delivery agents.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class SandboxEmailInterceptor
def self.delivering_email(message)
message.to = ['[email protected]']
end
end
</pre>
</div>
<p>Before the interceptor can do its job you need to register it with the Action
Mailer framework. You can do this in an initializer file
<code>config/initializers/sandbox_email_interceptor.rb</code></p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
if Rails.env.staging?
ActionMailer::Base.register_interceptor(SandboxEmailInterceptor)
end
</pre>
</div>
<div class="note"><p>The example above uses a custom environment called "staging" for a
production like server but for testing purposes. You can read
<a href="configuring.html#creating-rails-environments">Creating Rails environments</a>
for more information about custom Rails environments.</p></div>
<h3>反饋</h3>
<p>
歡迎幫忙改善指南的品質。
</p>
<p>
如發現任何錯誤之處,歡迎修正。開始貢獻前,可以先閱讀<a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">貢獻指南:文件</a>。
</p>
<p>翻譯如有錯誤,深感抱歉,歡迎 <a href="https://github.com/docrails-tw/guides/fork">Fork</a> 修正,或至此處<a href="https://github.com/docsrails-tw/guides/issues/new">回報</a>。</p>
<p>
文章可能有未完成或過時的內容。請先檢查 <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> 來確定問題在 master 是否已經修掉了。再上 master 補上缺少的文件。內容參考 <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南準則</a>來了解行文風格。
</p>
<p>最後,任何關於 Ruby on Rails 文件的討論,歡迎至 <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs 郵件論壇</a>。
</p>
</div>
</div>
</div>
<hr class="hide">
<div id="footer">
<div class="wrapper">
<p>本著作係採用<a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh_TW">創用 CC 姓名標示-相同方式分享 4.0 國際授權條款</a>授權。</p>
<p>“Rails”、“Ruby on Rails”,以及 Rails logo 為 David Heinemeier Hansson 的商標。版權所有。</p>
</div>
</div>
<script src="javascripts/jquery.min.js"></script>
<script src="javascripts/responsive-tables.js"></script>
<script src="javascripts/guides.js"></script>
<script src="javascripts/syntaxhighlighter/shCore.js"></script>
<script src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
<script src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
<script src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
<script src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
<script type="text/javascript">
SyntaxHighlighter.all();
$(guidesIndex.bind);
</script>
<script>
(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,"script","//www.google-analytics.com/analytics.js","ga");
ga("create", "UA-49903900-1", "auto");
ga("require", "displayfeatures");
ga("send", "pageview");
</script>
</body>
</html>