In order to send automatic mails via Gmail and Microsoft's Office, the complex steps are made procedural with this library.
NOTE: Reqiuires node -v
>= 18. Or can be used with --experimental-fetch
flag for node -v
= 16.
Grant Mails to Send Mail Automatically from Microsoft Office 365 and Gmail
Microsoft Office 365
- Go to https://portal.azure.com
- Login as the mail address you will send the mails from (like automail@YOURDOMAIN.com)
- Click on "Manage Azure Active Directory"
- Click on "App registrations"
- Click on "New registration"
- Enter "YOURDOMAIN Automail" as "Name"
- Select "Accounts in this organizational directory only (YOURDOMAIN only - Single tenant)"
- Click on "Register"
- Click on "Certificates & secrets"
- Click on "New client secret"
- Enter "YOURDOMAIN Automail" as "Description"
- Choose maximum expiry date
- Click on "Add"
- Copy the value of "Value" and save it in an environment variable (check next block)
- Click on "Overview"
- Copy the value of "Application (client) ID" and save it in an environment variable (check next block)
- Copy the value of "Tenent ID" and save it in an environment variable (check next block)
- Click on "API permissions"
- Click on "Add a permission"
- Click on "Microsoft APIs"
- Click on "Microsoft Graph"
- Click on "Application permissions"
- Click on "Mail.Send"
- Click on "Add permissions"
- In an other session, again go to the https://portal.azure.com login as Office 365 admin user for YOURDOMAIN (like admin@YOURDOMAIN.com)
- Follow the same path as above to reach "App registrations"
- Check all applications tab page
- Click on "YOURDOMAIN Automail"
- Click on "API permissions"
- Click on "Grant admin consent for YOURDOMAIN"
- Click on "Overview"
Set the variables coming from steps 14, 16, 17
export TAMED_MAILER_OFFICE_FROM_MAIL="automail@YOURDOMAIN.com"
export TAMED_MAILER_OFFICE_CLIENT_SECRET="Coming-From-Step-14-Should-Be-40-Chars--"
export TAMED_MAILER_OFFICE_CLIENT_ID="Comes-From-Step-16-Should-Be-36-Char"
export TAMED_MAILER_OFFICE_TENANT_ID="Comes-From-Step-17-Should-Be-36-Char"
Gmail
- Go to https://myaccount.google.com/ and login with your user
- Click the "Security" from left navigation menu
- Click "2-Step Verification" from the "Signing in to Google" section
- Follow the instructions to setup 2-Step Verification
- Go back and click "App passwords" from the "Signing in to Google" section
- Verify your identity by entering your password
- Click "Select app" and choose "Mail"
- Click "Select device" and choose "Other"
- Enter a name (for example
tamed-mailer
) for the app password and click "Generate" - Copy the generated password and configure it as an environment variable as described in the next section
export TAMED_MAILER_GMAIL_USER="yourmail-account@YOURDOMAIN.com" # or "yourmail-account@gmail.com"
export TAMED_MAILER_GMAIL_APP_PASSWORD="ComesFromStep-10"
export TAMED_MAILER_GMAIL_SERVICE="Gmail"
Installation
yarn add tamed-mailer
Usage
const { tamedMailer } = require('tamed-mailer');
const gmailFrom = "tamed-mailer@gmail.com"; // gmail is to automatically convert this to the configured gmail account
const mailTo = "tamed-mailer@yopmail.com";
const mailSubject = "Test Mail Subject";
const textMailContent = "This is a text based test mail.\nLine2\nLine3";
const htmlMailContent = `<span style="color: blue"><h1>This is an HTML based test mail</h1><br>Line2<br>Line3</b></span>`;
let credentials = {
client_secret: 'OFFICE_CLIENT_SECRET',
client_id: 'OFFICE_CLIENT_ID',
tenant_id: 'OFFICE_TENANT_ID',
from_mail: 'OFFICE_FROM_MAIL',
};
let response = await tamedMailer('office', credentials, mailTo, mailSubject, textMailContent, 'text');
// set scheduledTime to 2 minutes later
let currentTime = new Date();
let scheduledTime = new Date(currentTime.getTime() + 2 * 60000); // Add 2 minutes (2 * 60,000 milliseconds) to the current time
let responseScheduled1 = await tamedMailer('office', credentials, mailTo, mailSubject, htmlMailContent, 'text', scheduledTime);
let credentials2 = {
user: 'TAMED_MAILER_GMAIL_USER',
app_password: 'TAMED_MAILER_GMAIL_APP_PASSWORD',
}
let response2 = await tamedMailer('gmail', credentials2, mailTo, mailSubject, textMailContent, 'text');
API
tamedMailer
Name | Description |
---|---|
p_gmail_or_office | Should be either gmail or office . No other values are allowed. Case sensitive. |
p_credentials | Depends on the value of p_gmail_or_office .If it was 'gmail' then following keys must exist: user , app_password .If it was 'office' , then following keys must exist: client_secret , client_id , tenant_id , from_mail . |
p_to | Single reciever of the email. In order to send for multiple reciepents, function must be called multiple times. |
p_subject | Subject line. |
p_body | Body of the mail, depending on p_html_or_text , should be either plain text or an html text. |
p_html_or_text | Should be either html or text . Case sensitive. Dictates how to treat the p_body parameter. |
p_scheduled_time | Optional, only for Office Mails. If provided, the office mail will be sent at the given time. If not provided, the mail will be sent immediately. |
p_save_to_sent_items | Optional, only for Office Mails. If provided, the mail will be saved to the sent items. If not provided, the mail will not be saved to the sent items. |
License
The license is MIT and full text here.