Nodemailer in code block passes test, but does not send mail

Hi,
I’m pretty new to all this stuff. I’m trying to add a code block to my workflow that sends an email generated with data from a prior step using nodemailer.

My code works correctly if I don’t don’t wrap it inside export default defineComponent({ async run({ steps, $ }) { ... }, })

But then the steps object is undefined.

If I wrap it in the async function, my code runs a test successfully, but doesn’t actually send the email. Anyone see what I’m doing wrong? Thank you!!

Here’s my code:

import nodemailer from 'nodemailer'

export default defineComponent({
  async run({ steps, $ }) {

    // Get new member's details
    const recipientEmail = steps.get_record.$return_value.fields["member-email"]
    const recipientName = steps.get_record.$return_value.fields["member-name"]
    const inviteCode = steps.get_record.$return_value.fields["invite-code"]

    // Create the message 
    const message = `
    Hey ${recipientName} - So great to hear from you and thanks so much for your interest in our ogranization. We're excited to have you join the community!

    Best way to jump in is to join our Discord here (https://discord.js.org/${inviteCode}).`

    // configure the mail transport
    const transporter = nodemailer.createTransport({
      host: 'smtp.gmail.com',
      auth: {
        user: process.env.GMAIL_USERNAME,
        pass: process.env.GMAIL_PASSWORD_PIPEDREAM
      }
    });
    
    const mailOptions = {
        from: process.env.GMAIL_USERNAME,
        to: recipientEmail,
        subject: 'Welcome aboard!',
        text: message
    };

    transporter.sendMail(mailOptions, function(error, info){
      if (error) {
        console.log(error);
      } else {
        console.log('Email sent: ' + info.response);
      }
    });

  },
})

Hi @jaredehunter

First off, welcome to the Pipedream community. Happy to have you!

I believe the issue is within the transporter.sendMail call you’re making near the bottom of the step, it’s using a callback which is not letting Pipedream know to wait for that asynchronous action of sending the email.

Try await'ing the email, or wrapping that particular call with a Promise.

The docs describe this problem and a few ways of solving it here: Running asynchronous code

Hope this helps!

Thanks, that makes sense. Unfortunately that link is now broken? It was working earlier today but I didn’t really have a chance to check it out then. I’ll see if I can figure it out…

Sorry, we’re migrating some documentation and still linking old links here’s the newest link:

1 Like