this repo has no description
1// filename: ExportEmails.gs
2// Export all College/Auto emails to JSON for model training/testing
3
4const AUTO_LABEL_NAME = "College/Auto";
5const MAX_EMAILS_TO_EXPORT = 500; // Adjust as needed
6
7function exportEmailsToJSON() {
8 const autoLabel = GmailApp.getUserLabelByName(AUTO_LABEL_NAME);
9
10 if (!autoLabel) {
11 Logger.log(`Label "${AUTO_LABEL_NAME}" not found`);
12 return;
13 }
14
15 const threads = autoLabel.getThreads(0, MAX_EMAILS_TO_EXPORT);
16 Logger.log(`Found ${threads.length} threads to export`);
17
18 const emails = [];
19
20 threads.forEach((thread, idx) => {
21 try {
22 const msg = thread.getMessages().slice(-1)[0]; // Get latest message in thread
23
24 const email = {
25 thread_id: thread.getId(),
26 subject: msg.getSubject() || "",
27 from: msg.getFrom() || "",
28 to: msg.getTo() || "",
29 cc: msg.getCc() || "",
30 date: msg.getDate().toISOString(),
31 body: msg.getPlainBody() || "",
32 // Optional: include labels for context
33 labels: thread.getLabels().map(l => l.getName()),
34 is_in_inbox: thread.isInInbox()
35 };
36
37 emails.push(email);
38
39 if ((idx + 1) % 50 === 0) {
40 Logger.log(`Processed ${idx + 1}/${threads.length} threads`);
41 }
42 } catch (e) {
43 Logger.log(`Error processing thread ${thread.getId()}: ${e}`);
44 }
45 });
46
47 const output = {
48 exported_at: new Date().toISOString(),
49 total_count: emails.length,
50 label: AUTO_LABEL_NAME,
51 emails: emails
52 };
53
54 const json = JSON.stringify(output, null, 2);
55
56 // Log first 1000 chars to verify structure
57 Logger.log("JSON structure:");
58 Logger.log(json.slice(0, 1000));
59 Logger.log(`\nTotal JSON size: ${json.length} characters`);
60 Logger.log(`\nExported ${emails.length} emails`);
61
62 // Return the JSON string for manual copy/paste from logs
63 // OR save to Drive (see below)
64 return json;
65}
66
67// Optional: Save to Google Drive instead of logging
68function exportEmailsToDrive() {
69 const json = exportEmailsToJSON();
70
71 const fileName = `college_emails_export_${new Date().toISOString().slice(0,10)}.json`;
72 const file = DriveApp.createFile(fileName, json, MimeType.PLAIN_TEXT);
73
74 Logger.log(`Saved to Google Drive: ${fileName}`);
75 Logger.log(`File ID: ${file.getId()}`);
76 Logger.log(`Direct link: ${file.getUrl()}`);
77
78 return file.getUrl();
79}
80
81// Optional: Export in batches if you have a lot of emails
82function exportEmailsInBatches() {
83 const autoLabel = GmailApp.getUserLabelByName(AUTO_LABEL_NAME);
84 if (!autoLabel) {
85 Logger.log(`Label "${AUTO_LABEL_NAME}" not found`);
86 return;
87 }
88
89 const BATCH_SIZE = 100;
90 const MAX_BATCHES = 5; // Max 500 emails
91
92 for (let batch = 0; batch < MAX_BATCHES; batch++) {
93 const threads = autoLabel.getThreads(batch * BATCH_SIZE, BATCH_SIZE);
94
95 if (threads.length === 0) {
96 Logger.log(`No more threads. Exported ${batch} batches.`);
97 break;
98 }
99
100 const emails = threads.map(thread => {
101 const msg = thread.getMessages().slice(-1)[0];
102 return {
103 thread_id: thread.getId(),
104 subject: msg.getSubject() || "",
105 from: msg.getFrom() || "",
106 to: msg.getTo() || "",
107 cc: msg.getCc() || "",
108 date: msg.getDate().toISOString(),
109 body: msg.getPlainBody() || "",
110 labels: thread.getLabels().map(l => l.getName()),
111 is_in_inbox: thread.isInInbox()
112 };
113 });
114
115 const output = {
116 batch: batch + 1,
117 exported_at: new Date().toISOString(),
118 count: emails.length,
119 emails: emails
120 };
121
122 const json = JSON.stringify(output, null, 2);
123 const fileName = `college_emails_batch_${batch + 1}.json`;
124 const file = DriveApp.createFile(fileName, json, MimeType.PLAIN_TEXT);
125
126 Logger.log(`Batch ${batch + 1}: Saved ${emails.length} emails to ${fileName}`);
127 Logger.log(`Link: ${file.getUrl()}`);
128
129 // Small delay between batches
130 if (batch < MAX_BATCHES - 1) {
131 Utilities.sleep(1000);
132 }
133 }
134}