1

My nodemailer is sending the attachment with the filename, and it's empty.

I asked previously for help on this and haven't gotten any response, hopefully, this time, I will. Nodemailer sending attachment using sendinblue

Server.js

  const cName = req.body.cName;
  const cAddress = req.body.cAddress;
  const phone = req.body.phone;
  const email = req.body.email;
  const vName = req.body.vName;
  const vAddress = req.body.vAddress;
  const violation = req.body.violation;
  let fileMultiple = req.body.fFileMultiple;

  let mail = {
    from: "hello@gmail.com",
    to: "email@gmail.com",
    subject: `Complaint from ${cName} on ${vName}`,
    html: `The message is from ${cName} <br />
    Complaint's Name: ${cName} <br />
    Complaint's address: ${cAddress} <br />
    Email: ${email} <br />
    Phone: ${phone} <br />
    Violator's Name: ${vName} <br />
    Violator's Address: ${vAddress} <br />
    Violation: ${violation} <br />`,
    attachments: [
      {
        filename: fileMultiple,
        contentType: "images/jpeg",
      },
    ],
  };
  contactEmail.sendMail(mail, (error) => {
    if (error) {
      console.log(error);
    } else {
      console.log("Message has been sent");
    }
  });
});

Complaint.js

                <Form.Label>Multiple Photos Allowed</Form.Label>
                <Form.Control type='file' accept='.jpg, .png, .jpeg' />
              </Form.Group>
            </Col>
          </Row>
        </Form.Group>
        <Button type='submit' value={"submit"}>
          {status}
        </Button>

My mailbox takeout shows enter image description here

----_NmP-852038be5b3ad90e-Part_1 Content-Type: images/jpeg; name="C:\fakepath\sea-turtle-wave-attunation.JPG" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="C:\fakepath\sea-turtle-wave-attunation.JPG"

----_NmP-852038be5b3ad90e-Part_1--

Terry Hunt
  • 41
  • 8
  • Are you actually passing any content here: `attachments: [ { filename: fileMultiple, contentType: "images/jpeg", }`? – ddastrodd Jul 15 '22 at 00:19
  • Look at the documentation here: https://nodemailer.com/message/attachments/ – ddastrodd Jul 15 '22 at 00:21
  • You are not passing `content`. – ddastrodd Jul 15 '22 at 00:21
  • attachments: [ { filename: fileMultiple, contentType: "images/jpeg", }, ], ----_NmP-e15415be3161479d-Part_1 Content-Type: images/jpeg; name="C:\fakepath\Screenshot_2.png" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="C:\\fakepath\\Screenshot_2.png The file is still coming up blank with 0 MB. Sorry, didn't realized that enter would almost submit that quickly. – Terry Hunt Jul 15 '22 at 00:30
  • In `server.js`, when you construct `mail`, you need to include `content` under `attachments` as shown in the documentation link above. – ddastrodd Jul 15 '22 at 00:40
  • I have tried this ```content: fs.createReadStream('file.txt')``` and it didn't work – Terry Hunt Jul 15 '22 at 00:43
  • That's an example. Did you have an actual file called `file.txt`? – ddastrodd Jul 15 '22 at 00:44
  • Just try passing a string to test it out. See my answer below. – ddastrodd Jul 15 '22 at 00:44
  • The files are what is uploaded from the form. ``` Multiple Photos Allowed ``` – Terry Hunt Jul 15 '22 at 00:51

1 Answers1

1

You need to include content or path (or one of the other options shown here):

let mail = {
    from: "hello@gmail.com",
    to: "email@gmail.com",
    subject: `Complaint from ${cName} on ${vName}`,
    html: `The message is from ${cName} <br />
    Complaint's Name: ${cName} <br />
    Complaint's address: ${cAddress} <br />
    Email: ${email} <br />
    Phone: ${phone} <br />
    Violator's Name: ${vName} <br />
    Violator's Address: ${vAddress} <br />
    Violation: ${violation} <br />`,
    attachments: [
      {
        filename: 'somefile.txt',
        contentType: 'text/plain',
        content: 'string attachment example'
      },
    ],
  };
ddastrodd
  • 710
  • 1
  • 10
  • 22
  • That works, but then the files in the nodemailer app would say corrupted or unable to open. – Terry Hunt Jul 15 '22 at 01:03
  • When you click on the image, it will say it appears that we don't support this format. This is a great start but almost there. – Terry Hunt Jul 15 '22 at 01:22