Each preview environment (a development or staging environment) has its own SendGrid sub-user, so the 12,000 email credits per calendar month limit applies individually per environment, not as a shared pool across your project.
1. Turn on outgoing email
You can turn on outgoing email for each environment. By default, email is turned on for your Production environment and blocked for other environments. To turn it on for a specific environment, follow these steps:- In the Console
- Using the CLI
- Select the project.
- Select the environment from the Environment menu.
- Click Settings.
- On the General tab, select the Enable outgoing emails checkbox.
2. (Recommended) Improve deliverability
Improve deliverability of your email with Sender Policy Framework (SPF). If you don’t have an SPF record, add the followingTXT record to your domain’s DNS records:
TXT records isn’t supported due to rfc4408 section 3.1.2.
If you already have an SPF record, add SendGrid to your existing record.
3. (Optional) Validate your email
You can request DomainKeys Identified Mail (DKIM) validation for your domain. DKIM improves your delivery rate as an email sender. Learn more about how DKIM works. To have DKIM enabled for your domain:-
Open a support ticket and request the following, replacing
<SENDER_EMAIL_ADDRESS>with the address you send from:The DKIM domain might not be the same as your site domain (but it can be). DKIM needs to be generated on the domain of the<SENDER_EMAIL_ADDRESS>you provide above (the sender address), not necessarily your site’s domain. For example, if<SENDER_EMAIL_ADDRESS>isnoreply@media.yourdomain.com, request DKIM formedia.yourdomain.com, notyourdomain.com.Make sure you’ve sent at least one email before requesting DKIM. -
Add the
CNAMEandTXTrecords from the support team’s reply to your domain’s DNS configuration. The reply looks similar to the following:ThisTXTrecord replaces the generic one from step 2 — as part of DKIM setup, SendGrid provides a unique SPF record for your domain instead of the generic one. Checks for the expected DNS records run every 15 minutes. After SendGrid detects the records, it confirms them automatically — you don’t need to take any further action.SendGrid rotates DKIM keys on an internal schedule that it doesn’t publish, and offers no manual rotation option — the process is entirely transparent to you.
4. Test the email service
To test the email service, use the CLI to connect to your app by runningupsun ssh.
Run the following command:
5. Send email from your app
You can use/usr/sbin/sendmail on your app container to send emails as with the example in the previous step.
Or use the PLATFORM_SMTP_HOST environment variable in your SMTP configuration.
When outgoing emails are on, PLATFORM_SMTP_HOST is the address of the SMTP host that should be used.
When outgoing emails are off, the variable is empty.
When using PLATFORM_SMTP_HOST, send email through port 25 (often the default).
Your emails are proxied through the Upsun SMTP host and encrypted over port 465
before being sent to the outside world.
The precise way to send email depends on the language and framework you use.
The following examples show PHP and Java.
- PHP
- Java
To send email in PHP, you can use the built-in
mail() function.
The PHP runtime is configured to send email automatically with the correct configuration.
This works even for libraries such as PHPMailer, which uses the mail() function by default.The From header is required. Your email will not be sent if the header is missing.Beware of potential security problems when using the mail() function.
If you use any input from users in the $additional_headers or $additional_params parameters,
be sure to sanitize it first.