N
N
Nicholas V2021-08-11 22:33:56
gmail
Nicholas V, 2021-08-11 22:33:56

How to extract the email address from the body of the letter and send a standard response to it?

Help with google script. It is necessary for incoming mail to stir up an auto-reply to the address taken from the text of the letter. The situation is as follows - mail (gmail) receives letters generated by a web form on the site. In these letters, the Name, Phone and Soap are indicated. It is necessary to take mail from the text and send a standard response to it ( something like - application accepted, thanks for contacting, we will contact you soon ). Tell me, if anyone can and knows how to implement this through Google Apps Script?
--
The text of incoming emails is something like this:

*Request details:*
Name: Тест
Email: [email protected]
Phone: +7 (999) 999-99-99

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas V, 2021-08-12
@Amunrah

In short, I solved the problem. I'll leave it here in case it's useful to someone.
In general, I made a plate with two identical sheets (New and Processed) of the form:
61154258cbce4938784054.png
And I wrote the following code:

var name, email, phone;
name = email = phone = 'None';

function getEmails(){
  var threads = GmailApp.search('label:Заявка');

  if (threads.length) {
    for (var i=0; i < threads.length; i++) {
      var messages = threads[i].getMessages();
      for (var j=0; j < messages.length; j++) {
        var message = messages[j];
        message.markRead(); 
        extractDetails(message);
      }
      getSheetData();
      threads[i].removeLabel(GmailApp.getUserLabelByName("Заявка"));
      threads[i].addLabel(GmailApp.getUserLabelByName("Обработано"));
    }    
  } else {}
}

function extractDetails(message){
  var dateTime = message.getDate();
  searchInBody(message.getPlainBody());
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName("Новые"));
  activeSheet.appendRow([dateTime, name, email, phone]);
}

function searchInBody(mes) {
  var lines, subline;
  lines = mes.split("\n");

  for (index = 0; index < lines.length; ++index) {
    if(lines[index].indexOf('Name:') + 1) {
      subline = lines[index].split(":");
      name = subline[1].toString().trim();
    }
    if(lines[index].indexOf('Email:') + 1) {
      subline = lines[index].split(":");
      email = subline[1].toString().trim();  
    }
    if(lines[index].indexOf('Phone:') + 1) {
      subline = lines[index].split(":");
      phone = subline[1].toString().trim().slice(1,);
    }
  }
}

function getSheetData() {
  var re = /\[email protected]\S+\.\S+/;
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName("Новые"));
  var data = sheet.getRange('A2:F').getValues();
  sheet.getRange('A2:F').clearContent();
  
  data.forEach(row => {
    if (row[2] !== '' && re.test(row[2])) {
      sendMail();
    } else {}
  })

  var sheet1 = SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName("Обработанные"));
  var row = data.length;
  var column = data[0].length;
  sheet1.getRange(sheet1.getLastRow()+1, 1, row, column).setValues(data);
}

function sendMail() {
  var html = 'Здравствуйте '+name+'!<br><br>Ваша заявка принята, наш специалист скоро свяжется с вами.<br><br>Спасибо за обращение.';  
  var recipientsTO = email;
  var recipientsCC = email;
  var Subject = "Ваша заявка принята";
  
  MailApp.sendEmail({
    to: recipientsTO,
    cc: recipientsCC,
    subject: Subject,
    htmlBody: html
  });
}

Gave him the necessary permissions and set the schedule. The code is damp and far from optimal, but so far everything works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question