Set up an SPF Record to (theoretically) reduce the risk of your web app’s email being marked as spam

I’m setting up email sending from my domain instead of faking gmail.com because of mail delivery issues. To make this work without just going automatically to everyone’s spam folder I have set up an SPF record. I found the documentation on the web a bit dense trying to work this out so here’s a short look at what I did.

“Show Original” in gmail used to say:

Received-SPF: neutral (google.com: 74.54.101.112 is neither permitted nor denied by domain of bakert+fantasyfootball@gmail.com) client-ip=74.54.101.112;
Authentication-Results: mx.google.com; spf=neutral (google.com: 74.54.101.112 is neither permitted nor denied by domain of bakert+fantasyfootball@gmail.com) smtp.mail=bakert+fantasyfootball@gmail.com

Now it says:

Received-SPF: pass (google.com: domain of admin+2@ff.bluebones.net designates 178.79.178.155 as permitted sender) client-ip=178.79.178.155;
Authentication-Results: mx.google.com; spf=pass (google.com: domain of admin+2@ff.bluebones.net designates 178.79.178.155 as permitted sender) smtp.mail=admin+2@ff.bluebones.net

This means Gmail is less likely to mark email sent by my web app as spam, thanks to the SPF record I have set up.

An SPF record is implemented as a DNS TXT (as opposed to A, CNAME, etc.) record with no “name” and a value something like this:

v=spf1 a mx include:_spf.google.com ~all

Let’s break that down …

v=spf1 – this indicates that the record is an SPF v1 record.

A – this indicates that any IP address in an A record for this network is allowed to send email for this domain.

MX – this indicates that any IP address in an MX record for this network is allowed to send email for this domain.

include:_spf.google.com – this indicates that _spf.google.com is allowed to send email for this domain.

~all – this indicates that _spf.google.com is allowed to send email for this domain.

All parts of the record except v=spf1 are optional and can take a prefix of -, + or ~. If there is no prefix + is assumed. + means allowed, – means not allowed and ~ means might be allowed. all is the catch-all for anything not explicitly called out by the rule.

You might be interested in a more detailed guide to SPF record syntax.

Setup NS Records for Subdomain

If you want to serve DNS for a subdomain while keeping most of the DNS records to do with its domain with another DNS provider you need NS records.

On the nameserver for example.com:

somesubdomain			NS	ns1.otherprovider.net
somesubdomain			NS	ns2.otherprovider.net
somesubdomain			NS	ns3.otherprovider.net
somesubdomain			NS	ns4.otherprovider.net

On the otherprovider.net nameserver for somesubdomain.example.com you just need a “normal” A record (+CNAME/MX/whatever):

somesubdomain			A	178.79.178.155

I have this setup working with everydns.net providing most of my DNS but linode providing DNS for the sites I have moved off my old providers and on to linode.

The previously wonderful everydns that served me well for many years (for nothing!) has been bought up and is going paid in less than 90 days so I’m taking this opportunity to move things around.

Google’s One-Letter Guesses

Top search suggestions on google.com just now after entering a single letter:

amazon
best buy
craigslist
dictionary
ebay
facebook
google
hotmail
irs
jet blue
kohls
lowes
mapquest
netflix
osama bin laden
pandora
quotes
rebecca black
southwest
target
ups
verizon
weather
xbox
youtube
zillow
127 hours
2011 calendar
3ds
411
50 cent
60 minutes
7zip
8 mile
90210
0
@font-face
&nbsp
_
.net

“Javascript: the Good Parts”: the Good Parts

  • Use JSLint to determine if what you are doing is sensible.
  • Avoid “/* … */” for comments, “*/” appears in useful constructs in javascript. Use “//”.
  • Javascript strings are 16-bit unicode. It has no character type, only string.
  • All numbers are numeric, there is no int/float/etc.
  • The value Infinity represents all values greater than ~1.79E+308
  • Javascript has functional scoping. Braces do not denote scope. Thus, declare variables at the top of functions not within braces.
  • Falsy values: false, null, undefined, ” (empty string), 0, NaN. Everything else is truthy.
  • If using a for ... in loop you must you hasOwnProperty.
  • typeof returns "object" for an array and for null.
  • Declare var that = this; in a function so that inner functions can access that function’s this value. (Prefer “self”?)
  • Because functions are first class objects, javascript supports currying.
  • General memoizer (p. 45)
  • Don’t use new at all.
  • Functional inheritance (p. 52).
  • Do not use “/g” with RegExp.test.
  • Prefer “if (” to “if(” because “if(” looks like a method invocation.
  • K&R-style curly braces because of how return works in javascript.
  • Always pass the second parameter (radix) to parseInt.
  • Always use === and !== not == and !=.
  • Do not use with.
  • Use JSON.parse not eval.
  • Do not use ++ and --.

git

git checkout -b $BRANCHNAME origin/$BRANCHNAME
 
git checkout $PATH

git diff $ID..$ID^

git diff --cached

git log $ID

git show $ID

git commit -m "$MSG"

git log --pretty=format:'%H %an %s (%cr)' --abbrev-commit -S"$SEARCH_STRING"

git blame $ID

git status

git show :$N:$PATH

git stash; git stash apply

git bisect

git log -u

git log --grep=$SEARCH_STRING

git log --author

git log -n $N

git cherry-pick $ID

Remember the Milk “Plugin” for Alfred

Alfred is a really slick new launcher for OS X. It’s a lot simpler than QuickSilver.

One feature I can’t live without in my launcher is the ability to add tasks to my todo list and diary, both kept in the excellent Remember the Milk.

Alfred can’t run commandline programs (yet) so I wrote this simple webpage to send in tasks via the Remember the Milk email interface.

<?php

define('RTM_ADDR', 'YOUR RMILK EMAIL POST ADDRESS HERE');
define('CC_ADDR', 'IF YOU WANT TO SEND A COPY ANYWHERE');

function main() {
    $list = isset($_GET['list']) ? $_GET['list'] : 'To Do';
    $sending = $_GET['s'] . " #" . $list;
    if (strpos($sending, "^") === FALSE) {
        echo '<p>No Due Date - set to today</p>';
        $sending .= " ^today";
    }
    echo "<p>Sending " . htmlentities($sending) . "</p>";
    $success = mail(RTM_ADDR, $sending, '', "Cc: " . CC_ADDR . "\r\n");
    echo $success ? '<p style="color: green">Success</p>' : '<p style="font-size: large; color: red">Failure</p>';
}

main();

It’s quite annoying having a browser window come open and success/failure refers only to the sending of the email not the addition to Remember the Milk, but it’s a lot better than not having the capability.

PS I kind of hate the actual Remember the Milk API. It’s wonderful that they support undo and serious security and so on but it’s a real pain when all you want to do is post a task to a list and you have to deal with requesting timelines and frobs and tokens, etc.

Ning Appathon

Following last month’s Ning Apps launch, we’re excited to
announce that we’ll be holding a special developer event called the
Ning Appathon at our offices in Palo Alto, CA on Thursday,
November 5th from 6pm-10pm.

The event will include:

  • An overview of Ning Apps and our OpenSocial implementation
  • Presentations from existing Ning Apps developers
  • A chance to meet members of the Ning Engineering and Developer
    Advocacy teams
  • Free pizza and beer

Most importantly, we’ll be announcing the start of a
week-long app development competition which will include awards for
new applications in addition to ported applications. Prizes and
details will be revealed at the event.

Location

Ning
167 Hamilton Ave
2nd Floor
Palo Alto, CA 94301

Date

Thursday, November 5th

Time

6pm-10pm

Prize Info

To be announced at the event!

All attendees will receive a complimentary Ning hoodie, so be
sure to tell us your shirt size when RSVPing. You can attend solo
or bring one colleague, we only ask that you RSVP by 9pm PST on
Thursday, October 29th. All attendees must be at least 21 years
of age.

More Details and Registration

League Table Generator

I’ve written a simple league table generator in the style of my fixtures generator.

Example table

Source code, for those that are interested in such things …

Source code on github

<?php

require_once('masort.php');

function main() {
    $s = "";
    $cmd = (isset($_REQUEST['cmd']) ? $_REQUEST['cmd'] : null);
    $id = (isset($_GET['id']) ? $_GET['id'] : null);
    $title = "";
    if ($cmd === 'add') {
        list($id, $display) = add($id, $_POST['results']);
        $s .= $display;
        $title = ($id ? "Table $id" : 'Create Table');
    }
    if ($id) {
        if (isset($_GET['txt'])) {
            $s .= "<pre>" . table_text($id) . "</pre>";
        } else {
            $s .= table($id);
        }
        $title = "Table $id";
    }
    $s = head($title, $id) . $s;
    $s .= input_form($id);
    $s .= ($id ? results($id) : "");
    $s .= table_links();
    $s .= foot();
    echo $s;
}

// String of HTML input form for results.
function input_form($id) {
    $instructions = '<p class="instructions"><b>Enter results in format</b> <code>name X - Y name</code> <b>to ';
    $instructions .= ($id ? "add to the" : "start a new");
    $instructions .= " table.</b></p>";
    ob_start();
    ?>
    <form method="POST">
        <input type="hidden" name="cmd" value="add" />
        <input type="hidden" name="id" value="<?php echo h($id); ?>" />
        <?php echo $instructions; ?>
        <textarea name="results"></textarea>
        <p><input type="submit" value="Add" /></p>
    </form>
    <?php
    echo ($id ? "" : "<p>Example: <pre>Liverpool 1 - 0 Man Utd\nEverton 2 - 0 Aston Villa\nLiverpool 3 - 1 Everton\nAston Villa 0 - 0 Man Utd</pre>");
    return ob_get_clean();
}

// Add results to a table, creating the table if necessary.
//TODO if we just created a table we won't display it here but we should.
function add($provided_id, $s) {
    $id = ($provided_id ? $provided_id : generate_id());
    $results = parse_results($s);
    $added = 0;
    foreach ($results as $r) {
        extract($r);
        $sql = "INSERT INTO result (table_id, home, away, for, against) VALUES ";
        $sql .= "(" . q($id) . ", " . q($home) . ", " . q($away) . ", " . q($for) . ", " . q($against) . ")";
        $added += db($sql);
    }
    if (! $provided_id) {
        header("Location: " . self_ref_url() . "?id=" . $id);
        return;
    }
    ob_start();
    ?>
    <p class="success">Added <?php echo $added; ?> results to the table.</p>
    <?php
    return array($id, ob_get_clean());
}

// String of HTML display of table $id.
function table($id) {
    $table = generate_table($id);
    $s = "<table><thead><tr><th>Team</th><th>P</th><th>W</th><th>D</th><th>L</th><th>F</th><th>A</th><th>Pts</th></tr></thead><tbody>";
    foreach ($table as $team) {
        extract(hmap($team));
        $s .= "<tr><td>$name</td><td class=\"n\">$played</td><td class=\"n\">$won</td><td class=\"n\">$drawn</td><td class=\"n\">$lost</td><td class=\"n\">$for</td><td class=\"n\">$against</td><td class=\"n\">$points</td></tr>";
    }
    $s .= "</tbody>";
    $s .= '<p><a href="' . self_ref_url() . '?id=' . h($id) . '&txt=1">Text version</a></p>';
    return $s;
}

// String of display of table $id suitable for display in monospace font.
function table_text($id) {
    $EXTRA_PADDING = 2;
    $table = generate_table($id);
    list($longest, $numeric) = array(array(), array());
    foreach ($table as $team) {
        foreach (hmap($team) as $k => $v) {
            $longest[$k] = (isset($longest[$k]) && $longest[$k] >= mb_strlen($v) ? $longest[$k] : mb_strlen($v));
            $numeric[$k] = (isset($numeric[$k]) ? $numeric[$k] && is_numeric($v) : is_numeric($v));
        }
    }
    $s = "";
    foreach ($longest as $k => $max) {
        $display = ucwords(strlen($k) > $longest[$k] ? substr($k, 0, 1) : $k);
        if ($numeric[$k]) {
            $s .= str_pad($display, $max + $EXTRA_PADDING, " ", STR_PAD_LEFT);
        } else {
            $s .= str_pad($display, $max + $EXTRA_PADDING);
        }
    }
    foreach ($table as $team) {
        $s .= "\n";
        foreach (hmap($team) as $k => $v) {
            if ($numeric[$k]) {
                $s .= str_pad($v, $longest[$k] + $EXTRA_PADDING, " ", STR_PAD_LEFT);
            } else {
                $s .= str_pad($v, $longest[$k] + $EXTRA_PADDING);
            }
        }
    }
    $s .= '<p><a href="' . self_ref_url() . '?id=' . h($id) . '">HTML version</a></p>';
    return $s . "\n";
}

// String of HTML results.
function results($id) {
    $rs = get_results($id);
    $s = '<table><tbody>';
    foreach ($rs as $r) {
        extract(hmap($r));
        $s .= "<tr><td>$home</td><td>$for</td><td>-</td><td>$against</td><td>$away</td></tr>";
    }
    return $s . "</tbody></table>";
}

// String of HTML links to all known tables.
function table_links() {
    $sql = "SELECT DISTINCT(table_id) AS id FROM result ORDER BY table_id";
    $rs = db($sql);
    if (! is_array($rs)) { return ""; }
    $s = "";
    foreach ($rs as $r) {
        extract(hmap($r));
        $s .= '<p><a href="?id=' . $id . '">Table ' . $id . '</a></p>';
    }
    return $s;
}

// ********** Helpers **********

function get_results($id) {
    $sql = "SELECT home, away, for, against FROM result WHERE table_id = " . q($id);
    return db($sql);
}

function generate_table($id) {
    $rs = get_results($id);
    $table = array();
    foreach ($rs as $r) {
        extract($r);
        $table = add_result($table, $home, $for, $against);
        $table = add_result($table, $away, $against, $for);
    }
    masort($table, 'points_d,for_d,against_a'); //TODO sort should be more complicated for GD etc.
    return $table;
}

function parse_results($s) {
    $s = preg_replace('/[ \t]+/', ' ', $s);
    $matches = explode("\n", $s);
    $results = array();
    foreach ($matches as $match) {
        if (preg_match('/^(.*?) (\d+) - (\d+) (.*?)$/', $match, $details)) {
            $results[] = array('home' => trim($details[1]), 'for' => trim($details[2]), 'against' => trim($details[3]), 'away' => trim($details[4]));
        }
    }
    return $results;
}

function add_result($table, $team, $for, $against) {
    if (! isset($table[$team])) {
        $table[$team] = array('name' => $team, 'played' => 0, 'won' => 0, 'drawn' => 0, 'lost' => 0, 'for' => 0, 'against' => 0, 'points' => 0);
    }
    if ($for > $against) {
        $table[$team]['won'] += 1;
        $table[$team]['points'] += 3;
    } else if ($for < $against) {
        $table[$team]['lost'] += 1;
    } else {
        $table[$team]['drawn'] += 1;
        $table[$team]['points'] += 1;
    }
    $table[$team]['played'] += 1;
    $table[$team]['for'] += $for;
    $table[$team]['against'] += $against;
    return $table;
}

// Get next table id in the database.  Unsafe.
function generate_id() {
    $sql = "SELECT IFNULL(MAX(table_id), 0) + 1 AS result FROM result";
    $rs = db($sql);
    return $rs[0]['result'];
}

// ********* Header/Footer **********

// String of HTML header.
function head($title, $id) {
    ob_start();
    ?>
    < !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <title>League Table Generator<?php if ($title) { echo " - $title"; } ?></title>
            <link rel="stylesheet" href="blueprint/screen.css" type="text/css" media="screen, projection">
            </link><link rel="stylesheet" href="blueprint/print.css" type="text/css" media="print">
            <!--[if lt IE 8]>
            </link><link rel="stylesheet" href="css/blueprint/ie.css" type="text/css" media="screen, projection">
            < ![endif]-->
            <link rel="stylesheet" type="text/css" href="table.css" />
        </link></head>
        <body>
            <div class="container">
                <div class="span-10 last">
                    <h1>Table Generator</h1>
                    <p>This program is part of <a href="/2009/09/league-table-generator">bluebones.net</a></p>
                    <?php if ($title) { echo "<h2>$title"; } ?>
                    <?php if ($id) { ?>
                        <p><a href="<?php echo h($_SERVER['SCRIPT_NAME']); ?>">New Table</a></p>
                    <?php } ?>

    <?php
    return ob_get_clean();
}

// String of HTML footer.
function foot() {
   ob_start();
   ?>
                </div>
            </div>
        </body>
    </html>
    <?php
   return ob_get_clean();
}

// ********** Utilities **********

function self_ref_url() {
    $host  = $_SERVER['HTTP_HOST'];
    $uri   = $_SERVER['PHP_SELF'];
    return "http://$host$uri";
}

// SQL-quote a string.
function q($s) {
    return "'" . str_replace("'", "''", $s) . "'";
}

// HTML escaping to prevent XSS
function h($s) {
    return htmlentities($s);
}

// HTML escape the values of an assoc array
function hmap($a) {
    $new = array();
    foreach ($a as $k => $v) {
        $new[$k] = h($v);
    }
    return $new;
}

// Exec query on db $id creating it if necessary and returning array of results if a SELECT.
function db($sql) {
    $db = sqlite_open('results');
    // Create table if it doesn't exist.  Ignore error if it does.
    @sqlite_exec($db, 'CREATE TABLE result (home VARCHAR(255), away VARCHAR(255), for INT, against INT, table_id INT)');
    if (strpos($sql, "SELECT") === 0) {
        $q = sqlite_query($db, $sql);
        return sqlite_fetch_all($q, SQLITE_ASSOC);
    } else {
        return sqlite_exec($db, $sql);
    }
}

main();

/*
Copyright (c) 2009 Thomas David Baker

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/