使用Google Apps脚本,如何检查电子邮件是否附有pdf,然后转发到另一个电子邮件地址? [英] Using Google Apps script, how to check if an email has a pdf attached to it, then forward it to another email address?

查看:139
本文介绍了使用Google Apps脚本,如何检查电子邮件是否附有pdf,然后转发到另一个电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法创建了一个将电子邮件转换为pdf的函数,然后将其转发到另一个电子邮件地址(我使用了由Mike Greiling制作的这个伟大的库: https://github.com/pixelcog/gmail-to-pdf )。



但是现在我想创建另一个函数来检查电子邮件是否已经有附件,然后立即转发它。



这是我的工作函数:

  function saveExpenses(){

GmailUtils.processStarred('label:test',5,function(message){

//创建消息的PDF
var pdf = GmailUtils.messageToPdf(消息);

//在文件名前添加日期字符串
pdf.setName(GmailUtils.formatDate(message,'yyyy / MM / dd - ')+ pdf.getName());

//将确认电子邮件发送给原始发件人
var confirmationEmail = message.getFrom();

/ /获取要用作电子邮件主题行的文档名称。
var subject = pdf.getName();

var body =这是确认已收到此收据;

//发送一封确认电子邮件给发件人
GmailApp.sendEmail(confirmationEmail,subject,body,{attachments:[pdf]});

返回true;

});
}
}


解决方案

好的,我找到了解决方案,其实很简单。
我想我没有足够的想法,所以基本上我只是通过函数 getAttachments 获取消息中的所有附件,该函数返回一个<$ c $的数组然后我只是检查数组的长度是否大于0(这意味着电子邮件中有附件)
,如果结果为0,则意味着存在

这是我做的: = message.getAttachments();

if(attachment.length> 0){
//我添加代码来处理附件

} else if(attachment.length == 0){
//我添加了我在
上面的问题中发布的代码}


I managed to create a function that converts an email to a pdf and then it forwards it to another email address ( I used this great library made by Mike Greiling :https://github.com/pixelcog/gmail-to-pdf).

but now I want to create another function that checks if the email already has an attachment, and then forward it right away.

here's my working function:

function saveExpenses() {

    GmailUtils.processStarred( 'label: test', 5, function(message) {

    // create a pdf of the message
    var pdf = GmailUtils.messageToPdf(message);

    // prefix the pdf filename with a date string
    pdf.setName(GmailUtils.formatDate(message, 'yyyy/MM/dd - ') + pdf.getName());   

     // send confirmation email to the original sender 
     var confirmationEmail = message.getFrom();

     // Get the name of the document to use as an email subject line.
     var subject =  pdf.getName();

     var body = "This is a confirmation that this receipt has been sent";

     // Send a confirmation email to the sender
     GmailApp.sendEmail(confirmationEmail, subject, body,  {attachments: [pdf]});

    return true;

  });
 }
} 

解决方案

Ok, I found the solution, actually it was pretty easy. I guess I didn't think enough, so basically I just get all the attachments from the message with the function getAttachmentswhich returns an array of attachments, I then just check if the length of the array is greater than 0 ( which means there are attachments in the email ) and if the result is 0, it means there are no attachments.

Here is what I did :

var attachment = message.getAttachments();

    if (attachment.length > 0 ) {
          // I add the code to deal with the attachment

    } else if  (attachment.length == 0 ) {
          // I add the code that I posted in the question above
    }

这篇关于使用Google Apps脚本,如何检查电子邮件是否附有pdf,然后转发到另一个电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆