Google Apps Script - only forward email with pdf attachment

59 Views Asked by At

this is my google apps script, i found it here also and its working good. This looking for emails in the label "forCompany" and export the attachments and send an new email with the attachments.

so far so good ... but i need only the pdf to send, not attachments like jpg, png or much other, only the pdf i want sent.

// script start

var labelName = "forCompany";

function Send_Gmail_Attachments_To_Datev() {
initLabels();
var threads = getThreads();
var attachments = getAttachments(threads);
sendAttachments(attachments);
removeThreadsFromLabel(threads);
}

function getThreads(){
return getGmailLabel().getThreads();}

function getMessages(threads){
var res = new Array();
var c = 0;
for(var i in threads){
var messages = threads[i].getMessages();
for(var a in messages){
res[c++]=messages[a];}
}
return res;}

function getAttachments(threads){
var res = new Array();
var messages = getMessages(threads);
for(var i in messages){
var attachments = messages[i].getAttachments();
for(var a in attachments){
//Eliminate attachments with same name
res[attachments[a].getName()]=attachments[a];}
}
return res;}

function removeThreadsFromLabel(threads){
getGmailLabel().removeFromThreads(threads);}

function sendAttachments(attachments){
for(var n in attachments){
sendAttachment(attachments[n])}
}

function sendAttachment(blob){
return MailApp.sendEmail("[email protected]","","ready-upload",{attachments: [blob]});}

function getGmailLabel(){
return GmailApp.getUserLabelByName(labelName);}

function initLabels(){
var label = null;

try{
label = getGmailLabel();
}catch(e){
Logger.log(e.getCause());}

if(!label){
GmailApp.createLabel(labelName);}
}

// script end


i try some parts of other script but the most scripts export the content of the pdf to text and so on, this not help me :(

0

There are 0 best solutions below