Today I wanted to make sure a bunch of editors from one site existed as editors of a new staging site that we’re building out. Both sites exist as part of the same multisite network.
Thanks to WP-CLI and xargs, this is pretty straight forward:
wp user list --role=editor --url=prod.site.edu --field=user_login | xargs -n1 -I % wp --url=stage.site.edu user set-role % editor
This tells WP-CLI to list only the user_login field for all of the editors on prod.site.edu. It then passes this list via pipe to xargs, which runs another wp command that tells WP-CLI to set the role of each user as editor on stage.site.edu.
Because users are already “created” at the global level in multisite, they are added to other sites by setting their role with wp user set-role.
I’d estimate that with a list of 15 users, this probably saved closed to 15 minutes and didn’t require a whole bunch of clicking and typing with two browser windows open side by side.
A long, long, long time ago, sending email via your website was really horrible. Alongside the static HTML powering your Guestbook, you had some copy/pasted CGI script your ISP somehow allowed you to use that probably didn’t work, but oh crap it started working I hope it doesn’t break now.
A long, long time ago, sending email suddenly became easier. It kind of just worked accidentally. You install WordPress or another app on a shared host and you got emails when people left comments.
A while ago, things started to get hard again. When it’s easy for everyone to send email, it’s also really easy for people to send massive amounts of spam. So larger email clients got smart and started requiring things like DKIM and SPF to really guarantee mail delivery. Without these configured on your domain, you’re at the mercy of the algorithm. Thankfully, places like Digital Ocean had excellent documentation for configuring stuff like this and with a bit of elbow grease, you could get there on a $10/month Linode server.
But then it got super easy! Mandrill offered a free tier for transactional emails that had a limit nobody would reach with a standard blog/comment configuration. You could sign up for an account and use a wp_mail drop-in that handled the API interactions for you.
And so it goes. On to the next service, hopefully configured in a structure that’s prepared for long-term use.
It looks nice, I’ve seen it mentioned in a handful of conversations, the API seems straight forward, and I didn’t run into anything that made it hard to setup an account. I’m easy.
You get 25,000 free emails with Postmark. After that you pay a really reasonable rate. If you send a ton of emails, the rate gets more reasonable. I think this is a good model and they should probably even charge earlier because it’s going to take me a while to send 25,000 emails.
Once you sign up, Postmark is just as easy as Mandrill. There’s an official plugin that provides a settings screen for you to add your API key and a wp_mail replacement that handles the API calls. If you’re like me, you’ll skip the full plugin and grab only the wp_mail drop-in, add some static configuration, and toss it into mu-plugins.
As it should, Postmark requires that you add Sender Signatures for any email address from which you’ll be sending email. So before I can send email from firstname.lastname@example.org, I need to show that I can already receive email on that same address.
At this point, a normal person decides to enable email forwarding through their domain registrar or host. That is the easy way, but it was Saturday and I was looking for a party.
Receiving email through Amazon
Amazon has 9 million AWS services. It only takes 3 of them to solve the email receipt problem and not one involves setting up a server. The hardest part is keeping track of all the open tabs.
Amazon Simple Email Service (SES) was originally built to handle the sending of transactional emails. In September 2015, they added support for receiving email through the same service.
Amazon Simple Storage Service (S3) is a place to store things. In this case, it will be where we drop incoming emails to be processed.
Amazon’s AWS Lambda is the cool new kid on the block and allows for “serverless” computing. You define a function and are charged only for the computing time that the function actually uses.
To get through this, you’re going to need a verified AWS account and access to your domain’s DNS settings via whichever name server you use. I use DNSimple, which has made every DNS configuration in the last 5 years a pleasant experience. That’s an affiliate link even though it’s already only $7/month for me. 🍻
Let’s do it.
Configuring SES to receive and forward email
Go to SES via the Services menu your AWS Console and select Domains under Identity Management.
Click on Verify a New Domain at the top of the screen.
Enter the root of the domain you’re verifying, in my case chipconf.com, check the Generate DKIM Settings option, and click Verify This Domain.
You’ll be presented with an overlay containing the new records that need to be attached to your domain as part of your DNS configuration. Carefully enter all of these as any mistakes may add extra time to the verification process. I set all of mine with a 10 minute TTL so that any errors may resolve sooner.
A TXT record that acts as domain verification.
Three CNAME records for the DKIM record set, which SES rotates through automatically.
And an MX record to route incoming email on your domain to AWS.
Click Close on the DNS overlay. You’ll now need to be patient as the domain is verified. Amazon says this may take 72 hours, but it’s taken 5 minutes for 3 of my domains and 20 minutes for one where I had an error in the config at first. You’ll get a couple emails as soon as the verification goes through.
In the meantime, you’ll want to verify any email addresses that you will be forwarding email to. As part of the initial SES configuration, you’re locked in the Amazon SES sandbox and can only send emails to addresses you have verified ahead of time.
Select Email Addresses under Identity Management.
Click on Verify a New Email Address at the top of the screen.
Enter the address you’ll be forwarding mail to and click Verify This Email Address.
Once you receive an email from AWS, click on the link to complete the verification.
Note: You’re also limited to sending 200 messages every 24 hours and a maximum of one per second. Because transactional emails will be sent using Postmark, and only replies to those emails will become through SES, that shouldn’t be a huge deal. If you do reach that limit, you’ll need to request access for a sending limit increase for SES. If you think you’ll be receiving large volumes of email, you may want to also consider using SES for all of your transactional email (HumanMade has a plugin) and not use Postmark at all.
Ok, go back to Domains under Identity Management and check that the status for your domain is listed as verified. Once it is, we can continue. If you’re concerned that something isn’t working properly, use a command like dig to double check the TXT record’s response.
The first example returns nothing because it’s an invalid record. The second returns the expected value.
Note that I’m using ns1.dnsimple.com above. I can change that to ns2.dnsimple.com, ns3.dnsimple.com, etc… to verify that the record has saved properly throughout all my name servers. You should use your domain’s name server when checking dig.
Once domain verification has processed, click on Rule Sets under Email Receiving on the left.
Click on View Active Rule Set to view the default rule set. If a default rule set does not exist, create a new one.
Click Create Rule to create a receipt rule for this domain.
For recipient, enter the base of your domain (e.g. chipconf.com) rather than a full email address so that all addresses at that domain will match. Click Next Step.
Select S3 as the first action.
Choose Create S3 bucket in the S3 Bucket dropdown and enter a bucket name. Click Create Bucket.
Leave Object key prefix blank and Encrypt Message unchecked.
Choose Create SNS Topic in the SNS Topic dropdown and enter a Topic Name and Display Name. Click Create Topic.
Click Next Step. We’ll need to do some things before adding the Lambda function.
Give the rule a name, make sure Enabled is checked, Require TLS is unchecked, andEnable spam and virus scanning is checked. Click Next Step.
Review the details and click Create Rule.
Now head over to Lambda via the Services menu in the top navigation. Before completing the rule, we need to add the function used to forward emails that are stored in the S3 bucket to one of the verified email addresses.
Luckily, the hard legwork for this has already been done. We’ll be use the appropriately named and MIT licensed AWS Lambda SES Email Forwarder function. The README on that repository is worth reading as well, it provides more detail for the instructions involved with this section.
Click Create a Lambda function.
Click Skip on the next screen without selecting a blueprint.
Edit the defaultConfig object at the top of this file to reflect your configuration.
fromEmail should be something like email@example.com
emailBucket should be the name of the S3 bucket you created earlier.
emailKeyPrefix should be an empty string.
forwardMapping is used to configure one or more relationships between the incoming email address and the one the email is forwarded to. Use something like @chipconf.com as a catch-all for the last rule.
Leave Handler set to index.handler.
Select Basic Execution Role from the role list. A new window will appear to grant Lambda permissions to other AWS resources.
Choose Create a new IAM Role from the IAM Role drop down and provide a Role Name.
Click View Policy Document and then Edit to edit the policy document. Copy and paste the below policy document, also taken from the AWS Lambda SES Email Forwarder repository, into that text area. Make sure to change the S3 bucket name in that policy to match yours. In the below policy document, I replaced S3-BUCKET-NAME with chipconf-emails.
Click Allow. You should be transferred back to the Lambda screen.
Under Advanced Settings, set Memory to 128MB and Timeout to 10 seconds. You can leave VPC set to No VPC.
Review the new function details and click Create function.
Whew. Almost there.
Now head back to SES via the Services menu in the top navigation. We need to edit the rule set to use the new Lambda function.
Click Rule Sets under Email Receiving and then View Active Rule Set to see the existing rules.
Click on the name of the rule from the previous steps.
Select Lambda as an action type next to Add Action.
Select the new function you created next to Lambda function. Leave Event selected for the Invocation type. Leave None selected for SNS topic.
Click Save Rule.
A permissions overlay will appear to request access for SES to invoke the function on Lambda. Click Add permissions.
Now I can go back to Postmark and add firstname.lastname@example.org as a valid Sender Signature so that the server can use the Postmark API to send emails on behalf of email@example.com to any address.
If someone replies to one of those emails (or just sends one to firstname.lastname@example.org), it is now received by Amazon SES. The email is then processed and stored as an object in Amazon S3. SES then notifies Amazon Lambda, which fires the stored function used to process that email and forward it via SES to the mapped email address.
Now that you have 1800 words to guide you through the process, I’m going to dump a bunch of screenshots that may help provide some context. Feel free to leave a comment if one of these steps isn’t clear enough.
The pains people working on WordPress go to in making security, stored content, and backward compatibility all first priorities are amazing.
This is what I’ve learned and been inspired by the most since I started contributing to WordPress.
If there’s a moment in the course of development in which any of these three things are at risk, the tension or pain—or whatever you want to call it—is palpable. Providing a secure place for users to publish their content without worry is why we’re here.
It’s okay to feel offended, especially if you were burned. Know that good work was done to try and make the bandaid as easy to pull of as possible. Know that people have been working on or thinking through easing the burn every minute since.