diff --git a/apps/mail/actions/send.ts b/apps/mail/actions/send.ts index f49044fe3b..ba0c2a5692 100644 --- a/apps/mail/actions/send.ts +++ b/apps/mail/actions/send.ts @@ -14,6 +14,7 @@ export async function sendEmail({ headers: additionalHeaders = {}, threadId, fromEmail, + draftId, }: { to: Sender[]; subject: string; @@ -24,6 +25,7 @@ export async function sendEmail({ bcc?: Sender[]; threadId?: string; fromEmail?: string; + draftId?: string; }) { if (!to || !subject || !message) { throw new Error('Missing required fields'); @@ -43,7 +45,7 @@ export async function sendEmail({ }, }); - await driver.create({ + const emailData = { subject, to, message, @@ -53,7 +55,13 @@ export async function sendEmail({ bcc, threadId, fromEmail, - }); + }; + + if (draftId) { + await driver.sendDraft(draftId, emailData); + } else { + await driver.create(emailData); + } return { success: true }; } diff --git a/apps/mail/app/api/driver/google.ts b/apps/mail/app/api/driver/google.ts index be6e3035a5..8af30acec5 100644 --- a/apps/mail/app/api/driver/google.ts +++ b/apps/mail/app/api/driver/google.ts @@ -832,6 +832,25 @@ export const driver = async (config: IConfig): Promise => { { threadIds, options }, ); }, + sendDraft: async (draftId: string, data: IOutgoingMessage) => { + return withErrorHandler( + 'sendDraft', + async () => { + const { raw } = await parseOutgoing(data); + await gmail.users.drafts.send({ + userId: 'me', + requestBody: { + id: draftId, + message: { + raw, + id: draftId, + }, + }, + }); + }, + { draftId, data }, + ); + }, getDraft: async (draftId: string) => { return withErrorHandler( 'getDraft', diff --git a/apps/mail/app/api/driver/types.ts b/apps/mail/app/api/driver/types.ts index 51310839f5..2d9c46295e 100644 --- a/apps/mail/app/api/driver/types.ts +++ b/apps/mail/app/api/driver/types.ts @@ -10,6 +10,7 @@ export interface IGetThreadResponse { export interface MailManager { get(id: string): Promise; create(data: IOutgoingMessage): Promise; + sendDraft(id: string, data: IOutgoingMessage): Promise; createDraft(data: any): Promise; getDraft: (id: string) => Promise; listDrafts: (q?: string, maxResults?: number, pageToken?: string) => Promise; diff --git a/apps/mail/components/create/create-email.tsx b/apps/mail/components/create/create-email.tsx index f4f4dee4b6..0331e0dc6f 100644 --- a/apps/mail/components/create/create-email.tsx +++ b/apps/mail/components/create/create-email.tsx @@ -354,7 +354,7 @@ export function CreateEmail({ // Use the selected from email or the first alias (or default user email) const fromEmail = selectedFromEmail || (aliases?.[0]?.email ?? userEmail); - await sendEmail({ + const emailData = { to: toEmails.map((email) => ({ email, name: email.split('@')[0] || email })), cc: showCc ? ccEmails.map((email) => ({ email, name: email.split('@')[0] || email })) @@ -366,7 +366,13 @@ export function CreateEmail({ message: messageContent, attachments: attachments, fromEmail: fromEmail, - }); + }; + + if (draftId) { + await sendEmail({ ...emailData, draftId }); + } else { + await sendEmail(emailData); + } // Track different email sending scenarios if (showCc && showBcc) { @@ -398,6 +404,8 @@ export function CreateEmail({ setResetEditorKey((prev) => prev + 1); setHasUnsavedChanges(false); + // Clear the draftId after successful send + setDraftId(null); } catch (error) { console.error('Error sending email:', error); setIsLoading(false); @@ -806,7 +814,7 @@ export function CreateEmail({

-
+
{attachments.map((file, index) => (