如何使用react.js中的nodemailer附加文件

发布于 2025-01-18 07:07:34 字数 1399 浏览 3 评论 0原文

我正在使用与NodeMailer的React发送来自用户输入表格的电子邮件,用户应能够通过表单附加文件(例如PDF),并且表格的内容将使用NodeMailer作为电子邮件发送。我的问题是不知道如何将文件附加到电子邮件中。 在这里是可以使用NodeMailer使用的属性的列表和示例。我可以从通过文件输入到event.target.files的对象中提取哪些属性,用于附加到电子邮件上,我可以获取输入文件的路径吗?

Code:

const [file, setFile] = useState(null);

const handleSubmit = async(e) => {
e.preventDefault();

try {
  await axios.post("http://localhost:4000/send_form", { file });
}
catch (error) {
  console.log(error);
}
}

return (
  <form onSubmit={handleSubmit}>
    <input
      type="file"
      onChange={(e) => setFile(e.target.files[0])}
      required/>
    <button type="submit">Send</button>
  </form>
);

Server:

app.post("/send_form", cors(), async (req, res) => {
  let { file } = req.body;

  await transport.sendMail({
    from: "[email protected]",
    to: "[email protected]",
    subject: "Subject",
    html: `<h1>Hello</h1>`,
    attachments: [{
      filename: "",
      path: ""
    }]
  })
});

I am using React with Nodemailer to send emails from a user input form, the user should be able to attach a file (a PDF for example) through the form and the content of the form will be sent as an email using Nodemailer. My issue comes with not knowing how to attach the file to the email. Here is a list and examples of properties that can be used using Nodemailer. What properties can I extract from the object inputted through the file input to the event.target.files to use to attach to the email, can I get the path of the inputted file for example?

Code:

const [file, setFile] = useState(null);

const handleSubmit = async(e) => {
e.preventDefault();

try {
  await axios.post("http://localhost:4000/send_form", { file });
}
catch (error) {
  console.log(error);
}
}

return (
  <form onSubmit={handleSubmit}>
    <input
      type="file"
      onChange={(e) => setFile(e.target.files[0])}
      required/>
    <button type="submit">Send</button>
  </form>
);

Server:

app.post("/send_form", cors(), async (req, res) => {
  let { file } = req.body;

  await transport.sendMail({
    from: "[email protected]",
    to: "[email protected]",
    subject: "Subject",
    html: `<h1>Hello</h1>`,
    attachments: [{
      filename: "",
      path: ""
    }]
  })
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

负佳期 2025-01-25 07:07:34

您不需要 axios 上传文件,只需将其 POST 为 FormData 使用 Fetch API

async function handleSubmit(event) {
  event.preventDefault();

  let fd = new FormData();
  fd.append('myfile', file);

  fetch('http://localhost:4000/upload', {
    method: 'POST', body: fd
  }).catch(err => {
    console.error(err);
  });
}

如果您还有其他数据要与图像一起提交,也可以将其附加到 FormData 对象。

fd.append('name', 'Joe');
fd.append('age', 40);
// etc...

或者,您可以简单地捕获任何 HTMLFormElement。只需确保将 enctype 属性设置为 multipart/form-data

let form = document.querySelector('#your-form');
let fd = new FormData(form);

然后,在服务器上,您可以使用 multer 中间件将文件缓冲区流式传输到 nodemailer 附件:

import express from 'express';
import multer from 'multer';
import transport from './your_app.js'

const app = express();

const upload = multer({
  storage: multer.memoryStorage()
});

app.post('/upload', upload.single('myfile'), (req, res) => {
  transport.sendMail({
    from: "[email protected]",
    to: "[email protected]",
    subject: "Subject",
    html: `<h1>Hello</h1>`,
    attachments: [{
      filename: req.file.originalname,
      content: req.file.buffer
    }]
  })
});

app.listen(4000);

如果您有其他中间件,您需要在此使用路由,它们可以作为数组传入:

import cors from 'cors';

let middleware = [
  cors(),
  upload.single('myfile')
];

app.post('/upload', middleware, handler);

请注意,以下两个语句中使用的键必须匹配。该键对应于文件输入的name属性。

handleSubmit()中:

fd.append('myfile', 文件);

app.post()中:

upload.single('myfile')

如果需要,Multer 还允许上传多个文件。您可以使用 multiple 属性:

upload.array('myfile', 3)

或者您可以使用多个文件输入:

upload.fields([
  { name: 'myfile', maxCount: 1 },
  { name: 'another-file', maxCount: 8 }
])

如果这样做,您将需要从 req.files 访问上传的文件数据属性而不是单数req.file

表单数据的其余部分将在 req.body 对象中提供:

req.body.name == 'Joe'
req.body.age == 40;

You don't need axios to upload the file, just POST it as FormData with the Fetch API.

async function handleSubmit(event) {
  event.preventDefault();

  let fd = new FormData();
  fd.append('myfile', file);

  fetch('http://localhost:4000/upload', {
    method: 'POST', body: fd
  }).catch(err => {
    console.error(err);
  });
}

If you have other data to submit along with your image it can also be append to the FormData object.

fd.append('name', 'Joe');
fd.append('age', 40);
// etc...

Or, you can simply capture all fields from any HTMLFormElement. Just make sure to set the enctype attribute to be multipart/form-data.

let form = document.querySelector('#your-form');
let fd = new FormData(form);

Then, on the server you can use the multer middleware to stream the file buffer to the nodemailer attachment:

import express from 'express';
import multer from 'multer';
import transport from './your_app.js'

const app = express();

const upload = multer({
  storage: multer.memoryStorage()
});

app.post('/upload', upload.single('myfile'), (req, res) => {
  transport.sendMail({
    from: "[email protected]",
    to: "[email protected]",
    subject: "Subject",
    html: `<h1>Hello</h1>`,
    attachments: [{
      filename: req.file.originalname,
      content: req.file.buffer
    }]
  })
});

app.listen(4000);

If you have other middleware you need to use on this route, they can be passed in as an array:

import cors from 'cors';

let middleware = [
  cors(),
  upload.single('myfile')
];

app.post('/upload', middleware, handler);

Note that the key used in the following two statements must match. This key corresponds to the name attribute of the file input.

In handleSubmit() :

fd.append('myfile', file);

In app.post() :

upload.single('myfile')

Multer also allows for multiple file uploads if needed. You can either capture several files from a single input with the multiple attribute:

upload.array('myfile', 3)

Or you could use several file inputs:

upload.fields([
  { name: 'myfile', maxCount: 1 },
  { name: 'another-file', maxCount: 8 }
])

If you do this, you will need to access the uploaded file data from the req.files property instead of the singular req.file.

The rest of your form data will be available in the req.body object:

req.body.name == 'Joe'
req.body.age == 40;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文