sofn-xyz / mailing

Build, test, send emails with React
https://www.mailing.run
MIT License
3.6k stars 74 forks source link

404 when using `buildSendMail`! #498

Open JeremyMeissner opened 1 month ago

JeremyMeissner commented 1 month ago

Describe the bug Something really weird is happening in my next api route.

If I use the following code I get a 404 like the route doesn't even exist, no console log is printed, no error is thrown, it's like if I was not calling the correct endpoint.

import nodemailer from "nodemailer";
import { buildSendMail } from "mailing-core";
import AccountCreated from "@/emails/AccountCreated";
import React from "react";

export async function POST(req: Request) {
    try {
        const transport = nodemailer.createTransport({
            host: 'smtp.sendgrid.net',
            port: 587,
            auth: {
                user: 'apikey',
                pass: process.env.SENDGRID_API_KEY,
            },
        });

        const sendMail = buildSendMail({
            transport,
            defaultFrom: "noreply@mysender.com",
            configPath: "./mailing.config.json",
        });

        sendMail({
            to: "iam@andireceive.com",
            subject: "hello",
            component: React.createElement(AccountCreated, { name: "Hello" }),
        });

        return Response.json({ success: true }, { status: 200 });
    }
    catch (error) {
        return Response.json({ success: false, error: error }, { status: 500 });
    }
}

BUT if I put comment the sendMail part like the following I get a perfect 200 and so console logs are printed, if I put error voluntary it's thrown and printed.

import nodemailer from "nodemailer";
import { buildSendMail } from "mailing-core";
import AccountCreated from "@/emails/AccountCreated";
import React from "react";

export async function POST(req: Request) {
    try {
        const transport = nodemailer.createTransport({
            host: 'smtp.sendgrid.net',
            port: 587,
            auth: {
                user: 'apikey',
                pass: process.env.SENDGRID_API_KEY,
            },
        });

        const sendMail = buildSendMail({
            transport,
            defaultFrom: "noreply@mysender.com",
            configPath: "./mailing.config.json",
        });

        // sendMail({
        //     to: "iam@andireceive.com",
        //     subject: "hello",
        //     component: React.createElement(AccountCreated, { name: "Hello" }),
        // });

        return Response.json({ success: true }, { status: 200 });
    }
    catch (error) {
        return Response.json({ success: false, error: error }, { status: 500 });
    }
}

What in the hell, in the buildSendMail function cause my next api route to 404? Is that a NextJS thing?

Feel free to ask any additional information if needed.

Versions nextjs 14.2.4 mailing-core 1.1.0 nodemailer 6.9.14 react 18.3.1