๐ Exciting Journey into Google Apps Script – GmailApp Class! ๐งโจ
๐ Exploring the dynamic capabilities of the GmailApp
class in Google Apps Script! ๐ Whether it’s sending personalized emails, managing drafts, or navigating through your inbox, the possibilities are endless.
๐ก Key Takeaways:
- Seamlessly interact with Gmail services.
- Automate email workflows with ease.
- Explore features like creating drafts, marking threads, and more.
Introduction to Google Apps Script – Gmail Service
Google Apps Script is a scripting language developed by Google to extend the capabilities of various Google Workspace applications, including Gmail. The Gmail service in Google Apps Script provides a way to automate tasks related to sending and receiving emails through your Gmail account.
GmailApp Class (Historical)
In the past, the GmailApp class was used for interacting with Gmail. It provided methods to compose and send emails, search for emails, and manage drafts. Here’s a brief overview:
Sending Emails:
- createDraft(recipient, subject, body): Creates a draft email with the given recipient, subject, and body.
- sendEmail(recipient, subject, body): Sends an email immediately.
Accessing Emails:
- getInboxThreads(): Retrieves a collection of threads in the inbox.
- getThreadById(threadId): Retrieves a specific thread by its ID.
- getInboxThreads(start, max): Retrieves a range of threads from the inbox.
Modifying Emails:
- markThreadRead(thread): Marks a thread as read.
- markThreadUnread(thread): Marks a thread as unread.
- moveThreadToArchive(thread): Moves a thread to the archive.
Working with Drafts:
- getDrafts(): Retrieves a collection of draft messages.
- getDraftById(draftId): Retrieves a specific draft by its ID.
Example:
function sendEmailExample() {
var recipient = ‘example@email.com’;
var subject = ‘Hello from Google Apps Script’;
var body = ‘This is a test email sent using Google Apps Script!’;
GmailApp.sendEmail(recipient, subject, body);
}
Coding Exercises: Gmail Automation with Google Apps Script
Exercise 1: Send Basic Email
Task: Write a Google Apps Script function to send a simple email.
Steps:
- Open Google Apps Script.
- Write a function to send an email.
- Run the function.
Code Example:
function sendBasicEmail() {
GmailApp.sendEmail(‘recipient@example.com’, ‘Subject’, ‘Body’);
}
Exercise 2: Send Email with HTML Body
Task: Modify the previous script to send an email with an HTML body.
Steps:
- Update the function to use HTML content.
- Run the function.
Code Example:
function sendHtmlEmail() {
var htmlBody = ‘<p>This is an <b>HTML</b> email.</p>’;
GmailApp.sendEmail(‘recipient@example.com’, ‘HTML Email’, ”, { htmlBody: htmlBody });
}
Exercise 3: Create Draft Email
Task: Write a script to create a draft email.
Steps:
- Write a function to create a draft.
- Run the function.
Code Example:
function createDraftEmail() {
var draft = GmailApp.createDraft(‘recipient@example.com’, ‘Draft Subject’, ‘Draft Body’);
Logger.log(‘Draft URL: ‘ + draft.getMessage().getId());
}
Exercise 4: Access Inbox Threads
Task: Retrieve and log the first few threads from the inbox.
Steps:
- Write a function to get inbox threads.
- Log thread details.
- Run the function.
Code Example:
function accessInboxThreads() {
var threads = GmailApp.getInboxThreads(0, 5);
threads.forEach(function (thread) {
Logger.log(‘Thread Subject: ‘ + thread.getFirstMessageSubject());
});
}
Exercise 5: Mark Thread as Read
Task: Write a script to mark the first thread in the inbox as read.
Steps:
- Write a function to mark a thread as read.
- Run the function.
Code Example:
function markThreadAsRead() {
var thread = GmailApp.getInboxThreads(0, 1)[0];
thread.markRead();
}
Exercise 6: Move Thread to Archive
Task: Move the first thread in the inbox to the archive.
Steps:
- Write a function to move a thread to the archive.
- Run the function.
Code Example:
function moveThreadToArchive() {
var thread = GmailApp.getInboxThreads(0, 1)[0];
thread.moveToArchive();
}
Exercise 7: Access Drafts
Task: Retrieve and log the details of the first few drafts.
Steps:
- Write a function to get drafts.
- Log draft details.
- Run the function.
Code Example:
function accessDrafts() {
var drafts = GmailApp.getDrafts();
drafts.forEach(function (draft) {
Logger.log(‘Draft Subject: ‘ + draft.getMessage().getSubject());
});
}
Exercise 8: Access Draft by ID
Task: Retrieve and log details of a specific draft by its ID.
Steps:
- Write a function to get a draft by ID.
- Log draft details.
- Run the function.
Code Example:
function accessDraftById() {
var draftId = ‘1234567890abcdef’; // Replace with an actual draft ID
var draft = GmailApp.getDraftById(draftId);
Logger.log(‘Draft Subject: ‘ + draft.getMessage().getSubject());
}
Exercise 9: Mark Draft as Unread
Task: Mark the first draft as unread.
Steps:
- Write a function to mark a draft as unread.
- Run the function.
Code Example:
function markDraftAsUnread() {
var draft = GmailApp.getDrafts()[0];
draft.markUnread();
}
Exercise 10: Send Email with Attachment
Task: Modify the email sending function to include an attachment.
Steps:
- Update the email sending function to include an attachment.
- Run the function.
Code Example:
function sendEmailWithAttachment() {
var file = DriveApp.getFileById(‘abcdef1234567890’); // Replace with an actual file ID
GmailApp.sendEmail(‘recipient@example.com’, ‘Email with Attachment’, ‘Body’, { attachments: [file] });
}
Note: Replace placeholders like ‘recipient@example.com’, ‘abcdef1234567890’, etc., with actual values.
Quiz Questions on GmailApp Class
- Question: What is the primary purpose of the GmailApp class in Google Apps Script?
- A) Creating spreadsheets
- B) Managing Gmail accounts
- C) Interacting with Gmail services
- Question: How can you send an email using the GmailApp class?
- A) sendMail()
- B) createEmail()
- C) sendEmail()
- Question: What is the purpose of the createDraft() method?
- A) Sends an email immediately
- B) Creates a draft email
- C) Deletes a draft email
- Question: How do you retrieve all drafts using the GmailApp class?
- A) getDrafts()
- B) retrieveDrafts()
- C) listDrafts()
- Question: What method is used to access threads in the inbox?
- A) getThreads()
- B) inboxThreads()
- C) getInboxThreads()
- Question: How can you mark a thread as read?
- A) markAsRead()
- B) markThreadRead()
- C) readThread()
- Question: What does moveToArchive() do in the GmailApp class?
- A) Deletes a thread
- B) Moves a thread to the archive
- C) Archives a thread
- Question: Which method is used to mark a draft as unread?
- A) markAsUnread()
- B) markDraftUnread()
- C) draftUnread()
- Question: How do you retrieve the subject of a draft?
- A) draft.getSubject()
- B) getDraftSubject()
- C) draft.subject()
- Question: What is the purpose of the createDraft() method’s return value?
- A) The draft’s ID
- B) The draft’s body
- C) The draft’s subject
- Question: How do you send an email with an HTML body?
- A) sendHtmlEmail()
- B) sendEmailWithHtmlBody()
- C) sendEmail() with HTML options
- Question: What is the purpose of getThreadById(threadId)?
- A) Retrieves a specific thread by ID
- B) Retrieves all threads in the inbox
- C) Retrieves the last thread
- Question: Which method retrieves the first message subject of a thread?
- A) thread.getFirstMessageSubject()
- B) getThreadSubject()
- C) firstMessageSubject()
- Question: How do you mark a draft as read using its ID?
- A) markDraftRead(draftId)
- B) getDraftById(draftId).markRead()
- C) readDraft(draftId)
- Question: What method is used to get the recipients of an email?
- A) getEmailRecipients()
- B) getRecipients()
- C) getToRecipients()
- Question: How do you access the body of the last received email?
- A) getLastEmailBody()
- B) getLatestEmailBody()
- C) GmailApp.getInboxThreads()[0].getMessages()[0].getBody()
- Question: What is the purpose of markThreadUnread(thread)?
- A) Marks the thread as unread
- B) Retrieves unread threads
- C) Unmarks the thread as read
- Question: Which method is used to send an email with attachments?
- A) sendEmailWithAttachments()
- B) sendEmail() with attachments option
- C) createEmailWithAttachments()
- Question: How do you retrieve the subject of the last draft?
- A) getLastDraftSubject()
- B) getLatestDraftSubject()
- C) GmailApp.getDrafts()[0].getSubject()
- Question: What is the purpose of getInboxThreads(start, max)?
- A) Retrieves threads starting from a specific position
- B) Retrieves all threads in the inbox
- C) Retrieves the last thread
- Question: How can you delete a draft using its ID?
- A) deleteDraftById(draftId)
- B) getDraftById(draftId).delete()
- C) delete(draftId)
- Question: Which method is used to mark a thread as unread?
- A) thread.markUnread()
- B) markThreadUnread(thread)
- C) unreadThread(thread)
- Question: What is the purpose of markDraftUnread()?
- A) Marks the draft as unread
- B) Retrieves unread drafts
- C) Unmarks the draft as read
- Question: How do you get the attachments of a specific email?
- A) getEmailAttachments(email)
- B) getAttachments(email)
- C) email.getAttachments()
- Question: What method is used to access the sender of an email?
- A) getSender()
- B) getEmailSender()
- C) getFrom()
- Question: How do you move the last received email to the trash?
- A) moveToTrash(getLastEmail())
- B) GmailApp.getInboxThreads()[0].moveToTrash()
- C) moveToTrash(getLastEmailThread())
- Question: What is the purpose of getDrafts()?
- A) Retrieves all drafts
- B) Retrieves unread drafts
- C) Retrieves the last draft
- Question: How can you mark a specific draft as read?
- A) markDraftRead(draft)
- B) getDraftById(draftId).markRead()
- C) readDraft(draft)
- Question: What method is used to access the date of the last sent email?
- A) getLastSentEmailDate()
- B) getLatestSentEmailDate()
- C) GmailApp.getSentMessages()[0].getDate()
- Question: How do you retrieve the plain text body of the last received email?
- A) getLatestEmailPlainTextBody()
- B) GmailApp.getInboxThreads()[0].getMessages()[0].getPlainBody()
- C) getLastEmail().getPlainTextBody()
Answers to Quiz Questions:
- C) Interacting with Gmail services
- C) sendEmail()
- B) Creates a draft email
- A) getDrafts()
- C) getInboxThreads()
- B) markThreadRead()
- B) Moves a thread to the archive
- A) markAsUnread()
- A) draft.getSubject()
- A) The draft’s ID
- C) sendEmail() with HTML options
- A) Retrieves a specific thread by ID
- A) thread.getFirstMessageSubject()
- B) getDraftById(draftId).markRead()
- B) getRecipients()
- C) GmailApp.getInboxThreads()[0].getMessages()[0].getBody()
- A) Marks the thread as unread
- B) sendEmail() with attachments option
- C) GmailApp.getDrafts()[0].getSubject()
- A) Retrieves threads starting from a specific position
- B) getDraftById(draftId).delete()
- B) markThreadUnread(thread)
- A) Marks the draft as unread
- C) email.getAttachments()
- C) getFrom()
- B) GmailApp.getInboxThreads()[0].moveToTrash()
- A) Retrieves all drafts
- A) markDraftRead(draft)
- C) GmailApp.getSentMessages()[0].getDate()
- B) GmailApp.getInboxThreads()[0].getMessages()[0].getPlainBody()