Javascript 谷歌脚本跳转电子邮件
下面的stackoverflow链接()提供了关于如何使用Amit Agarwal at()的脚本处理反弹的非常好的信息 我的问题是,是否可以通过编程方式调用代码?我曾尝试将每个代码片段放入函数中并调用它们,但都是徒劳的Javascript 谷歌脚本跳转电子邮件,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,下面的stackoverflow链接()提供了关于如何使用Amit Agarwal at()的脚本处理反弹的非常好的信息 我的问题是,是否可以通过编程方式调用代码?我曾尝试将每个代码片段放入函数中并调用它们,但都是徒劳的 函数RunReport(){ const findBouncedEmails_u=()=>{ 试一试{ 常量行=[]; 常数{ 消息=[] }=Gmail.Users.Messages.list(“我”{ q:“发件人:mailer守护程序”, 最大结果:200 }); 如果(
函数RunReport(){
const findBouncedEmails_u=()=>{
试一试{
常量行=[];
常数{
消息=[]
}=Gmail.Users.Messages.list(“我”{
q:“发件人:mailer守护程序”,
最大结果:200
});
如果(messages.length==0){
吐司(你的邮箱里没有被退回的邮件!);
返回;
}
吐司(工作);
for(设m=0;m
我还尝试将代码复制到我的项目中,带有“const”的行显示语法错误,无法将代码保存在工作表中
输入后,生成如下代码,该代码将收集被退回电子邮件的日期、主题和电子邮件,并将数据存储在谷歌表单中。发布此消息以帮助他人
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邮箱的权限。最好的编码方式。以“编程方式”调用代码的一种方法是:
无论如何,问题中的代码很好,但它需要使用新的GoogleApps脚本运行时V8,因为它使用
let
和const
您正在函数“
RunReport()
”的内部声明函数“findbouncedmails”
而且
等等
你从来没这么说过。(根据您提供的代码)
解决方案:
如果要“以编程方式调用”的函数名为“RunReport
”,则一种方法是编写以下语句:
findBouncedEmails_u2;()代码>
在函数声明之后
注:
无需在RunReport
中声明findbouncedmails\uuu
更新:
正如@Rubén所指出的,编辑器接受为有效的语法取决于脚本的运行时,您可以在中看到语法的不兼容性。谢谢您的更正。但是,如何更改此箭头功能。我们应该搜索并找出答案。如果您能给出一些说明,这将有助于更改所有三个选项。在问题的代码中,尝试用函数()替换()=>
,
更新我的答案
function RunReport(){
findBouncedEmails_();
}