Skip to content

Commit 282345b

Browse files
committed
fix(emails): fix base URL
1 parent 5febdfb commit 282345b

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

apps/sim/app/api/help/route.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import { Resend } from 'resend'
33
import { z } from 'zod'
44
import { env } from '@/lib/env'
55
import { createLogger } from '@/lib/logs/console-logger'
6-
import { getBaseDomain } from '@/lib/urls/utils'
6+
import { getEmailDomain } from '@/lib/urls/utils'
77

88
const resend = env.RESEND_API_KEY ? new Resend(env.RESEND_API_KEY) : null
99
const logger = createLogger('HelpAPI')
1010

11-
// Define schema for validation
1211
const helpFormSchema = z.object({
1312
email: z.string().email('Invalid email address'),
1413
subject: z.string().min(1, 'Subject is required'),
@@ -99,8 +98,8 @@ ${message}
9998

10099
// Send email using Resend
101100
const { data, error } = await resend.emails.send({
102-
from: `Sim Studio <noreply@${getBaseDomain()}>`,
103-
to: [`help@${getBaseDomain()}`],
101+
from: `Sim Studio <noreply@${getEmailDomain()}>`,
102+
to: [`help@${getEmailDomain()}`],
104103
subject: `[${type.toUpperCase()}] ${subject}`,
105104
replyTo: email,
106105
text: emailText,
@@ -122,7 +121,7 @@ ${message}
122121
// Send confirmation email to the user
123122
await resend.emails
124123
.send({
125-
from: `Sim Studio <noreply@${getBaseDomain()}>`,
124+
from: `Sim Studio <noreply@${getEmailDomain()}>`,
126125
to: [email],
127126
subject: `Your ${type} request has been received: ${subject}`,
128127
text: `
@@ -137,8 +136,8 @@ ${images.length > 0 ? `You attached ${images.length} image(s).` : ''}
137136
138137
Best regards,
139138
The Sim Studio Team
140-
`,
141-
replyTo: `help@${getBaseDomain()}`,
139+
`,
140+
replyTo: `help@${getEmailDomain()}`,
142141
})
143142
.catch((err) => {
144143
logger.warn(`[${requestId}] Failed to send confirmation email`, err)

apps/sim/app/api/workspaces/invitations/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { WorkspaceInvitationEmail } from '@/components/emails/workspace-invitati
77
import { getSession } from '@/lib/auth'
88
import { env } from '@/lib/env'
99
import { createLogger } from '@/lib/logs/console-logger'
10-
import { getBaseDomain } from '@/lib/urls/utils'
10+
import { getEmailDomain } from '@/lib/urls/utils'
1111
import { db } from '@/db'
1212
import { user, workspace, workspaceInvitation, workspaceMember } from '@/db/schema'
1313

@@ -229,7 +229,7 @@ async function sendInvitationEmail({
229229
}
230230

231231
await resend.emails.send({
232-
from: `noreply@${getBaseDomain()}`,
232+
from: `noreply@${getEmailDomain()}`,
233233
to,
234234
subject: `You've been invited to join "${workspaceName}" on Sim Studio`,
235235
html: emailHtml,

apps/sim/lib/auth.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { createLogger } from '@/lib/logs/console-logger'
1717
import { db } from '@/db'
1818
import * as schema from '@/db/schema'
1919
import { env } from './env'
20-
import { getBaseDomain } from './urls/utils'
20+
import { getEmailDomain } from './urls/utils'
2121

2222
const logger = createLogger('Auth')
2323

@@ -147,7 +147,7 @@ export const auth = betterAuth({
147147
const html = await renderPasswordResetEmail(username, url)
148148

149149
const result = await resend.emails.send({
150-
from: `Sim Studio <team@${getBaseDomain()}>`,
150+
from: `Sim Studio <team@${getEmailDomain()}>`,
151151
to: user.email,
152152
subject: getEmailSubject('reset-password'),
153153
html,
@@ -189,7 +189,7 @@ export const auth = betterAuth({
189189

190190
// In production, send an actual email
191191
const result = await resend.emails.send({
192-
from: `Sim Studio <onboarding@${getBaseDomain()}>`,
192+
from: `Sim Studio <onboarding@${getEmailDomain()}>`,
193193
to: data.email,
194194
subject: getEmailSubject(data.type),
195195
html,
@@ -1144,7 +1144,7 @@ export const auth = betterAuth({
11441144
)
11451145

11461146
await resend.emails.send({
1147-
from: `Sim Studio <team@${getBaseDomain()}>`,
1147+
from: `Sim Studio <team@${getEmailDomain()}>`,
11481148
to: invitation.email,
11491149
subject: `${inviterName} has invited you to join ${organization.name} on Sim Studio`,
11501150
html,

apps/sim/lib/mailer.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Resend } from 'resend'
22
import { createLogger } from '@/lib/logs/console-logger'
33
import { env } from './env'
4-
import { getBaseDomain } from './urls/utils'
4+
import { getEmailDomain } from './urls/utils'
55

66
interface EmailOptions {
77
to: string
@@ -42,7 +42,7 @@ export async function sendEmail({
4242
from,
4343
}: EmailOptions): Promise<SendEmailResult> {
4444
try {
45-
const senderEmail = from || `noreply@${getBaseDomain()}`
45+
const senderEmail = from || `noreply@${getEmailDomain()}`
4646

4747
if (!resend) {
4848
logger.info('Email not sent (Resend not configured):', {
@@ -90,15 +90,14 @@ export async function sendBatchEmails({
9090
emails,
9191
}: BatchEmailOptions): Promise<BatchSendEmailResult> {
9292
try {
93-
const senderEmail = `noreply@${getBaseDomain()}`
93+
const senderEmail = `noreply@${getEmailDomain()}`
9494
const results: SendEmailResult[] = []
9595

9696
if (!resend) {
9797
logger.info('Batch emails not sent (Resend not configured):', {
9898
emailCount: emails.length,
9999
})
100100

101-
// Create mock results for each email
102101
emails.forEach(() => {
103102
results.push({
104103
success: true,
@@ -115,16 +114,13 @@ export async function sendBatchEmails({
115114
}
116115
}
117116

118-
// Prepare emails for batch sending
119117
const batchEmails = emails.map((email) => ({
120118
from: `Sim Studio <${email.from || senderEmail}>`,
121119
to: email.to,
122120
subject: email.subject,
123121
html: email.html,
124122
}))
125123

126-
// Send batch emails (maximum 100 per batch as per Resend API limits)
127-
// Process in chunks of 50 to be safe
128124
const BATCH_SIZE = 50
129125
let allSuccessful = true
130126

apps/sim/lib/urls/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,17 @@ export function getBaseDomain(): string {
3939
}
4040
}
4141
}
42+
43+
/**
44+
* Returns the domain for email addresses, stripping www subdomain for Resend compatibility
45+
* @returns The email domain (e.g., 'simstudio.ai' instead of 'www.simstudio.ai')
46+
*/
47+
export function getEmailDomain(): string {
48+
try {
49+
const baseDomain = getBaseDomain()
50+
return baseDomain.startsWith('www.') ? baseDomain.substring(4) : baseDomain
51+
} catch (_e) {
52+
const isProd = process.env.NODE_ENV === 'production'
53+
return isProd ? 'simstudio.ai' : 'localhost:3000'
54+
}
55+
}

0 commit comments

Comments
 (0)