I am trying to send an email with attachments.
The code for the attachments is working but struggling to add the gmail signature of whoever will be using the spreadsheet.
My html code is not displaying properly.
Can someone please assist me?
I have the following code:
//Send Email to Supplier
function approver() {
const ui = SpreadsheetApp.getUi();
const row = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
Logger.log(row);
const data = SpreadsheetApp.getActiveSheet().getRange(row, 8, 1, 8).getValues()[0];
const user = {
first: data[4]
, email: data[7]
, attachments: data[0]
, row: row
};
Logger.log(user);
const res = ui.alert('Send to ' user.first '(' user.email ')?', ui.ButtonSet.YES_NO);
if (res == ui.Button.YES) {
sendUser(user);
}
Logger.log(res);
}
function sendUser(user) {
var row = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
var filename = SpreadsheetApp.getActiveSheet().getRange(row,8,1,1).getValue();
var folder = DriveApp.getFoldersByName('COPIES SUPPLIER QUOTES').next();
var file = folder.getFilesByName(filename);
const temp = HtmlService.createTemplateFromFile('EmailTemplate');
temp.user = user;
const message = temp.evaluate().getContent();
GmailApp.sendEmail(
user.email
, 'Tester'
, htmlBody = message
, //'Automatic Emailer Script'
{attachments: [file.next().getAs(MimeType.PDF)]}
);
Thanks in advance...
CodePudding user response:
I tested your html with this function:
GS:
function sendEmail() {
const user = {first:"James",last:'Brown'};
let t = HtmlService.createTemplateFromFile('ah1');
t.user = user;
GmailApp.sendEmail('my email','Subject','Hello Word',{htmlBody:t.evaluate().getContent()})
}
HTML:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p style="font-family: fantasy;color:red;font-size:3em">Hi,
<?= user.first ?>
</p>
<div>
You have been approved
</div>
<div>
Congrats ....
<?= user.first ?>
<?= user.last ?>
</div>
</body>
</html>
This is what the email looked like:
Then I took you code and compressed down to nothing but the essentials
function approver() {
const folder = DriveApp.getFolderById(gobj.globals.testid);
let files = folder.getFilesByType(MimeType.GOOGLE_SHEETS);//need files that I can get as pdf
let fA = [];
while(files.hasNext()) {
let file = files.next();
fA.push(file.getAs(MimeType.PDF));//fA is the array for the attachments
}
let temp = HtmlService.createTemplateFromFile('ah1');
temp.user = {first: 'James',last: 'Brown', email: gobj.globals.calendarid};
GmailApp.sendEmail(temp.user.email,'Tester', 'The missing parameter',{htmlBody:temp.evaluate().getContent(),attachments: fA});
}
The final error that I found in your code is that you left out the null for the body parameter. With the body parameter missing it was causing the options parameter to be misread and that's why your html was not working.
And so now the Final Email Looks like this(I used one of my directories that had four spreadsheet in it):
CodePudding user response:
@Cooper, thank you for the code provided, very useful. This is the code currently using and you will see I have appended the signature in as well:
GS
function approver() {
const ui = SpreadsheetApp.getUi();
var claim = SpreadsheetApp.getActiveSheet().getRange('Claimnumber').getValue();
var signature = Gmail.Users.Settings.SendAs.list("me").sendAs.filter(function(account){if(account.isDefault){return true}})[0].signature;
const row = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
Logger.log(row);
const data = SpreadsheetApp.getActiveSheet().getRange(row, 8, 1, 8).getValues()[0];
const user = {
first: data[4]
//, last: data[1]
, email: data[7]
, attachments: data[0]
, dnumber: claim
, usignature: signature
, row: row
};
Logger.log(user);
const res = ui.alert('Send to ' user.first '(' user.email ')?', ui.ButtonSet.YES_NO);
if (res == ui.Button.YES) {
sendUser(user);
}
Logger.log(res);
}
function sendUser(user) {
var row = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
var filename = SpreadsheetApp.getActiveSheet().getRange(row,8,1,1).getValue();
var folder = DriveApp.getFoldersByName('COPIES SUPPLIER QUOTES').next();
var file = folder.getFilesByName(filename);
let temp = HtmlService.createTemplateFromFile('EmailTemplate');
temp.user = user;
GmailApp.sendEmail(
user.email
,'Quote for Claim number' ' ' user.dnumber
,'Hello World'
,{htmlBody:temp.evaluate().getContent() user.usignature,attachments:[file.next().getAs(MimeType.PDF)]})
HTML
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p style="font-family: fantasy;color:red;font-size:3em">Hi,
<?= user.first ?>
</p>
<div>
Please find attached the quote for Claim number <?= user.dnumber ?>.</p>
<p><br></p>
<p>Thank you.</p>
<p><br></p>
</div>
</body>
</html>
This is working as expected. How do I thank you? Words are not enough!!!