I am adjusting an existing script that we have for a Google Form that sends a custom email to the desired address, now we want to add a PDF upload in the form and we want to add it to the script in order to be attached in the email. I know it shouldnt be difficult but so many years without programming and I can’t fix it. I will attach my code right now without the tests I am doing to add the PDF attached from the form so you can see the structure and maybe help me with the import:
function GetContactsAndMakeThemMultiChoise(){
// Copy your form ID from the browser address bar when you open the form in edit mode
var form = FormApp.openById('XXXXXXXXXXmyformXXXXXXX');
var formItems = form.getItems(FormApp.ItemType.LIST);
var arr=[]
//i<contacts.length; i++
var contacts = ContactsApp.getContacts();
for (var i=0; i<contacts.length; i++) {
//Access my contacts and get all contact name that not blank (name.length>0)
var name = contacts[i].getEmailAddresses();
if(name.length>0 ){
arr.push(name); }
}
// Add the contact name to the first dropdown list found in my form
formItems[3].asListItem().setChoiceValues(arr.sort());
}
// Installation --> copy this code in the inbound code from the form (open form --> three dots --> Scripteditor
// run function createTrigger() once
function createTrigger() {
ScriptApp.newTrigger("sendMail").forForm(FormApp.getActiveForm()).onFormSubmit().create();
}
function getInfo(e) {
var formResponse;
if (!e) { formResponse = FormApp.getActiveForm().getResponses()[FormApp.getActiveForm().getResponses().length - 1]; } else { formResponse = e.response; }
var answers = {};
formResponse.getItemResponses().forEach(function (r) {
if (r.getResponse() != undefined && r.getResponse() != "") {
answers[r.getItem().getTitle()] = r.getResponse();
}
});
var equipo = answers["División"];
//please enter here your email adresses for the form
if (equipo == "S1") { sendtheMail("[email protected]", answers) }
else if (equipo == "R1") { sendtheMail("[email protected]", answers) }
else {
{ sendtheMail("[email protected]", answers) }
}
function sendtheMail(contactName, answers) {
var tipo = answers["Tipo de"];
var nombre = answers["Tu nombre"];
var beneficiario = answers["Nombre"];
var cantidad = answers["Cantidad"];
var comentarios = answers["Coments"];
var propertyid = answers["ID"];
var paddress = answers["Dirección"];
//You can change the mail text under htmlBody
MailApp.sendEmail({
name: "Mailing",
to: "[email protected]",
subject: "Nueva solicitud " + nombre + " del equipo " + equipo,
htmlBody: "<p>text " +nombre+ " del equipo " + equipo +
"<p>El tipo de transferencia es: "+ tipo +
"<p>El ID: "+ propertyid +
"<p>La dirección de la propiedad es: "+ paddress +
"<p>El nombre del beneficiario es: " + beneficiario +
"<p>Cantidad a transferir: " + cantidad + " €" +
"<p>Coments: " + comentarios ,
attachments: ,
noReply: true
});
}
}
I modified a little bit the names but everything works as should, the only thing is just to add the attachment to the email but cant find the way, thank you for the help in advance!
I tried already:
var attach = formResponse.getItemResponses();
var file = DriveApp.getFileById(attach[10].getItem());
attachments: [file.getAs(MimeType.PDF)]
But probably I am not setting it correctly on what we need.
