Notification Emails with Mailgun

Notifications in ShipFast

ShipFast comes with built-in functionality for sending notifications in key user scenarios, helping to keep your users informed and engaged with your platform. Notifications are sent through email and can be easily customized to meet your branding and communication requirements.

Key Features of Notifications

1. Welcome Message on User Registration

When a user registers for the platform, ShipFast automatically sends a welcome message to notify them of their successful registration. This email serves as a confirmation of their account creation and provides an opportunity to establish initial engagement with the user.


How It Works

The welcome email is sent using the sendWelcomeNotification function, which is triggered by the Cloud Firestore Triggers onUserCreated. This trigger observes changes in the firestore users collection and executes the sendWelcomeNotification function whenever a new document is added to the collection (indicating a new user registration).

Below is the implementation of the sendWelcomeNotification function:

export const sendWelcomeNotification = async email => {
  const { subject, html } = generateWelcomeEmailTemplate(email);
  return sendEmail(email, subject, html);
};

File Path: /functions/shared/services/mailgun.js

Template Generation

The sendWelcomeNotification function uses generateWelcomeEmailTemplate to create the email's subject and HTML body. This allows for dynamic content generation tailored to the user.

Sending the Email

Once the email content is generated, the sendEmail function is used to dispatch the message to the user's email address.

Customizing the Welcome Email

To customize the email template further, you can edit the generateWelcomeEmailTemplate function. Ensure that the email content reflects your platform's style and values for a seamless user experience.


2. Invitation to Join Organization

ShipFast includes functionality for inviting users to join an organization within the platform. When an organization administrator invites a member, an invitation notification is sent to the recipient’s email. This notification contains:

  • A link to join the organization.
  • Instructions on how to complete the registration or login process.
  • Relevant information about the organization they are joining.

How It Works

The invitation process is managed by the sendMemberInvitation and sendAddMemberNotification functions. These functions handle email notifications based on the recipient's registration status:

  1. New Users: If the user_organizations document does not contain a value in the userId property, it means the user is not yet registered in the app. In this case, the system sends an invitation email using the sendMemberInvitation function.
  2. Existing Users: If the user_organizations document contains a value in the userId property, it indicates the user is already registered. In this case, the system sends a notification email using the sendAddMemberNotification function to inform them about the organization they are being added to.

Both functions are triggered by the Firestore Cloud Function onOrganizationMemberChange, which monitors changes to the user_organizations collection. Specifically, it is invoked when:

  • A new document is added to the user_organizations collection.
  • The invitation status (isInviteAccepted) or invitation URL (inviteUrl) in the document is updated.

Both functions are triggered by the Firestore Cloud Function onOrganizationMemberChange, which monitors changes to the user_organizations collection. Specifically, it is invoked when:

  • A new document is added to the user_organizations collection.
  • The invitation status (isInviteAccepted) or invitation URL (inviteUrl) in the document is updated.

Code Implementation

Below is the simplified implementation of the sendMemberInvitation function:

const sendMemberInvitation = async (
  email,
  invitationLink,
  organizationName
) => {
  const { subject, html } = generateInvitationTemplate(
    invitationLink,
    organizationName
  );
  return sendEmail(email, subject, html);
};

File Path: /functions/shared/services/mailgun.js

Below is the simplified implementation of the sendAddMemberNotification function:

const sendAddMemberNotification = async (email, organizationName) => {
  const { subject, html } =
    generateAddMemberNotificationTemplate(organizationName);
  return sendEmail(email, subject, html);
};

File Path: /functions/shared/services/mailgun.js

  • Parameters:
    • email: The recipient's email address.
    • invitationLink: A dynamically generated URL for the recipient to accept the invitation (only used in sendMemberInvitation).
    • organizationName: The name of the organization the recipient is being invited to or added to.

Customizing the Email Templates

  • To customize the invitation email, edit the generateInvitationTemplate function in functions/services/templates/email.js.

  • To customize the member notification email, edit the generateAddMemberNotificationTemplate function in the same file.

These templates allow you to modify the subject, body, and design of the emails to align with your branding and desired messaging.


By following these guidelines, you can effectively manage and personalize invitations and notifications for your platform, ensuring a smooth onboarding experience for your users.

3. Plan Switching or Cancellation Notifications

ShipFast supports notifications to inform users when they switch or cancel their subscription plans. These notifications are essential for maintaining transparency and ensuring users are aware of changes to their accounts.

Types of Notifications

  1. Plan Change Notification: Sent when a user upgrades, downgrades, or switches to a different plan. Includes details of the new plan, such as its price and features.
  2. Cancellation Notification: Sent when a user cancels their subscription, providing details about the effective end date of their plan.

Implementation Details

The following functions are responsible for handling subscription-related notifications:

  1. sendSubscriptionPlanSwitch
    Sends notifications for plan upgrades, downgrades, or immediate/scheduled plan changes.

  2. sendSubscriptionCancellation
    Sends notifications when a user cancels their subscription.

These functions are triggered by Cloud Firestore Triggers that monitor changes in the user's subscription plan (priceId) within the subscriptions collection. The triggers handle the following scenarios:

  • First-time Paid Plan Subscription: Triggered when a user subscribes to a paid plan for the first time.
  • Immediate Plan Change: Triggered when a user switches to a new plan immediately.
  • Scheduled Plan Change: Triggered when a user schedules a plan change, such as downgrading at the end of the billing cycle.
  • Cancellation: Triggered when a user cancels their subscription, effective at the end of the billing cycle.

Code Implementation

sendSubscriptionPlanSwitch Function

const sendSubscriptionPlanSwitch = async (
  email,
  oldPlan,
  newPlan,
  effectiveDate
) => {
  const { subject, html } = generateSubscriptionPlanSwitchTemplate(
    email,
    oldPlan,
    newPlan,
    effectiveDate
  );
  return sendEmail(email, subject, html);
};

File Path: /functions/shared/services/mailgun.js

Parameters:

  • email: Recipient's email address.
  • oldPlan: The user's current subscription plan before the change.
  • newPlan: The user's new subscription plan after the change.
  • effectiveDate: Date when the new plan becomes effective.

sendSubscriptionCancellation Function

const sendSubscriptionCancellation = async (email, oldPlan, oldPlanEndDate) => {
  const { subject, html } = generateSubscriptionCancellationTemplate(
    email,
    oldPlan,
    oldPlanEndDate
  );
  return sendEmail(email, subject, html);
};

File Path: /functions/shared/services/mailgun.js

Parameters:

  • email: Recipient's email address.
  • oldPlan: The subscription plan being canceled.
  • oldPlanEndDate: Date when the plan will officially end.

How It Works

  1. Plan Change Notification:

    • When the subscription plan (priceId) in the subscriptions document is updated, the system determines if the change is immediate or scheduled.
    • For immediate changes, the sendSubscriptionPlanSwitch function sends a notification with details of the new plan.
    • For scheduled changes, the notification includes the effective date of the plan change.
  2. Cancellation Notification:

    • If the user cancels their subscription, the sendSubscriptionCancellation function is invoked.
    • The notification includes details of the canceled plan and the effective end date.

Customization

To customize the email templates:

  • Plan Switch Notifications: Modify the generateSubscriptionPlanSwitchTemplate function located in functions/services/templates/email.js.
  • Cancellation Notifications: Modify the generateSubscriptionCancellationTemplate function located in the same directory.