Reputation: 11
i working with Gmail in Nextjs 15, i can get all my email, sending email, but my reply has a problem.when i reply a mail, in my mail conversation i can fetch every previous mail which is fine. but the reciever is having a new mail. not in the same conversation we having
Here is my code
import { google } from 'googleapis';
export async function POST(req) {
const { threadId, messageId, to, replyText, accessToken, from } = await req.json();
const auth = new google.auth.OAuth2();
auth.setCredentials({ access_token: accessToken });
const gmail = google.gmail({ version: 'v1', auth });
const rawMessage = [
`From: ${from}`,
`To: ${to}`,
`In-Reply-To: ${messageId}`,
`References: ${messageId}`,
`MIME-Version: 1.0`,
`Content-Type: text/plain; charset=UTF-8`,
``,
replyText,
].join('\n');
const encodedMessage = Buffer.from(rawMessage).toString('base64');
try {
const res = await gmail.users.messages.send({
userId: 'me',
requestBody: {
raw: encodedMessage,
threadId: threadId,
},
})
.then((r) => {console.log(r)})
.catch((err) => { console.log(err) });
return new Response(JSON.stringify({ status: 'Reply sent successfully' }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}
i tried to console.log every thing and it fine. should my await gmail.users.messages.send add some fields or something so my reciever could in the same conversation mail please help me.
i expect the reciever and i in the same conversation mail. the mail i reply in my conversation, but the reciever get a new mail
Upvotes: 1
Views: 61
Reputation: 102
Changes: wrap the messageID, pass the threadID explicitly, add the base64 encoding
import { google } from 'googleapis';
export async function POST(req) {
const { threadId, messageId, to, replyText, accessToken, from } = await req.json();
const auth = new google.auth.OAuth2();
auth.setCredentials({ access_token: accessToken });
const gmail = google.gmail({ version: 'v1', auth });
const rawMessage = [
`From: ${from}`,
`To: ${to}`,
`Subject: Re: Your Subject Here`, // including the subject
`In-Reply-To: <${messageId}>`, // add angle brackets to prevent from detecting as plain text
`References: <${messageId}>`,
`MIME-Version: 1.0`,
`Content-Type: text/plain; charset=UTF-8`,
``,
replyText,
].join('\n');
const encodedMessage = Buffer.from(rawMessage)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
try {
await gmail.users.messages.send({
userId: 'me',
requestBody: {
raw: encodedMessage,
threadId: threadId, // to ensure msg belongs to correct thread
},
});
return new Response(JSON.stringify({ status: 'Reply sent successfully' }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}
Upvotes: 0