Sending Emails from a Private Stack
Instances on platformOS send emails with email notifications, which are delivered through SendGrid. On a private stack, the emails are sent through your own SendGrid account, from domains you own, so that they are billed to you and visible in your SendGrid console.
This article explains what you need to provide, how platformOS uses your account, and how to verify the setup.
Choose how to connect your account
There are three levels at which a SendGrid account can be connected. Each instance uses the most specific one that is configured.
| Stack account | Partner Portal account | Account per instance | |
|---|---|---|---|
| Who configures it | The platformOS team, once for the stack | The platformOS team, once on the Partner Portal | You, in the configuration of each instance |
| Used by | Every instance on the stack that has no SendGrid configuration of its own | Instances on which you enable SendGrid in the Partner Portal | The instance you configure it on |
| SendGrid structure | One account for all instances | One subuser per instance, inside your account | Any account or subuser you choose |
| Best for | Most stacks: emails work out of the box for every new instance | Separate sending reputation, statistics and limits for each instance | An instance that must send through a specific account |
The Partner Portal account is configured on the Partner Portal, not on a single stack, so it is used for every instance that enables SendGrid through that Partner Portal, on any of the private stacks it manages. The Partner Portal also uses it to send its own notifications, for example invitations and alerts to your team.
The stack account and the Partner Portal account can be the same SendGrid API key.
What you need to provide
API key
platformOS connects to SendGrid with an API key:
| Credential | Format | Where to create it |
|---|---|---|
| SendGrid API key | Starts with SG., about 69 characters |
SendGrid console, Settings > API Keys > Create API Key |
Choose the permissions of the key based on where it is used:
- Stack account: Restricted Access with Mail Send is enough.
- Partner Portal account: the Partner Portal creates subusers and their API keys, so the key needs Full Access, or a restricted key that can manage subusers, IP addresses and API keys, in addition to Mail Send.
Warning
SendGrid shows an API key only once, when you create it. Copy it immediately, and share it with the platformOS team only through the secure channel agreed for your engagement, never in plain email or in a support ticket.
Sender domains
SendGrid only delivers emails from domains it can verify. For every domain your instances send from, the domain in the from address of your email notifications, complete domain authentication in SendGrid (Settings > Sender Authentication > Authenticate Your Domain) and add the DNS records it gives you. See Authenticating Your Domain on SendGrid for the steps.
If a domain is not authenticated, SendGrid refuses the email with an error such as 550 The from address does not match a verified Sender Identity, and the email is not sent.
Also tell the platformOS team which address the Partner Portal should send its own notifications from, for example [email protected]. Its domain must be authenticated too.
Account requirements
- A paid plan with enough monthly volume for all instances on the stack. The free plan has a low daily limit that is shared by all instances.
- For a Partner Portal account: a plan that supports subusers, and at least one IP address in your account. The Partner Portal assigns each new subuser the first IP address of your account.
How it works
Stack account
- You provide the API key. The platformOS team stores it in your stack's secrets store, from which the stack's application servers read it. It is never stored in the code or in an instance's configuration.
- Every instance on the stack sends its emails through SendGrid with this key, unless the instance has its own SendGrid configuration.
New instances can send emails as soon as they are created, as long as their sender domain is authenticated.
Partner Portal account
- You provide the API key. The platformOS team stores it in the secrets store of the Partner Portal that manages your private stacks.
- In the Partner Portal, open your instance, go to Integrations, and click Manage SendGrid.
- Enter an email address and a password for the instance's SendGrid subuser, and click Enable SendGrid Integration. The password must be at least 8 characters long and contain a letter and a number.
- In the background, the Partner Portal signs in to your account and:
- Creates a subuser named
instance-followed by the instance's ID, with the email address and password you entered. - Creates an API key for the subuser that can only send emails and read statistics.
- Saves the subuser's name and API key in the instance's configuration.
- Creates a subuser named
- After a few minutes, the SendGrid integration page of the instance says that SendGrid is set up. From then on, the instance's emails are sent through its subuser.
Because each instance has its own subuser, you can see each instance's emails, statistics and reputation separately in your SendGrid console, and suspend one instance without affecting the others. You can also sign in to SendGrid as the subuser with the email address and password you entered.
If SendGrid rejects a request, for example because your plan does not support subusers, the integration is not saved, and the person who enabled it receives an email with the error reported by SendGrid.
Account per instance
-
In the Partner Portal, open your instance, click Update Configuration, and then click Switch to edit mode.
-
Add or update the
sendgridsection underintegrations, withapikeyas the user name and your SendGrid API key as the password:{ "integrations": { "sendgrid": { "user_name": "apikey", "password": "SG.your_api_key" } } } -
Click Update Instance configuration. The instance's emails are sent with this key from now on.
Sending the emails
When an email notification is triggered, the instance renders the email and sends it to SendGrid with the account that applies to it. SendGrid then delivers it to the recipients.
Whether the email reaches its recipients depends on the stack:
- Production stacks deliver emails to their recipients.
- Staging stacks never deliver emails to their original recipients, so testing does not reach your users. Each email is redirected to the instance's test email address, with the original recipients and sender added to the subject, and is sent from a
no-replyaddress of the staging stack's domain. If the instance has no test email address, the email is not sent, and a message is written to the instance's logs.
Verify the setup
- Make sure the domain of the sender address is authenticated in SendGrid.
- Create an email notification on a production instance and send it to your own email address. On a staging stack, set a test email address first.
- Check that the email arrives from the expected sender, and that it is not marked as spam.
- In the SendGrid console, open Activity in the account (or the instance's subuser) and check that the email is listed as delivered.
If the email does not arrive, check the instance logs with pos-cli logs, followed by the name of your environment. Common causes are a sender domain that is not authenticated, an invalid recipient address, or a missing test email address on a staging stack.
Rotating the API key
- Stack account and Partner Portal account: create a new API key in SendGrid, send it to the platformOS team, and delete the old key only after they confirm that the new one is in use. Instances set up with the Partner Portal keep working, because they use their subusers' own keys.
- Account per instance: after creating a new key in SendGrid, update it in the instance's configuration, then delete the old key.