Attach Files to Mandrill Message with Browse Button

前端 未结 2 1351
星月不相逢
星月不相逢 2021-01-19 17:14

I\'m not sure whether this is even possible, but I\'m trying to attach a file to an outgoing email with the Mandrill API from the file upload button (input type=\"file

2条回答
  •  迷失自我
    2021-01-19 18:08

    If you wanted to attach a file, you could look at AJAX file Upload

    The Mandrill API requires a base64 encoded string of the file, so if you can upload the file you can easily make the server return a base64 encoded string for you to use in the API..

    There are lots of plugins that will upload files over ajax and if you're using a JavaScript library like jQuery you can look at jQuery File Upload

    If you're using PHP for instance:

    $result = array();
    $result['status'] = 'error';
    if(isset($_FILES["mandrill_attachment"]))
    {
        $base64 = base64_encode(file_get_contents($_FILES["mandrill_attachment"]["tmp_name"]));
        $result['base64'] = $base64;
        $result['status'] = 'ok';
    }
    
    die(json_encode($result));
    

    I've kept that short for the example, but you should check the file types etc, not just assuming it's an OK file

    and the JS

    var mandrill_attachment = false;
    
    $(".attachment").uploadFile({
        url: "upload.php",
        dragDrop:false,
        multiple:false,
        autoSubmit:true,
        fileName: "mandrill_attachment",    
        returnType:"json",
        onSuccess:function(files,data,xhr)
        {
            if( data.status == 'ok' )
            {
                mandrill_attachment = data.base64;
            } else {
                alert('something went wrong...');
            }
        }
    });
    

提交回复
热议问题