I have the following HTML code:
<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 : [
{
}]
}).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:
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:
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?