This Google Apps Script can get a label email and parse the content with regular expressions.
In this case, it is an email sent by google and the script will search by the label “alerts” then the result is parsed with regular expressions to find a title and a description.
The primary condition is to add the email address googlealerts-noreply@google.com for the alerts to the label alerts. If you don’t have label alerts then create one and add that email address.
This source code is permissive, you can add any email address attached to a label and parse the body content.
This is the source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | function parseBodyMail(body) { // this define the regular expressions for the title and description var titleRegex = /"title": "([^"]*)"/; var descriptionRegex = /"description": "([^"]*)"/; // extract the title and description from the body var titleMatch = body.match(titleRegex); var descriptionMatch = body.match(descriptionRegex); // get the actual value of the title and description the first group of the matches var title = titleMatch[1]; var description = descriptionMatch[1]; // return the title and description return { title: title, description: description }; } function readAlerts() { // get the list of all messages in the "alerts" label, // you can use any label set to googlealerts-noreply@google.com var messages = GmailApp.getMessagesForThreads(GmailApp.search("label:alerts")); // this iterate through the messages for (var i = 0; i < messages.length; i++) { var message = messages[i][0]; // Get the subject and body of the message var subject = message.getSubject(); var body = message.getBody(); // the object body is parsed var data = parseBodyMail(body); // this show values from data object Logger.log("Title : "+ data['title'] + " Description : " + data['description']); } } |
The result of the running source code is this:
1 2 3 | 6:53:52 PM Notice Execution started 6:53:57 PM Info Title : Google Alert - Daily Digest Description : Bite Solution 5: Get data from database, and query it with Pandas | Python mystery game. Curbal. Curbal. •. •. 224 views 20 hours ago. 6:53:53 PM Notice Execution completed |
This is an screenshot with the mail parsed by this source code: