7.0.0 • Published 2 days ago

@nodefony/mail-bundle v7.0.0

Weekly downloads
86
License
CECILL-B
Repository
github
Last release
2 days ago

Welcome to mail-bundle

Register and Configure mail Bundle (Core Bundle)

For a Register mail-bundle add mail: true in config framework

./config/config.js

module.exports = {
  system: {
    /**
    * BUNDLES CORE
    */
    mail: true
  }
}

Override mail-bundle configuration your bundle

./app/config/config.js

/**
 *  OVERRIDE MAIL Bundle
 *
 *   @see FRAMEWORK MAIL config for more options
 *     https://nodemailer.com
 *
 *
 */
 "mail-bundle": {
   nodemailer: {
     default: "free",
     transporters: {
       free: {
         host: "smtp.free.fr",
         port: 465,
         secure: true, // true for 465, false for other ports
         auth: {
           user: "username", // generated  user
           pass: "passwd" // generated  password
         }
       },
       gmail : {
         host: "smtp.gmail.com",
         port: 465,
         secure: true, // true for 465, false for other ports
         auth: {
           user: "user@gmail.com",
           pass: "xxxxxxxxx"
         },
         tls: {
           // do not fail on invalid certs
           rejectUnauthorized: false
         }
       }
     }
   }
 }

Example to use :

Check transporters with sendTestMail

./controller/defaultController.js

mailAction() {
  return this.mailer.sendTestMail("admin@nodefony.com", "gmail", this.context)
    .catch((e) => {
      throw e;
    });
}

Complete example

const vCardsJS = require('vcards-js');
const puppeteer = require('puppeteer');
...


async indexAction() {
  switch(this.method){
    case "POST":
      if (this.query.mail){
        return this.sendEmail(this.query.mail)
        .then((info) =>{
          this.logger(info, "INFO", "EMAIL");
          return this.redirectToRoute("route-after-sended");
        });
      }else{
        return this.redirectToRoute("myroute");
      }
      break;
  }
}

async sendEmail(to) {
  let res = await this.twigToHtml();
  const message = {
    to: to,
    from: `"${this.mailer.authorName}" <admin@nodefony.net>`,
    subject: `${this.kernel.projectName} ✔`, // Subject line
    html: res, // html body
    attachments: [{
      // binary buffer as an attachment
        filename: 'nodefony.pdf',
        content: await this.renderPdf({}, {
          queryString: {
            mail: true
          }
        })
      }, {
      // define custom content type for the attachment
        filename: 'nodefony.vcf',
        content: this.renderVcard(),
        contentType: 'text/vcard'
    }]
  };
  // sendMail(message, transporterName, context)
  // sendMail(message,"gmail", this.context);
  return await this.sendMail(message);
}

async twigToHtml() {
  let file = path.resolve(this.bundle.viewsPath, "index.html.twig");
  let res = await this.renderTwigFile(file)
    .catch((e) => {
      throw e;
    });
  return this.mailer.juiceResources(res, null, this.context);
}

// vcard example
renderVcard() {
  let vCard = vCardsJS();
  //set properties
  vCard.firstName = 'admin';
  vCard.lastName = 'admin';
  vCard.organization = 'Nodefony';
  vCard.photo.attachFromUrl('https://cdn.nodefony.com/app/images/app-logo.png', 'JPEG');
  vCard.logo.attachFromUrl('https://cdn.nodefony.com/app/images/app-logo.png', "PNG");
  vCard.workPhone = '+033xxxxxxxxxx';
  vCard.birthday = new Date(xxxx, xx, xx);
  vCard.title = 'Software Developer ';
  vCard.url = this.generateAbsoluteUrl("myurl", true);
  vCard.gender = 'M';
  vCard.email = 'admin@nodefony.net';
  vCard.source = this.generateAbsoluteUrl("render-vcard", true);
  vCard.homeAddress.label = 'Home Address';
  vCard.homeAddress.street = '';
  vCard.homeAddress.city = '';
  vCard.homeAddress.stateProvince = '';
  vCard.homeAddress.postalCode = '';
  vCard.homeAddress.countryRegion = '';
  vCard.socialUrls.github = 'https://github.com/nodefony';
  //vCard.note = '';
  return vCard.getFormattedString();
}

// RENDER PDF ON attachement with puppeteer
async renderPdf(options, query = {}) {
  const browser = await puppeteer.launch({
    headless: true,
    ignoreHTTPSErrors:true,
    defaultViewport:null
  });
  options = nodefony.extend({
    //format: 'A6',
    scale: 0.8,
    printBackground: true,
    //width: '1300px',
    preferCSSPageSize: false,
    displayHeaderFooter: false,
    //headerTemplate: ,
    landscape: false,
    margin: {
      top: "30px",
      bottom:"30px",
      right:"0px",
      left:"0px"
    }
  }, options);
  const page = await browser.newPage();
  await page.goto(this.generateAbsoluteUrl("route_page_to_transform", query));
  await page.emulateMedia("print");
  let buffer = await page.pdf(options);
  await browser.close();
  return buffer;
}

Authors

  • Camensuli Christophe ccamensuli@gmail.com

License

7.0.0

2 days ago

7.0.0-beta.22

7 months ago

7.0.0-beta.21

7 months ago

7.0.0-beta.20

1 year ago

7.0.0-beta.19

1 year ago

7.0.0-beta.15

1 year ago

7.0.0-beta.16

1 year ago

7.0.0-beta.17

1 year ago

7.0.0-beta.18

1 year ago

7.0.0-beta.11

1 year ago

7.0.0-beta.12

1 year ago

7.0.0-beta.13

1 year ago

7.0.0-beta.14

1 year ago

7.0.0-beta.10

1 year ago

7.0.0-beta.9

1 year ago

7.0.0-beta.8

2 years ago

7.0.0-beta.6

2 years ago

7.0.0-beta.7

2 years ago

7.0.0-beta.4

2 years ago

7.0.0-beta.5

2 years ago

7.0.0-beta.2

2 years ago

7.0.0-beta.3

2 years ago

7.0.0-beta.0

2 years ago

7.0.0-beta.1

2 years ago

6.12.0

2 years ago

6.11.0

2 years ago

6.10.0

2 years ago

6.8.0

3 years ago

6.9.0

3 years ago

6.9.1

3 years ago

6.6.0

3 years ago

6.5.7

3 years ago

6.5.6

3 years ago

6.5.5

3 years ago

6.5.2

3 years ago

6.5.4

3 years ago

6.5.3

3 years ago

6.5.1

3 years ago

6.5.0

3 years ago

6.4.3

3 years ago

6.4.2

3 years ago

6.4.1

3 years ago

6.4.0

3 years ago

6.3.0

3 years ago

6.2.0

3 years ago

6.1.0

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

6.0.0-beta.16

4 years ago

6.0.0-beta.15

4 years ago

6.0.0-beta.14

4 years ago

6.0.0-beta.13

4 years ago

6.0.0-beta.12

4 years ago

6.0.0-beta.11

4 years ago

6.0.0-beta.10

4 years ago

6.0.0-beta.9

4 years ago

6.0.0-beta.8

4 years ago

6.0.0-beta.7

4 years ago

6.0.0-beta.6

4 years ago

6.0.0-beta.5

4 years ago

6.0.0-beta.4

4 years ago

6.0.0-beta.3

4 years ago

6.0.0-beta.2

4 years ago

6.0.0-beta.1

4 years ago

5.1.1

5 years ago

5.1.0

5 years ago

5.0.9

5 years ago

5.0.8

5 years ago

5.0.7

5 years ago

5.0.6

5 years ago

5.0.5

5 years ago

5.0.4

5 years ago

5.0.3

5 years ago

5.0.2

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

4.3.3

5 years ago

4.3.2

5 years ago

4.3.1

5 years ago

4.3.0

5 years ago

4.2.0-beta.2

5 years ago