Следующая ссылка на stackoverflow ( Google Scripts - Захват адреса электронной почты из возвращенного сообщения и синтаксический анализ ) дала очень хорошую информацию о том, как бороться с Bounces с помощью скрипта от Амита Агарвала (https://www.labnol.org/internet/gmail-bounced-email-report/29209/).
У меня вопрос, можно ли программно вызывать код? Я тщетно пытался поместить каждый из фрагментов кода в функции и вызывать их.
function RunReport() {
const findBouncedEmails_ = () => {
try {
const rows = [];
const {
messages = []
} = Gmail.Users.Messages.list("me", {
q: "from:mailer-daemon",
maxResults: 200
});
if (messages.length === 0) {
toast_("No bounced emails found in your mailbox!");
return;
}
toast_("Working..");
for (let m = 0; m < messages.length; m += 1) {
const bounceData = parseMessage_(messages[m].id);
if (bounceData) {
rows.push(bounceData);
}
if (rows.length % 10 === 0) {
toast_(`${rows.length} bounced emails found so far..`);
}
}
writeBouncedEmails_(rows);
} catch (error) {
toast_(error.toString());
}
};
}
Я также пытался скопировать код в свой проект, и строки с «const» показывают синтаксические ошибки и не удалось сохранить код в листе.
После ввода от { ссылка } сделал код, как показано ниже, который будет собирать дату, тему и адрес электронной почты возвращенного электронного письма и сохранять данные в лист Google. Разместите это, чтобы помочь другим.
function getBouncedEmails() {
// Create a sheet in your project "BouncedEmails" and add this code in the script.
var sheet = SpreadsheetApp.getActive().getSheetByName('BouncedEmails');
// Fetch all the bounced emails using a query
var query = "from:(mailer-daemon@google.com OR mailer-daemon@googlemail.com)";
//Get the most recent 500 bounced email messages
//Change the number according to your requirement
GmailApp.search(query, 0, 500).forEach(function(thread) {
thread.getMessages().forEach(function(message) {
if (message.getFrom().indexOf("mailer-daemon") !== -1) {
//get the emailAddress from the Header
var emailAddress = message.getHeader('X-Failed-Recipients');
//add a filter if you would like to write certain messages only
if(thread.getFirstMessageSubject() == "YOUR SUBJECT"){
// Save the data in Google Spreadsheet
sheet.appendRow([
thread.getLastMessageDate(),
thread.getFirstMessageSubject(),
emailAddress]);
}
}
});
});
}
При первом запуске вам необходимо предоставить разрешение на доступ к соответствующему ящику электронной почты Gmail. Всего наилучшего в кодировании.