如何将NodeMailer与Apollo一起使用?
我有一个使用Apollo Server Express,Apollo客户端以及GraphQL的React应用程序。而且,我正在寻找一种将一些电子邮件从我的客户发送到例如身份验证用户或允许用户重置密码的方法。但是我很难找到良好的阅读/观看材料。
我已经安装了NodeMailer,并且已经在root server/email
文件夹中创建了一个文件:
// 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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论