How to attach a file using Mandrill API?

3

Ie, guys! I've been trying for two days, but I have not had success yet. I know I'm close, but I need your help. I often get a lot of jobs with only front-end data. So I use the Mandrill API to email form data.

In official documentation says that it supports attachments, as long as content is set to base64 -encode.

PROBLEM:

I can not generate an attachment in the sent email. However, all other data (Subject, E-mail of who sent, Name of who sent, Body of E-mail) arrive at the recipient normally. To take the proof that content of 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 code of the file (base64) is ok.

USED CODE:

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

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

$(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...
    }
  });
});

MANDRILL CODE BLOCK:

//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 E-MAIL PRINT

    
asked by anonymous 05.06.2015 / 23:14

0 answers