Payload Email Example
This example demonstrates how to integrate email functionality into Payload.
Quick Start
To spin up this example locally, follow these steps:
- Clone this repo
cdinto this directory and runyarnornpm installcp .env.example .envto copy the example environment variablesyarn devornpm run devto start the server and seed the databaseopen http://localhost:8000/adminto access the admin panel- Create your first user
How it works
Payload utilizes NodeMailer for email functionality. Once you add your email configuration to payload.init(), you send email from anywhere in your application just by calling payload.sendEmail({}).
- Navigate to
src/server.ts- this is where your email config gets passed to Payload - Open
src/email/transport.ts- here we are defining the email config. You can use an env variable to switch between the mock email transport and live email service.
Now we can start sending email!
- Go to
src/collections/Newsletter.ts- with anafterChangehook, we are sending an email when a new user signs up for the newsletter
Let's not forget our authentication emails...
- Auth-enabled collections have built-in options to verify the user and reset the user password. Open
src/collections/Users.tsand see how we customize these emails.
Speaking of customization...
- Take a look at
src/email/generateEmailHTMLand how it compiles a custom template when sending email. You change this to any HTML template of your choosing.
That's all you need, now you can go ahead and test out this repo by creating a new user or newsletter-signup and see the email integration in action.
Development
To spin up this example locally, follow the Quick Start.
Production
To run Payload in production, you need to build and serve the Admin panel. To do so, follow these steps:
- First invoke the
payload buildscript by runningyarn buildornpm run buildin your project root. This creates a./builddirectory with a production-ready admin bundle. - Then run
yarn serveornpm run serveto run Node in production and serve Payload from the./builddirectory.
Deployment
The easiest way to deploy your project is to use Payload Cloud, a one-click hosting solution to deploy production-ready instances of your Payload apps directly from your GitHub repo. You can also deploy your app manually, check out the deployment documentation for full details.
Resources
For more information on integrating email, check out these resources:
Questions
If you have any issues or questions, reach out to us on Discord or start a GitHub discussion.