如何将NodeMailer与Apollo一起使用?

发布于 2025-01-24 21:21:43 字数 1020 浏览 1 评论 0原文

我有一个使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文