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

How to send email from visualforce without apex class

I wrote some test logic for sending an email from VF page with using apex class.
Javascript from VF page:
function sendMessage(){
        var receiver = document.getElementById("receiverId").value;
        var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
        if(receiver.length > 0 && pattern.test(receiver)){
            var subject = "{!$User.FirstName}" + " " + "{!$User.LastName}" + " contact information";
            var body = document.getElementById("info").innerHTML.trim();
            sforce.connection.sessionId = "{!$Api.Session_ID}"; 
            var success = sforce.apex.execute("Emailer", "sendEmail", {toAddress: receiver, subject: subject, body: body});
                alert("Email was sent successfully.");
            } else {
                alert("Email was not sent. There is some error!");
        } else {
            alert("Wrong email");
Apex class:
global class Emailer {
    webService static Boolean sendEmail(String toAddress, String subject, String body){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[]{toAddress};
        Messaging.SendEmailResult[] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
        return result[0].isSuccess();
Is there a way to do the same without apex class, just using VF and javascript(remote objects or something like that)?
Short answer: Not really.  There may be some email endpoint available out there that will let you connect to it via JavaScript and send your email.

Long Answer: You could create a new object called something like WorkflowEmail__c and set a bunch of String fields for to addresses and create your body and subject lines.  Then write a workflow rule that send an email anytime that object is sent using a VisualForce email template with the only merge feilds being the subject / body.  Then in your JavaScript just insert the WorkflowEmail__c object.  It's similar to something we do but it's not the cleanest solution.