How to Configure a Rackspace Cloud Server to Use Sendgrid

There are many reasons that we always strongly encourage our clients to use Rackspace as their host; one of the main reasons is that Rackspace is highly proactive in finding solutions to problems before users noticed that there even is a problem. Such is the case with their recent partnership with SendGrid. They noticed that because IPs for Cloud Servers are dynamically allocated that problems were arising with the deliverability of emails generated from the servers.

Starting at the end of May, all new server instances (*nix, at least, I don't know about Windows) have their configuration files preset to use SendGrid. This is all well and good but for whatever reason the communication about how to detect this change and finalize configuration is a bit hazy. If you contact their tech support they know exactly what to do, but let's be honest: if you're able to work at the command line level, tech support is a tool of last resort. Google always comes first. So having successfully worked through a setup without not finding a comprehensive "how to" I figured I'd write one.

Let's get started.

Step #1: Set up the SendGrid Account

Before we even SSH into the server, we're going to need to create our free SendGrid account. This will require that you authenticate an email address and then wait for SendGrid to get your account turned on (takes less than an hour usually). The link is a bit obscure on the Rackspace site, so here it is:

https://sendgrid.com/products/rackspace

You'll get provisioned more quickly if the email address domain used is the same as the domain of the website that you registered with them. Also note that while SendGrid supports sub-accounts in the event that you want to manage multiple domains, this is not available with the plan provided via the Rackspace partnership...for this you have to upgrade.

Step #2: Set up a non-root user

If you've not done this already, you need to create a user account other than root to use when administrating the server. The "why" is beyond the scope of this post; just do it!

Create the user:

adduser username

Add the user to the 'sudo' group:

adduser username sudo

Log out of root and log back in using your new account.

Step #3: Update the hostname

Determine the current hostname of the server:

hostname -f

It probably outputs whatever you named the server when you spun up the instance. You'll need to modify this so that it reflects the FQDN:

sudo vim /etc/hostname

Replace the existing value with the FQDN of the site (e.g. www.domain.com).

Next you'll need to update the hosts file to resolve the FQDN to your localhost.

sudo vim /etc/hosts

Note that you will want the FQDN to resolve to the static IP assigned to the site, not just to the usual 127.0.0.1, eg:

50.57.15.10 www.domain.com

Now reboot the server to make sure that the changes take effect:

sudo reboot

Step #4: Set up reverse DNS

Log in to the Cloud Control Panel and set up reverse DNS by following this guide: http://www.rackspace.com/knowledge_center/index.php/DNS_-_Creating_a_Rev...

Once that's done, let's validate the RDNS setting (the RDNS will take a bit to propagate so you may wish to save this validation for later).

If you've not done so recently, you'll need to update/upgrade currently installed packages:

sudo aptitude update

sudo aptitude upgrade

Make sure that the dnsutils packae is installed as we'll be using the dig command:

whatis dig

Your output should look as follows:

dig (1) - DNS lookup utility

If you get "nothing appropriate, then you need to install dnsutils:

sudo aptitude install dnsutils

To check the RDNS:

dig -x 208.75.84.20

You should see something like:

...

...

;; QUESTION SECTION:

;6.105.57.50.in-addr.arpa. IN PTR

;; ANSWER SECTION:

6.105.57.50.in-addr.arpa. 86400 IN PTR www.domain.com.

Again, you may need to wait until later to validate the RDNS; it's not crucial to continue but you'll definitely need it going when the site starts sending emails.

Step #5: Configure postfix and SASL

You'll need to modify the sasl_passwd file:

sudo vim /etc/postfix/sasl_passwd

Replace 'username:password' with the username and password you used when you created your Sendgrid account back in Step #1.

Recreate the SASL hash file:

sudo postmap hash:/etc/postfix/sasl_passwd

Assuming that the SendGrid account has been provisioned, you should be able to successfully test:

mail your@emailaddress.com

Fill in the required information, terminating the email with a period followed by a return/enter. Pop your email and you should see the message.

If not, the postfix log is quite helpful in determining why emails may not be sending; it can be found at /var/log/mail.log

Language like this indicates that SASL is not configured properly to use SendGrid:

SASL authentication failed; server smtp.sendgrid.net[174.36.32.204] said: 535 Authentication failed: Bad username / password

Step #6: Wrapping Up

You should be good to go. If you've not validated the RDNS entry then do that now. You'd also do well to set up an SPF record in the DNS zone file for your domain. SPF or Sender Protection Framework is yet another validation mechanism used to determine the legitimacy of incoming email. Instructions are beyond the scope of this post, but help can be found here:

http://openspf.org

http://www.google.com/support/a/bin/answer.py?answer=33786

These tutorials were helpful in putting this guide together:

http://www.symana.com/2010/12/setup-a-lamp-rackspace-cloud-server-runnin... by Matt of Symana

http://www.rackspace.com/knowledge_center/index.php/Preparing_the_Cloud_...