How do I send Email with WordPress hosted on Azure Websites?
Recently I moved my old MSDN Blog to the new DaveDev.net url running hosted WordPress on Azure WebSites. Setting up the new blog couldn’t have been more simple (hint – Click New – Compute – WebSites – Gallery – WordPress in the Azure Dashboard) and only took about five minutes.
The stuff that actually took the longest was customizing WordPress itself (themes, plugins, etc) , pointing the new Azure Website to a cname (davedev.azurewebsites.net to davedev.net) and then making sure I was backing up the MySQL Database. One of the areas that was not obvious to me however was how to send Emails.
Usually, if you sign up for WordPress from an online hoster or WordPress.com you will have an SMTP server set up for you which enables the wp_mail() function to automatically send Email on your behalf. This is not the case when you are running WordPress on Azure but the good news is there is a free solution via the Azure Marketplace.
The intent of this post is to walk you through the steps to get started with Email (it’s a free service too) on your WordPress Azure Website instance.
WordPress offers a crazy amount of customizations so the first step you need to do is to decide how you want to enable people to contact me on my Blog. After sifting through a bunch of contact form plugins I decided on one that would allow me to add a new tab and a clean contact form.
Here is what the Contact Tab looks like now on my Blog in a typical desktop browser:
Clicking on Contact will take the blog visitor to a clean contact form where they can enter their name, email I can each them at and then their message.
Once they click send they will get a response message if their Email has been sent correctly back to me.
If you have tried adding a similar contact plugin to your Azure Websites hosted WordPress instance this is where you will first encounter problems. Any call to send wp_mail() will result in an error as the Azure instance doesn’t know what SMTP server to use. I will cover that in a bit but first let’s walk through what the generated Email will look like.
You can easily apply filters now to the incoming Blog Emails and when you hit Reply to the Email it will be sent to the Email address the Blog visitor filled out in your Contact Form.
Setting things up
So how did I go about creating the Contact Form and enable Emails? The first step was to install the plugin I wanted to use for Blog Feedback Emails. I chose Contact Form 7 and then activated the Form as seen below.
The Contact Form will add a new menu option in your WordPress Dashboard called Contact. Opening it up will show a new form called “Contact form 1” with some code in it.
I copied the code from the Contact Form 1 and pasted it into a new Page for my Blog called “Contact”.
This is the Contact page you now see listed in my Blog’s Tab Menu. Now that I have a Contact Form Up and running the next step was to sign up for an SMTP Service that would send Emails on my behalf.
Setting Up SendGrid as a SMTP Service
In order for our Contact Page to send Emails we will need a SMTP Service. Luckily for us SendGrid offers just that and is part of the Azure Store.
Simply open up the Azure Dashboard, click New, select Store and then choose SendGrid from the App Services Add-ons list.
There is a completely free tier that will enable you to get (or blast out) up to 25,000 emails a month. That is more than enough for the purposes of a Feedback page on our Blog.
Like all products in the Azure Store billing is integrated with your Azure monthly Bill as is single sign on. I get complete status for the Sendgrid Service right from my Azure Dashboard and clicking on the SendGrid Name will take me to the SendGrid portal using my Azure credentials.
The only additional step needed is to verify your email address (which will be your Microsoft ID assigned to your Azure account). Once that is done be sure to copy over your SendGrid username and password right from the AppBar by clicking Connection Info.
You will also receive an Email from the Azure Store confirming your purchase of the SendGrid service using the free tier.
Setting Up WordPress to use a custom SMTP Service
Now that we have a Contact Form set up, and the SendGrid SMTP Service enabled we need to update our WordPress Blog so that any calls to the wp_mail() function will be routed to our new SMTP Service.
There are a bunch of free WordPress plugins that will allow you to do this I chose to go with WP-Mail-SMTP.
This plugin will add a new Email option in the Settings menu for your WordPress Dashboard. This is where you can set up your From and Name for the Email. Make sure you select SMTP for Wordpress emails, and then enter your SendGird information as seen below. Make sure you use the smtp.sendgrid.net SMTP Host name and specify a port. I used 587 based on the SendGrid recommendations.
That’s it! You can now send a Test message to yourself using the same settings page or go back to your Contact Form and send one from there. The only remaining recommendation I would have is to turn off new comment notifications inside your WordPress Dashboard. This is enabled by default but since my WordPress Azure Website instance could not send emails before I wasn’t getting spammed. Once the SMTP Host was set up I started to get notified every time a spam comment came in and oh my there are a lot of those with WordPress.