Click here to Skip to main content
15,118,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have the following HTML code:

HTML
<pre><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 my 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@gmail.com",
    Password : "passwordHere",
    To : 'email@gmail.com',
    From : "email@gmail.com",
    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.

What I have tried:

I've tried to grab the file and with JavaScript get its name hoping that this would work so I don't have to manually put in the file:

JavaScript
message = document.getElementById("message").value;
           var fileName = document.getElementById('file').files[0].name;


This code above gives me the file name. And with that, I use the variable "fileName" and place it in here:

JavaScript
Attachments : 
            [{
                name : fileName,
                path: ""

            }]


I'm struggling to figure out how to get the path through with JavaScript.
And to be fair, I'm not too sure a method like this would work anyways. What's an alternative solution for what I'm trying to solve?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900