Action Mailer <br />Action Mailer is the Rails component that enables applications to send and receive e-mail. In this cha...
This tells ActionMailer that you want to user SMTP server. You can also set it to be :sendmail if you are using a Unix-bas...
ActionMailer::Base.default_content_type = &quot;text/html&quot;<br />ActionMailer::Base.default_content_type could be set ...
class Emailer &lt; ActionMailer::Base<br />end<br />Let&apos;s create one method as follows.<br />class Emailer &lt; Actio...
<ul><li>@subject defines the e-mail subject.
 @body is a Ruby hash that contains values with which you can populate the mail template. You created three key-value pair...
 @recipients is a list of the people to whom the message is being sent.
Upcoming SlideShare
Loading in...5
×

Action Mailer

1,301

Published on

Simple Mail Transfer Protocol (SMTP),
Net::SMTP.start,
smtp.send_message

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,301
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Action Mailer

  1. 1. Action Mailer <br />Action Mailer is the Rails component that enables applications to send and receive e-mail. In this chapter we will see how to send an email using Rails. So lets start with creating a emails project using following command.<br />C: uby&gt; rails emails<br />This will create required framework to proceed. Now we will start with configuring Action Mailer.<br />Action Mailer - Configuration<br />Following are the steps you have to follow to complete your configuration before proceeding with actual work:<br />Go into config folder of your emails project and open environment.rb file and add the following line at the bottom of this file.<br />ActionMailer::Base.delivery_method = :smtp<br />
  2. 2. This tells ActionMailer that you want to user SMTP server. You can also set it to be :sendmail if you are using a Unix-based operating system such as Mac OS X or Linux.<br />Add the following lines of code to the bottom of your environment.rb as well.<br />ActionMailer::Base.server_settings = {<br /> :address =&gt; &quot;smtp.site.com&quot;,<br /> :port =&gt; 25,<br /> :domain =&gt; “site.com&quot;,<br /> :authentication =&gt; :login,<br /> :user_name =&gt; &quot;username&quot;,<br /> :password =&gt; &quot;password&quot;,<br />}<br />Replace each hash value with proper settings for your Simple Mail Transfer Protocol (SMTP) server. You can take this information from your Internet Service Provider if you already don&apos;t know. You don&apos;t need to change port number 25 and authentication type if you are using standard SMTP server.<br />You may also change the default email message format. If you prefer to send email in HTML instead of plain text format, add the following line to config/environment.rb as well:<br />
  3. 3. ActionMailer::Base.default_content_type = &quot;text/html&quot;<br />ActionMailer::Base.default_content_type could be set to &quot;text/plain&quot;, &quot;text/html&quot;, and &quot;text/enriched&quot;. The default value is &quot;text/plain&quot;.<br />Next step will be to create a mailer<br />Generate a mailer:<br />Use the following command to generate a mailer as follows:<br />C: uby&gt; cd emails<br />C: ubyemails&gt; ruby script/generate mailer Emailer<br />This will create a file emailer.rb in appmodels directory. Check the content of this file is as follows:<br />
  4. 4. class Emailer &lt; ActionMailer::Base<br />end<br />Let&apos;s create one method as follows.<br />class Emailer &lt; ActionMailer::Base<br /> def contact(recipient, subject, message, sent_at = Time.now)<br /> @subject = subject<br /> @recipients = recipient<br /> @from = &apos;no-reply@yourdomain.com&apos;<br /> @sent_on = sent_at<br /> @body[&quot;title&quot;] = &apos;This is title&apos;<br /> @body[&quot;email&quot;] = &apos;sender@yourdomain.com&apos;<br /> @body[&quot;message&quot;] = message<br />end<br />end<br />The contact method has four parameters a recipient, subject, message and a sent_at, which defines when the e-mail is sent. The method also defines six standard parameters that are a part of every ActionMailer method:<br />
  5. 5. <ul><li>@subject defines the e-mail subject.
  6. 6. @body is a Ruby hash that contains values with which you can populate the mail template. You created three key-value pairs: title, email, and message
  7. 7. @recipients is a list of the people to whom the message is being sent.
  8. 8. @from defines who the e-mail is from.
  9. 9. @sent_on takes the sent_at parameter and sets the timestamp of the e-mail.</li></li></ul><li>Next we will create a controller for this application as follows:<br />C: ubyemails&gt; ruby script/generate controller Emailer<br />Now lets define a controller method in emailer_controller.rb which will call Model method to send actual email as follows:<br />class EmailerController &lt; ApplicationController<br /> def sendmail<br /> email = @params[&quot;email&quot;]<br /> recipient = email[&quot;recipient&quot;]<br /> subject = email[&quot;subject&quot;]<br /> message = email[&quot;message&quot;]<br />Emailer.deliver_contact(recipient, subject, message)<br /> render :text =&gt; &apos;Message sent successfully&apos;<br /> end<br />end<br />
  10. 10. Now let&apos;s define our view in appviewsemailsindex.rhtml<br />&lt;h1&gt;Send Email&lt;/h1&gt;<br />&lt;%= start_form_tag :action =&gt; &apos;sendmail&apos; %&gt;<br />&lt;p&gt;&lt;label for=&quot;email_subject&quot;&gt;Subject&lt;/label&gt;:<br />&lt;%= text_field &apos;email&apos;, &apos;subject&apos; %&gt;&lt;/p&gt;<br />&lt;p&gt;&lt;label for=&quot;email_recipient&quot;&gt;Recipient&lt;/label&gt;:<br />&lt;%= text_field &apos;email&apos;, &apos;recipient&apos; %&gt;&lt;/p&gt;<br />&lt;p&gt;&lt;label for=&quot;email_message&quot;&gt;Message&lt;/label&gt;&lt;br/&gt;<br />&lt;%= text_area &apos;email&apos;, &apos;message&apos; %&gt;&lt;/p&gt;<br />&lt;%= submit_tag &quot;Send&quot; %&gt;<br />&lt;%= end_form_tag %&gt;<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×