Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions apps/sim/tools/gmail/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,22 @@ function generateBoundary(): string {
return `----=_Part_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`
}

/**
* Encode a header value using RFC 2047 Base64 encoding if it contains non-ASCII characters.
* Email headers per RFC 2822 must be ASCII-only. Non-ASCII characters (emojis, accented
* characters, etc.) must be encoded as =?UTF-8?B?<base64>?= to avoid mojibake.
* @param value The header value to encode
* @returns The encoded header value, or the original if it's already ASCII
*/
export function encodeRfc2047(value: string): string {
// eslint-disable-next-line no-control-regex
if (/^[\x00-\x7F]*$/.test(value)) {
return value
}
const encoded = Buffer.from(value, 'utf-8').toString('base64')
return `=?UTF-8?B?${encoded}?=`
}
Comment thread
waleedlatif1 marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.

/**
* Encode string or buffer to base64url format (URL-safe base64)
* Gmail API requires base64url encoding for the raw message field
Expand Down Expand Up @@ -333,7 +349,7 @@ export function buildSimpleEmailMessage(params: {
emailHeaders.push(`Bcc: ${bcc}`)
}

emailHeaders.push(`Subject: ${subject || ''}`)
emailHeaders.push(`Subject: ${encodeRfc2047(subject || '')}`)

if (inReplyTo) {
emailHeaders.push(`In-Reply-To: ${inReplyTo}`)
Expand Down Expand Up @@ -380,7 +396,7 @@ export function buildMimeMessage(params: BuildMimeMessageParams): string {
if (bcc) {
messageParts.push(`Bcc: ${bcc}`)
}
messageParts.push(`Subject: ${subject || ''}`)
messageParts.push(`Subject: ${encodeRfc2047(subject || '')}`)

if (inReplyTo) {
messageParts.push(`In-Reply-To: ${inReplyTo}`)
Expand Down
Loading