Nodemailer dependency with Realm functions

I have successfully uploaded the Nodemailer npm dependency in my Mongo Realm functions and now trying to send an email when a document in my Users collection is added. I use the following code:

exports = function(changeEvent) {

  const {fullDocument} = changeEvent;
  const nodemailer = require('nodemailer');

  let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
      user: 'my-gmail-address',
      pass: 'my-gmail-password'
    }
  });
  const mailOptions = {
    from: 'from-gmail-address',
    to: 'to-email-address',
    subject: 'New User',
    html: `<p>email content with new user info</p>` 
  };


  return transporter.sendMail (mailOptions, (error, info) => {
    if (error) {
      console.log(error);
      return;
    }
    console.log('Sent Successfully!');
  });
};

The function is triggered at document insert, but I keep getting an error that “hostname” is not a function. Any idea what the problem is?

2 Likes

I’m getting the exact same problem. Have you found a solution?

No. Unfortunately I couldn’t find a solution. What I did was to write my nodemailer code in my backend API (Express/NodeJs) and use the Realm trigger to send a request to that API endpoint instead.

Same issue here. Has anybody a working solution?

I couldn’t make it work, I’ll probably end up doing the same as @Akbar_bakhshi1. Although it would be cool to be able to use this library in Realm functions.

Hi All,

Nodemailer has a dependency for Punycode which is currently an unsupported module in Realm Functions as mentioned in this article, and is likely the cause of the error.

Realm functions do not support the following built-in modules:

  • child_process
  • cluster
  • domain
  • punycode
  • readline
  • v8
  • vm

Our team is looking to provide Punycode support at some point in the future, however we do recommend using a service such as AWS SES for sending out emails. This will be a more reliable alternative especially if you are sending with high frequency/volume.

Regards
Manny

1 Like