S
S
Stepan Shatalov2019-12-05 12:57:04
css
Stepan Shatalov, 2019-12-05 12:57:04

Sending emails through google spreadsheets, how to add attachments to a letter?

Please tell me what needs to be added to this script so that you can send emails with attachments?
We conditionally insert a link to the Google drive file in the sixth column and the file is attached to the letter.
I don’t understand the code very well, I made the script by analogy with others, but I couldn’t figure it out with attachments.
I will be grateful for your help!

function sendEmail(recipient, subject, htmlBody, options)
{
var sheet = SpreadsheetApp.getActiveSheet();
var activeRange = sheet.getActiveRange();
var data = activeRange.getValues();
for (i in data) {
var row = data[i];
var namesender = row[0];
var emailAddress = row[1];
var hiddencopy = row[2];
varsubject = row[3];
varmessage = row[5];
var information = row[4];
};
var emailQuotaRemaining = MailApp.getRemainingDailyQuota();
Logger.log("Remaining email quota: " + emailQuotaRemaining);
MailApp.sendEmail({
to: emailAddress,
bcc: hiddencopy,
replyTo: "[email protected]",
name: namesender,
subject: subject + " " + information,
htmlBody: message,
});
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Marat Garifullin, 2018-12-26
@magarif

This block
Says that the jquery-migrate-git.min.js file was not found, check the location. It should be next to the html file of the page
This block

preloader.js:1 Uncaught ReferenceError: $ is not defined
at preloader.js:1

Says that the object was not found in the variable &
Replace the head block with this
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" type="text/css" href="GLAV.css">

  <script
    src="https://code.jquery.com/jquery-3.3.1.slim.js"
    integrity="sha256-fNXJFIlca05BIO2Y5zh1xrShK3ME+/lYZ0j+ChxX2DA="
    crossorigin="anonymous"></script>

  <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/[email protected]/slick/slick.css"/>
  <script type="text/javascript" src="//cdn.jsdelivr.net/npm/[email protected]/slick/slick.min.js"></script>

  <script type="text/javascript" src="preloader.js"></script>
</head>

i removed jquery-migrate-git.min.js and added jquery connection and slickSlider

A
Alexander Ivanov, 2019-12-05
@oshliaer

The simplest is to add a body with a link to the Disk

MailApp.sendEmail({
  ...
  body: 'https://drive...',
  ...
});

The file will be loaded automatically. Other question is connection of files through attachment.
Example for PDF
var file = DriveApp.getFilesByName('test123.pdf');
if (file.hasNext()) {
    MailApp.sendEmail(emailAddress, subject, message, {
    attachments: [file.next().getAs(MimeType.PDF)],
    name: 'Automatic Emailer Script'
}

My code as per comments
function sendEmail() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var activeRange = sheet.getActiveRange();
  var data = sheet
    .getRange(
      activeRange.getRow(),
      1,
      activeRange.getLastRow() - activeRange.getRow() + 1,
      6
    )
    .getValues();

  data.forEach(function(row) {
    var file = DriveApp.getFilesByName(row[5]);
    if (file.hasNext()) {
      var namesender = row[0];
      var emailAddress = row[1];
      var hiddencopy = row[2];
      var subject = row[3];
      var message = row[5];
      var information = row[4];
      var emailQuotaRemaining = MailApp.getRemainingDailyQuota();
      Logger.log('Remaining email quota: ' + emailQuotaRemaining);

      MailApp.sendEmail({
        to: emailAddress,
        bcc: hiddencopy,
        replyTo: '[email protected]',
        name: namesender,
        subject: subject + ' ' + information,
        htmlBody: message + ' ' + https,
        attachments: [file.next().getAs(MimeType.PDF)],
      });
    } else {
      Logger.log('Файл для отправки не найден');
    }
  });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question