fx Google Apps Script for Sheets
ENहिन्दी

MailApp / GmailApp से ऑटोमेटेड अलर्ट

⏱ 15 min

आप क्या सीखेंगे

  • MailApp बनाम GmailApp
  • सबसे आसान ईमेल
  • Options ऑब्जेक्ट — HTML, CC, अटैचमेंट

समझिए

1. MailApp बनाम GmailApp

MailApp GmailApp
क्या कर सकता है सिर्फ़ ईमेल भेजना भेजना, पढ़ना, खोजना, लेबल, ड्राफ़्ट बनाना
माँगी गई अनुमति "आपकी ओर से ईमेल भेजना" आपके Gmail की पूरी पहुँच
कब इस्तेमाल करें अलर्ट और रिपोर्ट जब मेल पढ़ना या व्यवस्थित भी करना हो

भेजने के लिए MailApp को प्राथमिकता दें — कम अनुमति, वही नतीजा।

2. सबसे आसान ईमेल

function testMail() {
  MailApp.sendEmail('you@example.com', 'Test from Sheets', 'Hello! This came from Apps Script.');
}

ईमेल उस अकाउंट से जाता है जिसकी स्क्रिप्ट है (या जिसने ट्रिगर सेट किया)।

3. Options ऑब्जेक्ट — HTML, CC, अटैचमेंट

MailApp.sendEmail({
  to: 'owner@example.com',
  cc: 'manager@example.com',
  subject: 'Stock alert',
  htmlBody: '<p><b>3 items</b> are below reorder level.</p>',
  name: 'ExcelWalaa Stock Bot'        // sender display name
});

4. पूरा कम-स्टॉक अलर्ट

function sendStockAlert() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const data = ss.getSheetByName('Data').getDataRange().getValues();
  const header = data.shift();
  const low = data.filter(r => r[0] !== '' && Number(r[1]) < 5);

  if (low.length === 0) return;                        // nothing to report

  const rows = low.map(r =>
    `<tr><td>${r[0]}</td><td style="text-align:right">${r[1]}</td></tr>`).join('');
  const html = `
    <p>Good morning,</p>
    <p>These <b>${low.length}</b> items are below the reorder level:</p>
    <table border="1" cellpadding="6" style="border-collapse:collapse">
      <tr style="background:#1F4E78;color:#fff"><th>${header[0]}</th><th>${header[1]}</th></tr>
      ${rows}
    </table>
    <p><a href="${ss.getUrl()}">Open the stock sheet</a></p>`;

  MailApp.sendEmail({
    to: 'owner@example.com',
    subject: `Stock alert: ${low.length} items to reorder (${Utilities.formatDate(new Date(), 'Asia/Kolkata', 'dd-MMM-yyyy')})`,
    htmlBody: html
  });
}

इसे लेसन 4 के रोज़ सुबह 9 बजे वाले ट्रिगर से जोड़ें, और मालिक को हर सुबह लिस्ट मिलेगी — सिर्फ़ उन दिनों जब कुछ कम हो।

5. शीट को PDF के रूप में अटैच करें

const pdf = DriveApp.getFileById(ss.getId()).getAs('application/pdf').setName('Stock.pdf');
MailApp.sendEmail({ to: 'owner@example.com', subject: 'Stock report',
                    htmlBody: 'Report attached.', attachments: [pdf] });

यह पूरी स्प्रेडशीट (सारी दिखने वाली शीट) एक्सपोर्ट करता है। किसी ख़ास टैब और अपनी पेज सेटिंग के लिए UrlFetchApp के साथ export URL चाहिए — यह एडवांस्ड तरीका है।

6. लिस्ट से पर्सनलाइज़्ड ईमेल

"Customers" शीट जिसमें Name (A), Email (B), Due (C), Status (D):

function sendReminders() {
  const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Customers');
  const data = sh.getDataRange().getValues();
  for (let i = 1; i < data.length; i++) {
    const [name, email, due, status] = data[i];
    if (!email || status === 'Sent' || Number(due) <= 0) continue;
    MailApp.sendEmail(email, 'Payment reminder',
      `Dear ${name},\n\nYour pending amount is Rs ${Number(due).toLocaleString('en-IN')}.\n\nThank you.`);
    sh.getRange(i + 1, 4).setValue('Sent');           // log it
  }
}

7. रोज़ की सीमाएँ

फ़्री Gmail अकाउंट स्क्रिप्ट से रोज़ लगभग 100 प्राप्तकर्ताओं को भेज सकते हैं; Google Workspace अकाउंट को कहीं ज़्यादा (लगभग 1,500) मिलता है। बाकी कोटा देखें:

Logger.log(MailApp.getRemainingDailyQuota());

आम गलतियाँ

टेस्टिंग के दौरान पूरी ग्राहक लिस्ट को भेज देना (पहले अपने पते से जाँचें)। लूप से रोज़ का कोटा ख़त्म कर देना। MailApp काफ़ी होने पर GmailApp इस्तेमाल करना (बड़ा अनुमति प्रॉम्प्ट)। बताने को कुछ न हो फिर भी अलर्ट भेजना।

अभ्यास

mediumअपनी Data शीट के लिए sendStockAlert बनाएँ, अपने ईमेल से हाथ से चलाएँ, फिर रोज़ाना ट्रिगर से जोड़ें। ऐसी जाँच जोड़ें जो रविवार को भेजना छोड़ दे (new Date().getDay() === 0)।
शुरू में if (new Date().getDay() === 0) return रखें। Stock एक बार पढ़ें, Qty < 5 छाँटें, खाली होने पर लौटें और quota जाँचकर अपने inbox में एक summary भेजें। Starter की 15 reorder rows जाँचें। फिर एक installable daily trigger जोड़ें।

प्रश्नोत्तरी

भेजने के लिए किस सेवा को कम अनुमति चाहिए?
MailApp
आज और कितने ईमेल भेज सकते हैं, कैसे देखें?
MailApp.getRemainingDailyQuota()
low लिस्ट खाली होने पर जल्दी return क्यों?
ताकि खाली अलर्ट न जाएँ
MailApp / GmailApp से ऑटोमेटेड अलर्ट · हिंदी | ExcelWalaa