Sending form with attachment - Javascript

Asked

Viewed 187 times

-2

Good morning to all, I am developing an extremely simple e-mail sending system using javascript (Jquery).

I can already send the email using smtpjs. the first part of my system is OK.

However I would like to attach in the email some spreadsheets in Excel, I have tried some procedures and could not.

Follows current code:

$('#enviar').on('click',function(e){
e.preventDefault()
    var email = $('#email').val()
    var titulo =$('#titulo').val()
    var corpo =$('#corpo').val()

    Email.send({
        To : email,
        From : "[email protected]",
        Subject : titulo,
        Body :corpo
    }).then(message => alert(message))

})


<div class="row">
            <form class="col-12">
                <div class="form-group">
                    <label for="email">Email </label>
                    <input type="text" class="form-control" id="email" aria-describedby="Digite o email de destino">
                </div>
                <div class="form-group">
                    <label for="titulo">Titulo do Email</label>
                    <input type="text" class="form-control" id="titulo">
                </div>
                <div class="form-group">
                    <label for="corpo">Corpo do email</label>
                    <textarea class="form-control" id="corpo" rows="3"></textarea>
                </div>
                <div class="form-group">
                    <label for="inp">Enviar Arquivo</label>
                    <input type="file" class="form-control-file" id="input">
                </div>
                <button id="enviar" class="btn btn-primary">Enviar</button>
            </form>
        </div>

1 answer

0

The guy answered the question, but deleted the answer, well that’s the answer he had posted and it worked, I’m grateful!

function uploadFileToServer()
{
   var file = event.srcElement.files[0];
   console.log(event.srcElement)
   var reader = new FileReader();
   reader.readAsBinaryString(file);
   reader.onload = function () {
       var dataUri = "data:" + file.type + ";base64," + btoa(reader.result);
       Email.send({
           SecureToken :"",
           To : "",
           From :"",
           Subject : "Send with base64 attachment",
           Body : "Sending file:" + file.name,
           Attachments : [
            {
                name : file.name,
                data : dataUri
            }]
       }).then(
         message => alert(message)
       );
   };
   reader.onerror = function() {
       console.log('Erro!');
   };
}
  • But it’s good to explain what was done because it can help someone in the future, and not just put the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.