• Builds
  • latest 0.0.55 / 2018-03-14
  • Created 2018-01-28
  • Last modified 2018-09-18
  • grade 1

Description doesn't send invoices for paid accounts, so in case you don't want to visit this app every month and send the invoice to the accounting department, you can use this act, which will do it for you automatically. In the input, specify to whom should the act send the email with an attached invoice in PDF. In the settings of the act (source) update the environment attributes the Username and the Password, then you should create a Scheduler, which should be set with the value 0 0 X * * for Cron expression, where X should be the date when the invoice is created in the, check it there. For me, it is the 21th day of the month so I would put 0 0 23 * * to the scheduler settings, adding 2 days extra just in case something would go wrong and they would delay creating the invoice.


To run the actor, send a HTTP POST request to:<YOUR_API_TOKEN>

The POST payload will be passed as input for the actor. For more information, read the docs.

Example input

Content type: application/json; charset=utf-8

   "subject":"Invoice for the for {{date}}",
   "html":"Hello, <br/> in the attachement you can find the invoice for the Motionmail app for this month. <br/> Have a nice day. <br/> Best, <br/> Apify team"

Source code

Based on the apify/actor-node-puppeteer Docker image (see docs).

const Apify = require('apify');
const humanDelay = ms => (Math.random() + 1) * ms;

Apify.main(async() => {
    let input = await Apify.getValue('INPUT');
    if (!input) {
        throw new Error('Input is missing!');
    function formatDate(date) {
      var monthNames = [
        "January", "February", "March",
        "April", "May", "June", "July",
        "August", "September", "October",
        "November", "December"
      var day = date.getDate();
      //the invoice is for the previous month
      var monthIndex = date.getMonth() - 1;
      var year = date.getFullYear();
      return monthNames[monthIndex] + ' ' + year;

    if(input.subject.indexOf("{{date}}") !== -1){
        input.subject = input.subject.replace("{{date}}",formatDate(new Date()));
    const browser = await Apify.launchPuppeteer();
    const openPage = await browser.newPage();
    await openPage.setUserAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36.")

    //go to the login page
    console.log(`Opening URL:`);
    await openPage.goto("");
    //fill the login
    console.log("Fill the login.")
    await openPage.waitForSelector('input#Email');
    const userInput = await openPage.$('input#Email');
    await userInput.type(process.env.USERNAME, humanDelay(100));
    const pwdInput = await openPage.$('input#Password');
    await pwdInput.type(process.env.PASSWORD, {delay: humanDelay(100)});
    //login submit
    console.log("Login submit.")
    await'button[type="submit"]', {delay: humanDelay(100)});

    //Wait for the dashboard
    await openPage.waitForSelector('a[href*="billing"]')
    await openPage.addScriptTag({url: ''});
    //Do the post to get the invoice ids
    console.log("We are on dashboard. Go for the latest invoice.");
    const receipts = await openPage.evaluate(async() => {
        return await new Promise((resolve) => {
                type: "POST",
                contentType: "text/plain;charset=UTF-8",
                url: '',
            }).done(function (data, textStatus, request, xhr) {
                console.log("data !")
            }).fail(function (error) {
                console.log("failed ajax")

    //we have the PDF link, now go to the detail of the invoice
    const pdfLink = "" + receipts.pop()["id"];
    console.log("link to the pdf -> " + pdfLink);
    await openPage.goto(pdfLink);
    await openPage.waitForSelector('h1')

    //screenshot for debug
    try {
        const screenshotBuffer = await openPage.screenshot();
        //await Apify.setValue('screenshot', screenshotBuffer, {contentType: 'image/png'});
    } catch (e) {
        console.log("screenshot faild!")

    //pdf file
    const pdfBuffer = await openPage.pdf({"format": "a4",printBackground: true});
    console.log(`Saving PDF (size: ${pdfBuffer.length} bytes) to output...`);
    //remove comment for debug, you can download the invoice after
    //await Apify.setValue('invoice', pdfBuffer, {contentType: 'application/pdf'});
    await browser.close();
    console.log("We have PDF, now send it.");
    await'apify/send-mail', {
        subject: input.subject,
        html: input.html,
        attachments: [{
            filename: 'invoice.pdf',
            data: pdfBuffer.toString('base64')
    //Email sent.
    console.log("See you next month!")