繁体   English   中英

在 react 中上传文件并使用 nodemailer 将其作为附件发送

[英]Upload a file in react and send it as an attachment using nodemailer

我想从 React 前端上传一个文件,并使用 nodemailer 将其作为 email 的附件发送。

该文件在后端收到,但我无法通过 nodemailer 发送它。 我尝试了路径、内容等,但仍然无法正确发送。

这样做的正确方法是什么? 我找不到任何有用的资源。

Note: A file that is already stored in the backend is easily sent using path and content property of attachments array.

我不确定如何发送最初由 React 前端接收的文件。

发送 React 前端接收到的文件的正确方法是使用 fetch API 将文件作为 HTTP 请求发送。 然后,您可以使用 nodemailer 库中的 sendEmail function 发送 email。 这是一个例子:

import React from 'react';
import ReactDOM from 'react-dom';
import {
    fetch
} from 'fetch';
import Nodemailer from 'nodemailer';
const sendEmail = Nodemailer.sendEmail;
class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            file: null
        };
    }
    componentDidMount() {
        fetch('file.html').then((response) => response.text()).then((responseText) => {
            this.setState({
                file: responseText
            });
        });
    }
    sendEmail(to, subject, html, attachments) {
        let {
            file
        } = this.state;
        if (file) {
            sendEmail(to, subject, html, attachments, file);
        } else {
            sendEmail(to, subject, html, attachments);
        }
    }
    render() {
        return ( < div > < h3 > Send an Email < /h3> <form onSubmit={this.sendEmail.bind(this)}> <input type="text" name="to" / > < input type = "text"
            name = "subject" / > < textarea cols = "80"
            rows = "20"
            id = "html" / > < button type = "submit" > Send < /button> </form > < p > Your file is: {
                this.state.file
            } < /p> </div > );
    }
}
ReactDOM.render( < App / > , document.getElementById('root'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM