Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 10 additions & 2 deletions apps/mail/actions/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export async function sendEmail({
headers: additionalHeaders = {},
threadId,
fromEmail,
draftId,
}: {
to: Sender[];
subject: string;
Expand All @@ -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');
Expand All @@ -43,7 +45,7 @@ export async function sendEmail({
},
});

await driver.create({
const emailData = {
subject,
to,
message,
Expand All @@ -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 };
}
19 changes: 19 additions & 0 deletions apps/mail/app/api/driver/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,25 @@ export const driver = async (config: IConfig): Promise<MailManager> => {
{ 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',
Expand Down
1 change: 1 addition & 0 deletions apps/mail/app/api/driver/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface IGetThreadResponse {
export interface MailManager {
get(id: string): Promise<IGetThreadResponse>;
create(data: IOutgoingMessage): Promise<any>;
sendDraft(id: string, data: IOutgoingMessage): Promise<any>;
createDraft(data: any): Promise<any>;
getDraft: (id: string) => Promise<any>;
listDrafts: (q?: string, maxResults?: number, pageToken?: string) => Promise<any>;
Expand Down
14 changes: 11 additions & 3 deletions apps/mail/components/create/create-email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -806,7 +814,7 @@ export function CreateEmail({
</p>
</div>
<Separator />
<div className="touch-auto overflow-y-auto max-h-[40vh] overflow-x-hidden overscroll-contain px-1 py-1">
<div className="max-h-[40vh] touch-auto overflow-y-auto overflow-x-hidden overscroll-contain px-1 py-1">
<div className="grid grid-cols-2 gap-2">
{attachments.map((file, index) => (
<div
Expand Down