How to attach a file using Mandrill API?

Asked

Viewed 238 times

3

Yo, yo, y'all! I’ve been trying for two days, but I haven’t succeeded yet. I know I’m close, but I need your help. I usually take a lot of Jobs with only fixed data (front-end). So I use the API of the Mandrel to send, by e-mail, the data of the forms.

In the official documentation says that it supports attachments, provided that the content be put on Base64-Ncode.

PROBLEM:

I can’t generate an attachment in the email sent. However, all other data (Subject, E-mail of who sent, Name of who sent , Body E-mail) arrive at the recipient normally. To prove that the content the file was being generated before the file was sent, I sent it in the body of the email and it was stated that the file code (Base64) is ok.

CODE USED:

Just to contextualize the problem. I’m not sure if this plugin influences the problem, so I decided to quote it here.

How do I use the jQuery Validation to validate the form, the request POST of the Mandrill is implemeted within the function submitHandler(), which is nothing more than the function that is executed after the validation success:

$(document).ready(function() {
  $('#rh-form').validate({
    rules: {
      name: {
        required: true
      },
      ddd: {
        required: true
      },
      phone: {
        required: true
      },
      cv: {
        required: true
      }
    },
    messages: {
      name: {
        required: 'Informe seu nome!'
      },
      ddd: {
        required: 'Informe seu DDD!'
      },
      phone: {
        required: 'Informe seu contato!'
      },
      cv: {
        required: 'Selecione um arquivo!'
      }
    },
    submitHandler: function(form) {
      //AQUI DENTRO FICA O BLOCO DE CÓDIGO DO MANDRILL
      //QUE VOU MOSTRAR A SEGUIR...
    }
  });
});

CODE BLOCK OF THE MANDRILL:

//PEGO OS DADOS DO FORM
var name = $('input[name="name"]').val(),
          ddd = $('input[name="ddd"]').val(),
          phone = $('input[name="phone"]').val(),
          city = $('input[name="city"]').val(),
          uf = $('select[name="uf"]').val(),
          //CORPO DO E-MAIL
          mailText = '<p><label>Nome: </label>'+ name +'</p><p><label>Telefone: </label> (' + ddd + ') ' + phone + '</p><p><label>Localidade: </label>'+ city +' - '+ uf +'</p>';

      if( $('#fileInput').val() != '') {
        var file = $('#fileInput')[0].files[0];
        var reader = new FileReader();
        var fileResult;
        reader.onload = function(event) {
          //FILE RESULT RECEBE O CÓDIGO DO ARQUIVO #fileInput
          fileResult = btoa(event.target.result);
          sendMessage(mailText, name, fileType, fileName, fileResult);
        }
      }

      reader.readAsBinaryString(file);
      var fileType = file.type;
      var fileName = file.name;



      var sendMessage = function (mailText, name, fileType, fileName, fileResult) {

        var data = {
          'key': 'xxxxxxxxxxxxxxxxxxx',
          'message': {
            //ADICIONEI fileResult NO CORPO E TIVE SUCESSO EM VER O CÓDIGO. NADA DE ANEXO
            'html': mailText + '<br><br>'+ fileResult,
            'text': '',
            'subject': 'Testando Anexo',
            'from_email': '[email protected]',
            'from_name': name,
            'to': [
                {
                  'email': '[email protected]',
                  'name': 'Phellipe Lins',
                  'type': 'to'
                }
            ]
          },
          'attachments': [
              {
                  'type': fileType,
                  'name': fileName,
                  'content': fileResult
              }
          ],
          'async': false
        };

        $.post('https://mandrillapp.com/api/1.0/messages/send.json', data)
          .success(function() {
            window.alert('Contato feito com sucesso!');
            document.getElementById('rh-form').reset();
          })
          .fail(function() {
            window.alert('Ocorreu algum problema.');
          });
      }

TEST PRINT

Segue o e-mail.

  • Since you don’t use the file on the client side you could send everything with a fromData to the server and treat the image there. I think it would be better... have you tried it?

  • I went to take a look at the documentation and saw that it looks different the 'attachments' and 'images'. If your upload is an image you’ve tried to put on 'images'?

  • This is PDF and DOC, no images. I didn’t understand what you said, @Sergio. I could explain in another way?

  • 1

    The idea was to send this form to your server and deal with the conversion to Base64. But if you want to do it with Javascript you can test readAsDataURL instead of readAsBinaryString + btoa. There’s an answer on Soen which also suggests this.

  • There is nowhere that you speak the type of content?

  • Solved your problem?

Show 1 more comment
No answers

Browser other questions tagged

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