function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dejan CvetkoskiDejan Cvetkoski 

How i can send mail with attachment with javascript in visualforce

I trying to send email with attachment in javascript but i can't, somebody help me.
 sforce.connection.sessionId = '{!$Api.Session_ID}';
        var oppId = document.getElementById('{!$Component.page.form.oppId}');
        var query = "SELECT Account.Email__c FROM Opportunity WHERE Id = '" + oppId.value + "'";
        var record = sforce.connection.query(query);
        record = record.getArray('records');
var singleRequest = new sforce.SingleEmailMessage();
        var fileAttachment = new sforce.EmailFileAttachment();
        singleRequest.subject = 'Test Mail';
        singleRequest.toAddresses = record[0].Account.Email__c;
        singleRequest.optOutPolicy = 'FILTER';
        singleRequest.plainTextBody = j$('#textArea').val();
        
        fileAttachment.Body =  new Blob([bodyAttachment],{ type: 'application/pdf'});
        fileAttachment.ContentType = 'application/pdf';
        fileAttachment.FileName = name + '.pdf';
        fileAttachment.Inline = false;
        console.log(fileAttachment);
        singleRequest.fileAttachments = ([fileAttachment]);
        var sendMailRes = sforce.connection.sendEmail([singleRequest]);

User-added image
I don't know what is the problem. 
Thanks!
Andy BoettcherAndy Boettcher
You are not putting anything in the BODY of the email.  In your code snip, you are setting the body to be apparently a variable called "bodyAttachment", but you don't instanciate/set it anywhere in your code that I can see.

Just set the body to be "test 123" and see if it goes.
pconpcon
I hate to be the bearer of bad news, but it looks like you cannot do this.  It seems that you cannot interact with Blob fields as it's likely related to the limitations with Visualforce Remote Objects [1].  There's a post on Stackexchange [2] that is seeing a similar issue to yours that points to the limitation post.

[1] http://salesforce.stackexchange.com/questions/64299/save-attachment-with-remote-objects
[2] http://salesforce.stackexchange.com/questions/34405/js-synchronous-calls-email-with-attachment
Dejan CvetkoskiDejan Cvetkoski
I can insert attachment file whit this code, that it's ok ...
<script>
    function uploadAttachment(filecontent, filename, filetype, parentId) {
        var oppId = document.getElementById('{!$Component.page.form.oppId}');
        var attachment     = new sforce.SObject('Attachment');  
        attachment.Name    = filename;  
        attachment.IsPrivate  = false;  
        attachment.ContentType = filetype;  
        attachment.Body    = filecontent;  
        attachment.Description = filename;  
        attachment.ParentId  = parentId;  
        var result = sforce.connection.create([attachment]);
        if(result[0].success == 'true'){  
            alert('It's OK'); 
            close();
            location.reload(true);
        }  
        else{  
            alert('Error');
            close();
        }  
    }
    
    function reqListener () {
        var oppId = document.getElementById('{!$Component.page.form.oppId}');
        var reader = new window.FileReader();
        reader.readAsDataURL(this.response);   
        reader.onloadend = function() {  
            base64data = reader.result.replace('data:application/pdf;base64,','');
            if(base64data.indexOf('text/html') == -1){
                uploadAttachment(base64data,'test','application/pdf',oppId.value);  
            }  
            else{  
                alert('Wrong URL');  
            }  
        }  
    }  
    
    function createAttachment(){
        var oppId = document.getElementById('{!$Component.page.form.oppId}');
        sforce.connection.sessionId = '{!$Api.Session_ID}';
        console.log(oppId.value);
        var oReq = new XMLHttpRequest();
        oReq.onload = reqListener;
        oReq.responseType='blob';
        oReq.open("get", "/apex/Ponuda?isPdf=true&id=" + oppId.value, true);  
        oReq.send();
    }
    
    </script>

But when i try to attach file to Single Email Messenge i have error which I have said above, I think that it is possible to do because only attachment it's problem. When i try to send whitout attachment email is sent.
Andy BoettcherAndy Boettcher
pcon is correct in his previous post - you cannot do what you are trying to do via JavaScript.  You will need to create an Apex controller with a supporting Visualforce Page (so you can call it from a button correctly) and execute that code there.
Michael Jackson 13Michael Jackson 13
Dejan,
Did you ever get this to work? I'm trying to do the same.
I also tried to use your code to create the attachement - but having trouble with this line: var oppId = document.getElementById('{!$Component.page.form.oppId}');

Can youy tell me what Id it is referencing?

-thanks,
Michael