* docs: migrate ui docs to docs universe * created yarn workspace * added eslint and tsconfig configurations * fix eslint configurations * fixed eslint configurations * shared tailwind configurations * added shared ui package * added more shared components * migrating more components * made details components shared * move InlineCode component * moved InputText * moved Loading component * Moved Modal component * moved Select components * Moved Tooltip component * moved Search components * moved ColorMode provider * Moved Notification components and providers * used icons package * use UI colors in api-reference * moved Navbar component * used Navbar and Search in UI docs * added Feedback to UI docs * general enhancements * fix color mode * added copy colors file from ui-preset * added features and enhancements to UI docs * move Sidebar component and provider * general fixes and preparations for deployment * update docusaurus version * adjusted versions * fix output directory * remove rootDirectory property * fix yarn.lock * moved code component * added vale for all docs MD and MDX * fix tests * fix vale error * fix deployment errors * change ignore commands * add output directory * fix docs test * general fixes * content fixes * fix announcement script * added changeset * fix vale checks * added nofilter option * fix vale error
6.3 KiB
description, addHowToData
| description | addHowToData |
|---|---|
| Learn how to send a confirmation email to the customer after they sign up. This guide uses SendGrid as an example Notification provider. | true |
How to Send Sign Up Confirmation Email
In this document, you’ll learn how to send a confirmation email to the customer after they sign up.
Overview
When a customer registers, the event customer.created is triggered. You can then listen to this event in a subscriber to perform an action, such as send the customer a confirmation email.
This guide will explain how to create the subscriber and how to use SendGrid to send the confirmation email. SendGrid is only used to illustrate how the process works, but you’re free to use any other notification service.
Prerequisites
Event Bus Module
The event bus module trigger the event to the listening subscribers. So, it’s required to have an event bus module installed and configured on your Medusa backend.
The Local Event Bus module works in a development environment. For production, it’s recommended to use the Redis Event Bus module.
Notification Provider
As mentioned in the overview, this guide illustrates how to send the email using SendGrid. If you intend to follow along, you must have the SendGrid plugin installed and configured.
You can also find other available Notification provider plugins in the Plugins directory, or create your own.
Step 1: Create the Subscriber
To subscribe to and handle an event, you must create a subscriber.
:::note
You can learn more about subscribers in the Subscribers documentation.
:::
Create the file src/subscribers/customer-confirmation.ts with the following content:
type InjectedDependencies = {
// TODO add necessary dependencies
}
class CustomerConfirmationSubscriber {
constructor(container: InjectedDependencies) {
// TODO subscribe to event
}
}
export default CustomerConfirmationSubscriber
You’ll be adding in the next step the necessary dependencies to the subscriber.
:::note
You can learn more about dependency injection in this documentation.
:::
Step 2: Subscribe to the Event
In this step, you’ll subscribe to the customer.created event to send the customer a confirmation email.
There are two ways to do this:
Method 1: Using the NotificationService
If the notification provider you’re using already implements the logic to handle this event, you can subscribe to the event using the NotificationService:
import { NotificationService } from "@medusajs/medusa"
type InjectedDependencies = {
notificationService: NotificationService
}
class CustomerConfirmationSubscriber {
constructor({ notificationService }: InjectedDependencies) {
notificationService.subscribe(
"customer.created",
"<NOTIFICATION_PROVIDER_IDENTIFIER>"
)
}
}
export default CustomerConfirmationSubscriber
Where <NOTIFICATION_PROVIDER_IDENTIFIER> is the identifier for your notification provider.
:::note
You can learn more about handling events with the Notification Service using this documentation.
:::
Method 2: Using the EventBusService
If the notification provider you’re using isn’t configured to handle this event, or you want to implement some other custom logic, you can subscribe to the event using the EventBusService:
import { Customer, EventBusService } from "@medusajs/medusa"
type InjectedDependencies = {
eventBusService: EventBusService
}
class CustomerConfirmationSubscriber {
constructor({ eventBusService }: InjectedDependencies) {
eventBusService.subscribe(
"customer.created",
this.handleCustomerConfirmation
)
}
handleCustomerConfirmation = async (data: Customer) => {
// TODO: handle event
}
}
export default CustomerConfirmationSubscriber
When using this method, you’ll have to handle the logic of sending the confirmation email to the customer inside the handler function, which in this case is handleCustomerConfirmation.
Step 3: Handle the Event
The handleCustomerConfirmation method receives a data object as a parameter which is a payload emitted when the event was triggered. This object is the entire customer object. So, you can find in it fields like first_name, last_name, email, and more.
In this method, you should typically send an email to the customer. You can place any content in the email, such as welcoming them to your store or thanking them for registering.
Example: Using SendGrid
For example, you can implement this subscriber to send emails using SendGrid:
import { Customer, EventBusService } from "@medusajs/medusa"
type InjectedDependencies = {
eventBusService: EventBusService,
sendgridService: any
}
class CustomerConfirmationSubscriber {
protected sendGridService: any
constructor({
eventBusService,
sendgridService,
}: InjectedDependencies) {
this.sendGridService = sendgridService
eventBusService.subscribe(
"customer.created",
this.handleCustomerConfirmation
)
}
handleCustomerConfirmation = async (data: Customer) => {
this.sendGridService.sendEmail({
templateId: "customer-confirmation",
from: "hello@medusajs.com",
to: data.email,
dynamic_template_data: {
// any data necessary for your template...
first_name: data.first_name,
last_name: data.last_name,
},
})
}
}
export default CustomerConfirmationSubscriber
Notice that you should replace the values in the object passed to the sendEmail method:
templateId: Should be the ID of your confirmation email template in SendGrid.from: Should be the from email.to: Should be the customer’s email.data: Should be an object holding any data that should be passed to your SendGrid email template.