'How to use NodeMailer with Apollo?
I have an React app that uses Apollo Server Express, Apollo Client and of course GraphQL. And I'm looking for a way to send some emails from my client to, for example, authenticate a user or allow a user to reset their password. But I'm having some trouble finding good reading/viewing material.
I've installed nodemailer and I've created a file in my root server/email folder:
// sendEmail.ts
import nodemailer from 'nodemailer';
export const sendEmail = async () => {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
const testAccount = await nodemailer.createTestAccount();
// create reusable transporter object using the default SMTP transport
const transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: testAccount.user, // generated ethereal user
pass: testAccount.pass, // generated ethereal password
},
});
// send mail with defined transport object
const info = await transporter.sendMail({
from: '"Fred Foo 👻" <[email protected]>', // sender address
to: '[email protected], [email protected]', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world?', // plain text body
html: '<b>Hello world?</b>', // html body
}, (err, info) => {
console.log(err, info);
});
console.log('Message sent: %s', info.messageId);
// Message sent: <[email protected]>
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
};
I can't call the sendEmail function directly from my React app because NodeMailer is a NodeJS module. I've tried importing the sendMail.ts in my resolver.js file and just add call the function in a query:
// resolver.js
const {sendEmail} = require('../server/email/sendEmail');
const Query = {
returnNotifications: async (root, args, {res, req}) => {
sendEmail.sendEmail();
return await returnUserNotifications(args, req);
}
}
This gives the console error:
Error: Cannot find module '../server/email/sendEmail'
I'm probably mixing up some stuff or missing a step, if anyone could point it out to me I would be very grateful.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
