title: Using Nodemailer to send PDF specified in a React App
body: <p>My React component looks like:</p>
  
  <pre><code>import React from "react";
  import { Form, FormGroup, FormText, Input, Label, Button } from "reactstrap";
  import axios from "axios";
  
  import ThankYouModal from "./ThankYouModal";
  import Header from "./Header";
  import Footer from "./Footer";
  
  export default class Contact extends React.Component {
  constructor(props) {
  super(props);
  
  this.state = {
    name: "",
    email: "",
    message: "",
    file: "",
    submitted: false,
  }
  
  this.handleChange = this.handleChange.bind(this);
  this.handleSubmit = this.handleSubmit.bind(this);
  this.closeThankYouModal = this.closeThankYouModal.bind(this);
  }
  
  handleChange = (e) =&gt; {
  this.setState({ [e.target.name]: e.target.value })
  }
  
   async handleSubmit(e) {
  e.preventDefault();
  const { name, email, message, file } = this.state;
  this.setState({ submitted: true, message: "" });
  e.target.message = "";
  
  const formPDF = await axios.post("/api/formPDF", {
    name,
    email,
    message,
    file
  })
  }
  </code></pre>
  
  <p>Then within my render is:</p>
  
  <pre><code>          &lt;FormGroup&gt;
          &lt;Label for="file"&gt;File&lt;/Label&gt;
          &lt;Input
            type="file"
            name="file"
            value={this.state.file}
            onChange={this.handleChange} /&gt;
          &lt;FormText color="muted"&gt;
            File must be sent as a PDF.
          &lt;/FormText&gt;
        &lt;/FormGroup&gt;
  </code></pre>
  
  <p>I then have nodemailer set up to post on "/api/formPDF" and I try to use req.body.file to pull the file name but Javascript does its job and converts the path to "C:\fakepath\MyPDFFile.pdf" and it says it can't find a file at that location.</p>
  
  <p>I know my email setup is working because I can have it successfully send an email without an attachment, I am just having trouble getting a correct path name.  I thought about streaming the data through node with fs, but I'm a bit at a loss on how to tell where fs to look and what the file name will be.  Thanks in advance.</p>
  
  <p>EDIT: Here is my nodemailer setup:</p>
  
  <pre><code>app.post("/api/formPDF", (req, res) =&gt; {nodemailer.createTestAccount((err, account) =&gt; {
  const attach = fs.createReadStream("path", req.body.file);
  
  const htmlEmail = `
    &lt;h3&gt;Contact Details&lt;/h3&gt;
    &lt;ul&gt;
      &lt;li&gt;Name: ${req.body.name}&lt;/li&gt;
      &lt;li&gt;Email: ${req.body.email}&lt;/li&gt;
    &lt;/ul&gt;
    &lt;h3&gt;Message&lt;/h3&gt;
    &lt;p&gt;${req.body.message}&lt;/p&gt;
  `
  
  let transporter = nodemailer.createTransport({
    name: *hidden*,
    host: *hidden*,
    port: 465,
    secure: true,
    auth: {
      user: *hidden*,
      pass: *hidden*
    }
  })
  
  let mailOptions = {
    from: *hidden*,
    to: *hidden*,
    replyTo: req.body.email,
    subject: "New Message",
    text: req.body.message,
    html: htmlEmail,
    attachments: [
      {
       path: attach
      }
   ]
  };
  
  transporter.sendMail(mailOptions, (err, info) =&gt; {
    if (err) {
      return console.log(err)
    }
  })})});
  </code></pre>