<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
    <title>Mike O'Toole's Blog</title>
    <link href="http://blog.motoole.com/" />
    <updated>2012-01-18T00:59:49-05:00</updated>
    
    
	    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/mike-otoole" /><feedburner:info uri="mike-otoole" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
	        <title>Reading SOPA</title>
	        
	        <link href="http://feedproxy.google.com/~r/mike-otoole/~3/o0xy9v5xp4s/reading-sopa.html" />
	        <updated>2012-01-16T00:00:00-05:00</updated>
	        <content type="html">&lt;p&gt;Yesterday, while &lt;a href='http://upwithchrishayes.msnbc.msn.com/_news/2012/01/15/10161056-debating-sopa'&gt;appearing on Up with Chris Hayes&lt;/a&gt;, Richard Cotton, NBCUniversal Executive Vice President and General Counsel, repeatedly claimed the SOPA was exclusively dedicated to fighting websites outside the United States that are wholesale dedicated to the theft of intellectual property. At one point he said &amp;#8220;This legislation would not effect a single site in the U.S.&amp;#8221; I was curious to see how much truth there was to Cotton&amp;#8217;s statement, so I gave the &lt;a href='http://hdl.loc.gov/loc.uscongress/legislation.112hr3261'&gt;text of the bill&lt;/a&gt; a read.&lt;/p&gt;

&lt;p&gt;The bill actually has several sections, all mostly related to preventing theft of U.S. Property online. The two sections of the bill that are primarily related to what everyone is talking about seem to be &lt;a href='http://www.opencongress.org/bill/112-h3261/text?version=ih&amp;amp;nid=t0:ih:76'&gt;102 &amp;#8220;ACTION BY ATTORNEY GENERAL TO PROTECT U.S. CUSTOMERS AND PREVENT U.S. SUPPORT OF FOREIGN INFRINGING SITES,&amp;#8221;&lt;/a&gt; and &lt;a href='http://www.opencongress.org/bill/112-h3261/text?version=ih&amp;amp;nid=t0:ih:166'&gt;103 &amp;#8220;MARKET-BASED SYSTEM TO PROTECT U.S. CUSTOMERS AND PREVENT U.S. FUNDING OF SITES DEDICATED TO THEFT OF U.S. PROPERTY.&amp;#8221;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While I&amp;#8217;m certainly not a legal scholar, it would seem that Mr. Cotton did not read the bill terribly closely. His claim that it only applies to foreign sites that are wholesale involved in copyright infrigement conflates pieces of these two sections of the bill.&lt;/p&gt;

&lt;p&gt;Section 102 deals with foreign sites involved in any sort of intelectual property infrindgement, not just sites that are dedicted wholesale to IP infindgement as Cotton suggests. The section outlines a set of powers that allow Attorney General to serve notice to search engines to remove links to these foreign sites, and payment network providers and advertising services to stop servicing the sites.&lt;/p&gt;

&lt;p&gt;Section 103 applys to any &amp;#8220;Internet site is dedicated to theft of U.S. property.&amp;#8221; This appears to be the section of the bill where Cotton got the part about &amp;#8216;site that are wholesale dedicated to piracy.&amp;#8217; However, this section of the bill appears to apply to any site that is used by users in the U.S., whether it&amp;#8217;s foreign or domestic.&lt;/p&gt;

&lt;p&gt;103 is also a particularly scary part of the bill. It grants powers to intelectual property holders to submit notices to payment service providers and internet advertising services, requiring that they cut off their services to sites &amp;#8216;dedicated to theft of U.S. property.&amp;#8217;&lt;/p&gt;

&lt;p&gt;To clarify&amp;#8211; a holder of intelectual property can claim that essentially &lt;em&gt;any&lt;/em&gt; website is dedicated to the theft of U.S. property, and that they are being harmed by the site. The IP holder formalizes this claim into an official notice as outlined in &lt;a href='http://www.opencongress.org/bill/112-h3261/text?version=ih&amp;amp;nid=t0:ih:175'&gt;103(b)(4)&lt;/a&gt;, and delivers it to any payment service providers or internet advertising service that are servicing the site. Those companies are then required to stop all services to site within 5 days (for exact details on what this entails checkout &lt;a href='http://www.opencongress.org/bill/112-h3261/text?version=ih&amp;amp;nid=t0:ih:165'&gt;103(b)(1)&lt;/a&gt; and &lt;a href='http://www.opencongress.org/bill/112-h3261/text?version=ih&amp;amp;nid=t0:ih:166'&gt;103(b)(2)&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;No court judgement is involved in IP holders serving these notices. However, if one of the providers elects to ignore the notice, then the IP holder can get a court order to force them into halting their services to the site.&lt;/p&gt;

&lt;p&gt;Claiming that SOPA will not effect a single U.S. based website is simply false.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/mike-otoole/~4/o0xy9v5xp4s" height="1" width="1"/&gt;</content>
	    <feedburner:origLink>http://motoole.com/2012/01/16/reading-sopa.html</feedburner:origLink></entry>
    
	    <entry>
	        <title>Hosting @font-face fonts on Amazon S3</title>
	        
	        <link href="http://feedproxy.google.com/~r/mike-otoole/~3/rACoLNDMjBg/hosting-font-face-fonts-on-s3.html" />
	        <updated>2011-10-19T00:00:00-04:00</updated>
	        <content type="html">&lt;p&gt;At &lt;a href='http://nestio.com'&gt;Nestio&lt;/a&gt; we host all of our static assets on an Amazon S3 bucket, http://static.nestio.com. Generally, this works great, we keep all requests to nestio.com limited strictly to things that require our app servers to respond and offload the work serving static assets to S3. Unfortunately, this comes with one limitation: Firefox won&amp;#8217;t show your users fonts you specify with @font-face, unless they&amp;#8217;re served off the same domain of the page they&amp;#8217;re being displayed on (not even a different subdomain works) or the fonts are served up with a proper Access-Control-Allow-Origin header.&lt;/p&gt;

&lt;p&gt;Sadly, right now S3 doesn&amp;#8217;t allow you to specify the Access-Control-Allow-Origin header that your objects get served with. For a while, we sort of let this slide and Firefox users just saw the next standard font down on the font stack, but with our recent redesign, we decided to finally implement a fix.&lt;/p&gt;

&lt;p&gt;We didn&amp;#8217;t really want to make any major changes to our asset deploy pipeline just for fonts, so hosting the fonts somewhere besides S3 wasn&amp;#8217;t really a good option.&lt;/p&gt;

&lt;p&gt;Instead, we setup a simple nginx VirtualHost for fonts.nestio.com, with the following config:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;server &lt;span class='o'&gt;{&lt;/span&gt;
    listen 80;

    server_name fonts.nestio.com;

    location / &lt;span class='o'&gt;{&lt;/span&gt;
        proxy_pass http://static.nestio.com/fonts/;
        add_header Access-Control-Allow-Origin http://nestio.com;
    &lt;span class='o'&gt;}&lt;/span&gt;
&lt;span class='o'&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Requests to fonts.nestio.com are proxied back to our Amazon S3 bucket, so the files themselves are still hosted and can be deployed through S3, but before sending a response to the user, nginx appends the necessary Access-Control-Allow-Origin header.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/mike-otoole/~4/rACoLNDMjBg" height="1" width="1"/&gt;</content>
	    <feedburner:origLink>http://motoole.com/2011/10/19/hosting-font-face-fonts-on-s3.html</feedburner:origLink></entry>
    
	    <entry>
	        <title>Effectively sending email from your web application</title>
	        
	        <link href="http://feedproxy.google.com/~r/mike-otoole/~3/qNVIB_R29o0/effectively-sending-email-from-your-web-application.html" />
	        <updated>2011-05-28T00:00:00-04:00</updated>
	        <content type="html">&lt;p&gt;Successfully sending email from a web application is a complex problem. Unfortunately, this complexity is often hidden until you have to spend hours or days trying to figure out why your email isn&amp;#8217;t getting to your users.&lt;/p&gt;

&lt;p&gt;Sending an email in PHP is seemingly as simple as this:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='php'&gt;&lt;span class='x'&gt;mail($to, $subject, $body);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Frameworks like Rails and Django have similar methods for sending email.&lt;/p&gt;

&lt;p&gt;With the default configuration and the likely case that your web application server has a Mail Transfer Agent (MTA) like &lt;a href='http://sendmail.org/'&gt;Sendmail&lt;/a&gt; or &lt;a href='http://www.postfix.org/'&gt;Postfix&lt;/a&gt;, running on it, PHP will happily execute this call for you. And it&amp;#8217;ll return true, suggesting that the email will actually be delivered. Sometimes this will work beautifully. However, unless you&amp;#8217;ve done a lot of work upfront to ensure that your web server is allowed to send email on behalf of your domain, you&amp;#8217;ll start getting complaints from users that your email is ending up in their spam folder or not arriving at all.&lt;/p&gt;

&lt;p&gt;Rather than spending the time focusing on making your web server a valid point of origin for your email, it&amp;#8217;s much easier to punt the responsibility to a service that&amp;#8217;s already been setup to send email on behalf of your domain, like an existing email provider. Or, better yet, you can use a 3rd party service that specifically focuses on sending large volumes of transactional email from web apps. Some options are &lt;a href='http://sendgrid.com'&gt;Sendgrid&lt;/a&gt;, &lt;a href='http://postmarkapp.com'&gt;Postmark&lt;/a&gt;, or &lt;a href='http://aws.amazon.com/ses/'&gt;Amazon SES&lt;/a&gt;. Just follow the directions provided by any of these services for setting up DNS records to ensure that they are authorized to send email from your domain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note to GMail/Google Apps users:&lt;/strong&gt; It&amp;#8217;s not a great idea to have your web app send email using Google&amp;#8217;s SMTP servers, as Google limits all accounts to sending a maximum of 500 emails per day. If you start sending significant numbers of messages, you&amp;#8217;ll easily max that out. It&amp;#8217;s likely that many other mail providers have similar limits, so check with yours before using their SMTP servers to send high volumes of email.&lt;/p&gt;

&lt;h3 id='remote_network_connections'&gt;Remote Network Connections&lt;/h3&gt;

&lt;p&gt;However, sending mail via a remote server introduces a whole new problem: a remote network connection has to made whenever your app sends an email. So every time a user submits a request that triggers an email being sent, a connection has to be made to the remote SMTP server and the message data needs to be passed through it. All of this probably happens while the end-user is waiting for a response. If network latency is high then it can really slow down that response being returned to the user, or if latency is really bad the user&amp;#8217;s connection to your site might timeout entirely. Even worse, the remote SMTP server might be unreachable because it&amp;#8217;s down or there&amp;#8217;s a network partition somewhere along the line. If that happens, depending on how your app is built, it may fail silently or an error will be be returned to the user, in both cases the email is never sent.&lt;/p&gt;

&lt;p&gt;To prevent such problems, rather than attempting to connect to your SMTP server while the user is waiting for a response, it&amp;#8217;s preferable to queue the message locally, return a response to the user, and then in the background connect to your SMTP server and send the message. If the SMTP server is unreachable, requeue the message and try to send it later.&lt;/p&gt;

&lt;p&gt;You could build all that logic into your application. Maybe store messages to be sent in a database table, and have a background job that attempts to send them, but that really seems like a lot of work and overhead for something as simple as sending email. Low and behold, all this queuing and retry logic for sending messages to remote SMTP servers is already built into MTA software like Postfix. As we saw above, you can run Postfix locally on your web server, which virtually eliminates any latency or possibility of a network partition when your application is connecting to send an email.&lt;/p&gt;

&lt;p&gt;But you&amp;#8217;ve just spent all this money on a Sendgrid account so that your email you wouldn&amp;#8217;t end up in a user&amp;#8217;s spam box, why would you go back to using a local MTA!? Well it turns out that MTAs can be configured to relay all of the mail they receive to an upstream SMTP server (like Sendgrid, Postmark, or your email provider of choice), that server in turn will go ahead and relay the message to third party recipients.&lt;/p&gt;

&lt;p&gt;So rather than connecting to a remote server to send email, your web application can connect to it&amp;#8217;s local instance of Postfix. Postfix accepts the message, quickly adds to it to its message queue, and responds with a success code. Your web app can then go ahead return a response to the user, who can go about their business. In the meantime, Postfix will asynchronously attempt to connect to your upstream SMTP server and pass the message along. In the event that it can&amp;#8217;t reach the upstream server, it&amp;#8217;ll put the message back on the queue and try again later. Once the upstream SMTP server gets the message, it sends it to the recipient&amp;#8217;s SMTP server; so to that server it looks like the email is coming from a valid point of origin: your email provider, rather than your web server.&lt;/p&gt;

&lt;h3 id='implementing_the_solution'&gt;Implementing the Solution&lt;/h3&gt;

&lt;p&gt;If your application server is running a *NIX system, I highly recommend setting up Postfix as a local MTA to relay messages to an upstream SMTP server. It&amp;#8217;s fast and relatively easy to configure. If it&amp;#8217;s not already installed, you should be available to install it with your system&amp;#8217;s package manager. For example, using Debian or Ubuntu:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;apt-get install postfix
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;If the install process asks you about configuring Postfix, simply select No Configuration, we&amp;#8217;ll configure it ourselves in just a minute.&lt;/p&gt;

&lt;p&gt;Once it&amp;#8217;s installed, test to ensure that postfix is capable in sending mail in it&amp;#8217;s default state, by running the following command:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nb'&gt;echo&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;Oh hi there&amp;quot;&lt;/span&gt; | sendmail your_email_address@host.com
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This will send an email using Postfix to your email address. If you don&amp;#8217;t receive it, first check your spam folder, then check the Postfix log, usually stored at /var/log/mail.log, for errors.&lt;/p&gt;

&lt;p&gt;The first thing to do whenever you setup an MTA, is to ensure you&amp;#8217;re not creating an &lt;a href='http://en.wikipedia.org/wiki/Open_mail_relay'&gt;open relay&lt;/a&gt;. Open relays allow anyone on the Internet to connect to your MTA and send mail using it. This is really bad. Spammers will find this and use it to send lots of email through your server. Leading to many terrible things, including having your IP blacklisted as a server that sends spam. By default Postfix accepts all connections on port 25, so ideally you should deny all remote connections to port 25 via your firewall. In addition to that, you should to configure Postfix itself to only accept connections from localhost, by adding this line to the bottom of Postfix&amp;#8217;s main.cf file (located at /etc/postfix/main.cf on Debian/Ubuntu):&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;inet_interfaces&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; loopback-only
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Once you&amp;#8217;ve added that, restart the Postfix daemon. On Debian or Ubuntu:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;/etc/init.d/postfix restart
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With that, only connections coming from the within the server itself will be able to send email through Postfix.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note to Amazon SES users:&lt;/strong&gt; While it is possible to relay mail from Postfix to Amazon SES, the configuration is slightly different than what is outlined beyond this point, for more information check out &lt;a href='http://docs.amazonwebservices.com/ses/latest/DeveloperGuide/IntegratingWithServer.Postfix.html'&gt;Amazon&amp;#8217;s guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Your upstream SMTP server probably requires authentication, so you&amp;#8217;ll also need to install some SASL libraries that Postfix relies on for authentication. On Dedian/Ubuntu:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;apt-get install libsasl2-modules
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Or a yum-based system:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;yum install cyrus-sasl-plain
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Postfix then needs to be configured to relay all the mail it receives to your upstream SMTP server. To do that, simply add the following lines to the bottom of main.cf file:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;smtp_sasl_auth_enable&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; yes 
&lt;span class='nv'&gt;smtp_sasl_password_maps&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; static:your-SMTP-Username:your-SMTP-Password
&lt;span class='nv'&gt;smtp_sasl_security_options&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; noanonymous 
&lt;span class='nv'&gt;header_size_limit&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; 4096000
&lt;span class='nv'&gt;relayhost&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='o'&gt;[&lt;/span&gt;your.smtp.host&lt;span class='o'&gt;]&lt;/span&gt;:25
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Obviously replacing the auth credentials and host with those provided by your email provider.&lt;/p&gt;

&lt;p&gt;Or, if your upstream SMTP server supports it, enable TLS encryption, so that your messages and authentication credentials aren&amp;#8217;t sent in plaintext, use this configuration:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;smtp_sasl_auth_enable&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; yes 
&lt;span class='nv'&gt;smtp_sasl_password_maps&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; static:your-SMTP-Username:your-SMTP-Password 
&lt;span class='nv'&gt;smtp_sasl_security_options&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; noanonymous 
&lt;span class='nv'&gt;smtp_tls_security_level&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; may
&lt;span class='nv'&gt;header_size_limit&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; 4096000
&lt;span class='nv'&gt;relayhost&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='o'&gt;[&lt;/span&gt;your.smtp.host&lt;span class='o'&gt;]&lt;/span&gt;:587
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;After updating the configuration file, restart Postfix.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;/etc/init.d/postfix restart
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And configure your web application to send email through the SMTP server localhost, on port 25 with no authentication. For example in Rails:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='no'&gt;ActionMailer&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='no'&gt;Base&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;smtp_settings&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt;
  &lt;span class='ss'&gt;:address&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;localhost&amp;quot;&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt;
  &lt;span class='ss'&gt;:port&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;25&amp;#39;&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt;
  &lt;span class='ss'&gt;:domain&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;yourdomain.com&amp;quot;&lt;/span&gt;
&lt;span class='p'&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Send some test emails from your web app, or from the command line:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nb'&gt;echo&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;Oh hi there&amp;quot;&lt;/span&gt; | sendmail your_email_address@host.com
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;If you look at the headers of these messages, they should indicate that they&amp;#8217;re being sent through your upstream SMTP server rather than directly from your web app server.&lt;/p&gt;

&lt;p&gt;This gives you the advantages of sending mail via local MTA: minimal network latency, as well as having messages queued and retried when the upstream SMTP server is unavailable, combined with the deliverability advantages of using an SMTP server that&amp;#8217;s properly setup to deliver mail on behalf of your domain. To the recipient mail servers, messages will look like it&amp;#8217;s coming from your proper SMTP server rather than your web app server.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/mike-otoole/~4/qNVIB_R29o0" height="1" width="1"/&gt;</content>
	    <feedburner:origLink>http://motoole.com/2011/05/28/effectively-sending-email-from-your-web-application.html</feedburner:origLink></entry>
    
</feed>

