# Emails

Email Setup Documentation

Previously, we used the **Resend** service to handle emails. However, we moved away from it due to several issues:

* Emails often failed to reach the inbox folder due to problems with their SMTP servers.
* The free plan had significant limitations, such as allowing only one audience and restricting the number of contacts or emails.

To address these issues, we are pleased to introduce a **new custom email marketing system** integrated into our SaasCore boilerplate.

#### Benefits of the New System

* **Unlimited Emails**: Send as many emails as you want.
* **Unlimited Audiences**: Create unlimited audience groups.
* **Automation**: Send automated emails effortlessly.
* **Flexible SMTP Options**: Use any SMTP server you prefer, such as Zoho Mail, SendGrid, or even Resend (if desired).

***

#### Setup Instructions

**Using Zoho Mail for SMTP**

Personally, I use the free professional Zoho Mail plan to quickly create new emails for every SaaS project I launch.

Configuration for Zoho Mail:

```env
envCopy codeSMTP_HOST=smtp.zoho.com  
SMTP_PORT=465  
SMTP_SECURE=true  
SMTP_USER=  
SMTP_PASSWORD=  
```

To configure:

1. Enter your Zoho email and password into the `SMTP_USER` and `SMTP_PASSWORD` fields.
2. That's it! Your email setup is ready to go.

***

### Adding Audiences and Managing Emails

In the configuration file, there's a `customAudiences` property that allows you to manage audiences.

**Steps to Add a New Audience:**

1. During setup, create a new audience in the **Audiences** section of the admin panel.
2. Each audience created will have a unique ID. This ID is required to programmatically add emails to the specific audience.

Example configuration in the code:

```javascript
customAudiences: {
    potentialClientsAudienceId: "....", // ID for potential clients 
    newsletterAudienceId: "...", // ID for newsletter subscribers 
}
```

**Example: Adding Emails to "Potential Clients" Audience**

1. Create a new audience named "Potential Clients" in the admin panel.
2. Copy the unique audience ID and paste it into the configuration file under `potentialClientsAudienceId`.
3. When prospects submit their emails through the `emailsCollector` component, they will automatically be added to this audience using the `addToCustomAudience` utility function.

<figure><img src="https://1989858117-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgzjtImOJwwGv3VR3rBC7%2Fuploads%2FsNbPFhkqG1o6yclYyhcL%2FScreenshot%20-%2012_28_2024%20%2C%2010_04_34%20PM.png?alt=media&#x26;token=1506e892-eaef-4fd7-9050-78cf1896ad59" alt="" width="563"><figcaption></figcaption></figure>

Utility function implementation we use:

```javascript
export async function addToCustomAudience({
    email,
    customAudienceId,
}: {
    email: string;
    customAudienceId: string;
}) {
    try {
        // Check if the email already exists
        const existingContact = await prisma.customAudienceEmail.findUnique({
            where: {
                email_customAudienceId: {
                    email: email,
                    customAudienceId: customAudienceId,
                },
            },
        });

        if (existingContact) {
            return { error: "Email already exists" };
        }

        // Add the email to the audience
        await prisma.customAudienceEmail.create({
            data: {
                email,
                customAudienceId: customAudienceId,
            },
        });
        return { success: "Email added successfully" };
    } catch (error) {
        return { error: "Error adding email" };
    }
}
```

**Usage Example:**

We call the `addToCustomAudience` function as follows: (In the case of potential clients, this function is triggered whenever a client enters their email into the emails collector component.)

```javascript
 await addToCustomAudience({
    email: values.email,
    customAudienceId:
        config.marketing.customAudiences.potentialClientsAudienceId,
});
```

***

#### Unlimited Audiences and Flexibility

You can repeat this process to create and manage unlimited audiences. Simply add the audience ID to the configuration and use the same flow to manage emails efficiently.

If you have any questions about this setup, feel free to reach out via Discord or email.
