npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@plugcore/ds-email

v1.4.9-3

Published

plugcore.com Email datasource

Downloads

273

Readme

plugcore.com

@plugcore/ds-email

https://nodei.co/npm/@plugcore/ds-email.png?downloads=false&downloadRank=false&stars=false

Documentation can be found at the wiki.

Datasource: Email

This utility will help connect to an Email server (ex: SMTP server, GMAIL, Outlook 365, etc) to send our emails. It uses the datsources system, where every instance of the client will target an specific email server.

Internally it uses nodemailer to connect to any email server, but it also adds some helper functions to connect to a Google API Gmail account, which can be very helpful if you are using a gmail account to send your emails, or a GSuite account, where you can configure SPF, DKIM or DMARC records to secure your emails and evade SPAM filters.

Configuration

Inside our configuration file, for example {PROJECT_ROOT}/configuration/configuration.json, we will have to add a new entry for each email server we want to connect, like in this example:

{
    "connections": {
        "myemail": { // Connection id
            "type": "email", // Datasource type
            "host": "smtp.email.com", // SMTP Host
            "port": 587, // SMTP port
            "secure": false, // true for 465, false for other ports
            "auth": {
                "user": "user", // User/email
                "pass": "password",
            },
            "defaultFrom": { // Email address that will be used to send the emails
                "name": "Company name",
                "address": "[email protected]"
            }
        },
        ...
    },
    ...
}

For more information about configuration you can go to: https://nodemailer.com/smtp/

Usage

Once the connection is defined in the configuration we are ready to create a service decorated with @Service({ connection: 'myemail' }) that will be able to send emails through the configured server.

Basic example:

import { MailOptions, EmailAddress } from '@plugcore/ds-email';
 
// Service that will use the connection we defined before
@Service({ connection: 'myemail' })
export class EmailService { 
 
    constructor(
        // Connection to "myemail"
        private emailDataSource: EmailDataSource
    ) {}
 
    // Basic example
 
    public async sendEmail() {
        await this.emailDataSource.sendEmail(<MailOptions>{
            from: '[email protected]', // This field optional and it's recommended to leave it empty so the system will use the 
                                      // "defaultFrom" property from the configuration file
            to: <EmailAddress>{ name:'Customer Al', address: '[email protected]' },
            cc: [
                '[email protected]',
                { name:'Admin', address: '[email protected]' }
            ],
            subject: 'New receipt',
            html: `
                <div>
                    <img src="cid:embeddedlogo" title="Company logo">
                </div>
                <h2>Hi this is a title</h2>
                <p>This is text</p>
            `,
            attachments: [
                {
                    filename: 'logo.png',
                    path: '/path/to/logo.png',
                    cid: 'embeddedlogo'
                },
                {
                    filename: 'receipt.pdf',
                    content: createReadStream('/path/to/local/file.pdf')
                }
            ]
        });
    }
  
}

You can learn more about MailOptions at https://nodemailer.com/message/ and form file attch options at https://nodemailer.com/message/attachments/

Google API

In order to send emails safely using Google Suite accounts, we have created a special configuration. You can learn how to create your Oauth 2.0 certificate here: https://developers.google.com/gmail/api/auth/web-server, after this you will have to configure the datasource like in this example:

{
    "connections": {
        "myemail": { // Connection id
            "type": "email", // Datasource type
            "defaultFrom": {
                "name": "Company name",
                "address": "[email protected]"
            },
            "gSuiteApi": {
                    "clientEmail": "[email protected]",
                    "privateKey": "-----BEGIN PRIVATE KEY-----\nabc123...ab123==\n-----END PRIVATE KEY-----\n", // Important to scape the new lines with "\n"
                    "userId": "[email protected]" // Same value as from address
            }
        },
        ...
    },
    ...
}

With this you will be able to send emails through the Gmail API instead of an SMTP server.