Import base64 email in Gmail

I'm trying to build a bridge between an imap inbox an gmail in NodeJS.

I'm using node-imap for retrieving emails from the foreign inbox. Everything is ok.

But, when I import the mail in Gmail, it sometimes goes wrong when there is attachements parts already base64 encoded.

As when there is a part like that :

--------------XXXXX
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-ID: <XXXX>

/9j/4AAQSkZJRgABAgEAXwBfAAD/7QAsUGhvdG9zaG9wIDMuMAA4QklNA+0AAAAAABAAXwAA
...
--------------XXXXX--

As for now, I simply encode the whole email string in base64 and import it like that :

   /**
     * toBase64
     *
     * Encode string to base 64
     * 
     * @param  {String} st 
     */
    var toBase64 = function (st) {
        return new Buffer(st).toString('base64');
    };

    /**
     * handleNewMail
     *
     * Insert a new mail in Gmail
     * 
     * @param  {String} mail 
     */
    var handleNewMail = function (mail) {

        console.log(mail);

        var content = toBase64(mail).replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '*');

        gmail.users.messages.import({
            userId: 'me',
            resource : {
                raw : msg
            }
        }, function (err, result) {
            if (err) {
                console.error(err);
            } else {
                console.debug("New email imported in gmail");
            }
        });
    };

What could I do to prevent Gmail to refusing my mail where there is attachements parts in base64 ?