This topic was automatically generated from Slack. You can find the original thread here.
Is there any way to send custom mail to the users in the nodejs step? If yes please share an example.
Note- not to the workspace admin
This topic was automatically generated from Slack. You can find the original thread here.
Is there any way to send custom mail to the users in the nodejs step? If yes please share an example.
Note- not to the workspace admin
Yes, you can send custom emails to users using the Node.js step in Pipedream. One way to do this is by using the Nodemailer package. Here’s an example of how to send an email using Nodemailer: 1. First, add the Nodemailer package to your code step: import nodemailer from "nodemailer";
2. Then, use the following example code to send an email: export default defineComponent({ props: { fromEmail: { type: "string", label: "From Email", }, toEmail: { type: "string", label: "To Email", }, subject: { type: "string", label: "Subject", }, message: { type: "string", label: "Message", }, }, async run({ steps, $ }) { const transporter = nodemailer.createTransport({ host: "your_smtp_server", port: 587, secure: false, auth: { user: "your_email", pass: "your_email_password", }, }); const mailOptions = { from: this.fromEmail, to: this.toEmail, subject: this.subject, text: this.message, }; try { const info = await transporter.sendMail(mailOptions); console.log("Message sent: %s", info.messageId); } catch (error) { console.error("Error sending email:", error); } }, });
Replace your_smtp_server
, your_email
, and your_email_password
with your SMTP server details and email credentials. This example uses the Nodemailer package to send an email with the specified fromEmail
, toEmail
, subject
, and message
props. Please note that you’ll need to use an SMTP server and email provider that allows sending emails programmatically.
to multiple user
To send an email to multiple users, you can modify the toEmail
prop to accept an array of email addresses. Here’s an example using Nodemailer: import nodemailer from "nodemailer"; export default defineComponent({ props: { fromEmail: { type: "string", label: "From Email", }, toEmails: { type: "string[]", label: "To Emails", }, subject: { type: "string", label: "Subject", }, message: { type: "string", label: "Message", }, }, async run({ steps, $ }) { const transporter = nodemailer.createTransport({ host: "your_smtp_server", port: 587, secure: false, auth: { user: "your_email", pass: "your_email_password", }, }); const mailOptions = { from: this.fromEmail, to: this.toEmails.join(", "), subject: this.subject, text: this.message, }; try { const info = await transporter.sendMail(mailOptions); console.log("Message sent: %s", info.messageId); } catch (error) { console.error("Error sending email:", error); } }, });
In this example, the toEmails
prop is of type string[]
, which allows users to input multiple email addresses. When setting the to
field in mailOptions
, we join the email addresses with a comma ,
as a separator. This will send the email to all recipients in the toEmails
array.
create the text message as custom html.
To send an email with custom HTML content, you can modify the message
prop and the mailOptions
in the previous Nodemailer example. Here’s the updated code: import nodemailer from "nodemailer"; export default defineComponent({ props: { fromEmail: { type: "string", label: "From Email", }, toEmails: { type: "string[]", label: "To Emails", }, subject: { type: "string", label: "Subject", }, htmlContent: { type: "string", label: "HTML Content", }, }, async run({ steps, $ }) { const transporter = nodemailer.createTransport({ host: "your_smtp_server", port: 587, secure: false, auth: { user: "your_email", pass: "your_email_password", }, }); const mailOptions = { from: this.fromEmail, to: this.toEmails.join(", "), subject: this.subject, html: this.htmlContent, }; try { const info = await transporter.sendMail(mailOptions); console.log("Message sent: %s", info.messageId); } catch (error) { console.error("Error sending email:", error); } }, });
In this example, I’ve added a new prop htmlContent
for the custom HTML content. Then, I’ve replaced the text
property in mailOptions
with html
, which sets the email content to the provided HTML.