Skip to content

Email Manager

Abdi Urgessa edited this page Apr 21, 2024 · 3 revisions

The EmailManager class is designed to integrate email functionalities with your Telegram bot, allowing you to send emails directly from your bot. This functionality is especially useful for bots that need to send notifications, alerts, or any form of communication via email based on user interactions or internal triggers.

Given the Google Apps Script (GAS) runtime environment, this class avoids asynchronous operations, adhering to the execution model supported by GAS.

Example Usage

Below are detailed examples showcasing how to use the EmailManager within a Telegram bot to send emails under various scenarios:

Basic Email Sending
const telesun = new Telesun.config({
      telegram: '7172550832:AA******Vw',
    });

telesun.on("message", (ctx) => {
  const recipient = "example@example.com";
  const subject = "Welcome to Our Service";
  const body = "Hello there! Thank you for joining our service.";
  ctx.sendEmail(recipient, subject, body);
});
Sending Email with HTML Content
telesun.on("message", (ctx) => {
  const recipient = "example@example.com";
  const subject = "Your Weekly Newsletter";
  const body = "<h1>Weekly Newsletter</h1><p>Here's your weekly news roundup.</p>";
  const options = {htmlBody: body};
  ctx.sendEmail(recipient, subject, body, options);
});
Sending Email with CC and BCC
telesun.on("message", (ctx) => {
  const recipient = "primary@example.com";
  const subject = "Project Update";
  const body = "Please find attached the latest project update.";
  const options = {
    cc: "cc@example.com",
    bcc: "bcc@example.com"
  };
  ctx.sendEmail(recipient, subject, body, options);
});
Sending Email with Attachment
telesun.on("message", (ctx) => {
  const recipient = "example@example.com";
  const subject = "Document Submission";
  const body = "Attached is the document you requested.";
  const options = {
    attachments: [Utilities.newBlob('Hello, world!', 'text/plain', 'helloWorld.txt')]
  };
  ctx.sendEmail(recipient, subject, body, options);
});

Methods Overview

The EmailManager class encapsulates the email functionality into a single method:

  • sendEmail(recipient, subject, body, options): Sends an email to the specified recipient(s) with the given subject and body. The options parameter allows for additional email configurations such as CC, BCC, HTML content, and attachments.

Parameters:

  • recipient (string): The email address of the recipient.
  • subject (string): The subject line of the email.
  • body (string): The main content/body of the email. This can be plain text or HTML (if specified in options).
  • options (object): An optional parameter to specify additional settings like HTML body, CC, BCC, and attachments.
Clone this wiki locally