<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Notes on ScopePort &#187; actionmailer</title>
	<atom:link href="http://blog.scopeport.org/tag/actionmailer/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.scopeport.org</link>
	<description>The ScopePort Blog</description>
	<lastBuildDate>Sun, 07 Feb 2010 01:08:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to use Rails SMTP configuration parameters from database</title>
		<link>http://blog.scopeport.org/ruby-on-rails/rails-smtp-configuration-parameters-database/</link>
		<comments>http://blog.scopeport.org/ruby-on-rails/rails-smtp-configuration-parameters-database/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 01:08:34 +0000</pubDate>
		<dc:creator>Lennart</dc:creator>
				<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[actionmailer]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[settings]]></category>
		<category><![CDATA[smtp]]></category>

		<guid isPermaLink="false">http://blog.scopeport.org/?p=270</guid>
		<description><![CDATA[Usually the Rails SMTP configuration takes place in config/environment.rb like this:

ActionMailer::Base.smtp_settings = &#123;
  :address =&#62; &#34;smtp.gmail.com&#34;,
  :port =&#62; 587,
  :domain =&#62; &#34;domain.com&#34;,
  :user_name =&#62; &#34;user@domain.com&#34;,
  :password =&#62; &#34;password&#34;,
  :authentication =&#62; :plain
&#125;

ScopePort already has a Email settings part in the setup section where all the required SMTP settings are stored. [...]]]></description>
			<content:encoded><![CDATA[<p>Usually the Rails SMTP configuration takes place in <em>config/environment.rb</em> like this:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActionMailer::Base</span>.<span style="color:#9900CC;">smtp_settings</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:address</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;smtp.gmail.com&quot;</span>,
  <span style="color:#ff3333; font-weight:bold;">:port</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">587</span>,
  <span style="color:#ff3333; font-weight:bold;">:domain</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;domain.com&quot;</span>,
  <span style="color:#ff3333; font-weight:bold;">:user_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;user@domain.com&quot;</span>,
  <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;password&quot;</span>,
  <span style="color:#ff3333; font-weight:bold;">:authentication</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:plain</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>ScopePort already has a <em>Email settings</em> part in the setup section where all the required SMTP settings are stored. I wanted to fetch the SMTP configuration from the database to avoid double configuration. I stumbled over this blog post after a while: <a href="http://broadcast.oreilly.com/2009/03/using-multiple-smtp-accounts-w.html" target="_blank">http://broadcast.oreilly.com/2009/03/using-multiple-smtp-accounts-w.html</a> Based on this I developed the following dynamic way to define the SMTP settings:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> EmergencyMailer <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionMailer::Base</span>   
  <span style="color:#9966CC; font-weight:bold;">def</span> load_settings
    @@smtp_settings = <span style="color:#006600; font-weight:bold;">&#123;</span>
      <span style="color:#ff3333; font-weight:bold;">:address</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Setting.<span style="color:#5A0A0A; font-weight:bold;">first</span>.<span style="color:#9900CC;">mail_server</span>,
      <span style="color:#ff3333; font-weight:bold;">:port</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Setting.<span style="color:#5A0A0A; font-weight:bold;">first</span>.<span style="color:#9900CC;">mail_port</span>,
      <span style="color:#ff3333; font-weight:bold;">:domain</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Setting.<span style="color:#5A0A0A; font-weight:bold;">first</span>.<span style="color:#9900CC;">mail_hostname</span>,
      <span style="color:#ff3333; font-weight:bold;">:authentication</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:plain</span>,
      <span style="color:#ff3333; font-weight:bold;">:user_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Setting.<span style="color:#5A0A0A; font-weight:bold;">first</span>.<span style="color:#9900CC;">mail_user</span>,
      <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span>Setting.<span style="color:#5A0A0A; font-weight:bold;">first</span>.<span style="color:#9900CC;">mail_pass</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> emergency_notification<span style="color:#006600; font-weight:bold;">&#40;</span>emergency, email<span style="color:#006600; font-weight:bold;">&#41;</span>
    load_settings
    recipients  email
    from        Setting.<span style="color:#5A0A0A; font-weight:bold;">first</span>.<span style="color:#9900CC;">mail_from</span>
    subject     <span style="color:#996600;">&quot;[ScopePort] An emergency has been declared!&quot;</span>
    sent_on     <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
    body        <span style="color:#ff3333; font-weight:bold;">:emergency</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> emergency
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The method <em>load_settings</em> gets called by the delivery method and fills the <em>smtp_settings</em> instance variable with parameters from the database.</p>
<p>Check out this Rails Guide if you want to learn more about ActionMailer: <a href="http://guides.rubyonrails.org/action_mailer_basics.html" target="_blank">http://guides.rubyonrails.org/action_mailer_basics.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.scopeport.org/ruby-on-rails/rails-smtp-configuration-parameters-database/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
