sendMail API limitations?

Hello,

Is it possible to send email via the backendless email apis that have both a list of BCC recipients and an attachment? They appear to be mutually exclusive. If that is not possible, is there a recommendation for sending mail from JS cloud code?

Thank you,

Kelyl

1 Like

Hi Kelly,

Sending an email with attachments and bcc addresses is supported with Email Templates:
https://backendless.com/docs/js/email_send_email_with_templates_api.html

We just added support for attachments - the API doc hasn’t been updated yet. You can see the signature here:

Regards,
Mark

Perfecto! Thank you

Mark, is there any additional setup I need to do to use the new feature? I switched to calling sendEmailFromTemplate. All recipients in the envelope receive the email, template values are substituted correctly, everything looks perfect, except no attachments are present. I ran a trace through my smtp server and the mail is coming through without the specified attachments.

If I send a test message from the template definition screen and select a file from the appropriate directory, the attachment arrives. But when I call the function from my API service, the attachment is ignored. For testing I am hard-coding the same file name that the test message uses, and the file definitely exists. No errors appear in the log files. If I hard-code a BOGUS filename, the message is still sent without errors, so I’m pretty sure the new attachment argument is being ignored.

My app id is 8149FCCE-07DA-F988-FFBB-6154872C9500 and the template I’m using is called ReturnsTemplate.

Thank you!

Hello @Kelly_Oglesby

Unfortunately, I could not reproduce this problem in my application. Therefore, I will have some questions for you:

  1. Do you send your emails through the API service?
  2. Is the file you are sending in Files in Backendless?
  3. Please send the snippet code with the problem so that we can reproduce it in our place.

Also, I want to clarify that in the Messaging>> Emails section, files that were added using the “Add attachments” button will only be sent if the message is sent from this screen. That is, if you add a file in edit mode and then send an email using the API, there will be no attachment.

Regards,
Inna

Inna, thank you. Yes, I am sending via the console while running my API service in code runner in debug mode, so it isn’t currently deployed. I could deploy it if it would help. I’ve removed everything else in the service code and reduced it to the following:

'use strict';

class PartsScan {
    constructor() { }

    /**
     * @param {String} zone
     * @param {String} bdate 
     * @param {String} edate
     * @param {String} signature
     */
     async sendReturns(zone, bdate, edate) {
        var status = await this.sendMail();
        return {error: null, status: status};
    }

    /**
     * 
     * @private
     * 
     */
     async sendMail(options) {
        const env = new Backendless.Messaging.EmailEnvelope();

        env.setTo(["blah@gmail.com"]);
        env.setBcc(["blah2@gmail.com"]);
        return Backendless.Messaging.sendEmailFromTemplate(
              "ReturnsTemplate", 
              env, 
              {subject: "foo", dateRange: "bar"}, 
              ["app-files/pdftmp/6e3a7330310311ec9bc19191cfffcdd3.pdf"])
    }
}
Backendless.ServerCode.addService(PartsScan)

I just ran this from the console with everything hard-coded as you see it, except for the email addresses. Both email accounts received the email perfectly, but without any hint of an attachment.

Logging shows this:

2021-10-20 16:04:07,414 | Global logger |  INFO | Send custom template 'ReturnsTemplate' for 'blah@gmail.com' was published : 
2021-10-20 16:04:07,429 | Global logger |  INFO | Sending messages using the template 'ReturnsTemplate' is completed.The email template was sent to 1 recipients. : 

Substituting garbage for the attachments file name argument leads to the same result. I do understand how the attachments in the templates definition window is used. The code I removed actually generates a dynamic PDF file, that will be emailed then deleted. I got rid of that just to isolate the mail function.

Kelly

is the app-files directory sitting in the Root of your Backendless file storage?

If so, have you tried referencing the following with a leading slash?

return Backendless.Messaging.sendEmailFromTemplate(
              "ReturnsTemplate", 
              env, 
              {subject: "foo", dateRange: "bar"}, 
              ["/app-files/pdftmp/6e3a7330310311ec9bc19191cfffcdd3.pdf"])

Regards,
Mark

Yes, same behavior. The attachment is here.

image

Could you try it with a file that is not a pdf?

Same with a text file, no attachment. I tried with and without the slash.

Is behavior the same both in debug and prod deployments?

I reduced the entire api service to:

'use strict';

class PartsScan {
    constructor() {
    }

    /**
     * 
     * @param {String} to
     * @param {String} bcc
     * 
     */
     async sendMail(to, bcc) {
        const env = new Backendless.Messaging.EmailEnvelope();

        env.setTo([to]);
        env.setBcc([bcc]);
        
        return Backendless.Messaging.sendEmailFromTemplate(
              "ReturnsTemplate", 
              env, 
              {subject: "foo", dateRange: "bar"}, 
              ["/app-files/pdftmp/test.txt"])
    }
}

Backendless.ServerCode.addService(PartsScan)

and deployed it to production. I verified in the coding tab that the service is as shown. The invocation returned a message id, logging said the mail was sent. But now I’m not even getting the message on either the recipients account or the bcc (different email providers, but same smtp server.) It’s been ten minutes, and no messages have arrived. That may not have anything to do with backendless, it looks like it is hitting my smtp server, but still with no attachment.

What is your app ID? I’d like to try invoking the service directly

There is news. I didn’t get the messages after the production deployment because Google killed them as spam - I guess the attachment tickled their spam detection. So I actually DID get the attachment in production mode.

I tried it with the PDF file, and both the recipient and the BCC received everything perfectly.

My app id is 8149FCCE-07DA-F988-FFBB-6154872C9500.

If the code worked in production but doesn’t work in the debug mode, the only reason is the outdated Backendless NPM module. Please make sure to update your project dependencies. The latest Backendless npm module is v6.3.1.

Regards,
Mark

OK, I see I’m still at 6.1.5 so I will upgrade. Thanks for everything.