How can I send files to Gmail using SMTP?

  email, forms, html, javascript, smtp

I have the following HTML code:

<form method = "POST" name = "submissionsFrom">

        Full Name:
        <input type = "text" name = "subject" value = "" id = "name">
    </p>
    <p>
        Message:
        <textarea name = "message" id = "message"></textarea> 
    </p>
    <p>
        Select file:
        <input type = "file" accept = ".docx" required id = "file" name = "file"> 
    </p>

     <input type="button" value="Send Email" onclick="sendEmail()"/>
    
</form>

And this is the JavaScript (using SMTP):

  let name = document.forms["submissionsFrom"]["name"].value;
    var message = document.forms["submissionsFrom"]["message"].value;
    var file = document.forms["submissionsFrom"]["file"];
   
    function sendEmail() {
    
    message = document.getElementById("message").value;

    Email.send({
    Host: "smtp.gmail.com",
    Username : "[email protected]",
    Password : "passwordHere",
    To : '[email protected]',
    From : "[email protected]",
    Subject : "Email Submission",
    Body : message,
    Attachments : [
    {
        // name : "", // <-- Here, I'd like for the user's file to be an attachment,
        // path: ""  // not an already assigned attachment.
        

        }]
    }).then(
        message => alert("Submission sent!")
    );

}

What I want is, instead of a file already assigned with the attachments array, I’d like for the user to attach a file and send it in the email along with the body text that they put in. I already have the rest working, but I’m struggling with figuring out how to make the user upload a file that’ll be sent in the email.

Source: Ask Javascript Questions

LEAVE A COMMENT