7

I'm looking to send emails through the REST API, specifically through Restforce in Ruby, but I can't find any resources on this. I am finding my resources about the sendEmail function, and how to set this up in Apex, but nothing about sending an email through the REST API.

Is it possible to send an email via the REST API, and if so, how would I do so?

  • 1
    i think we can have an apex class with http annotation built for this as this send email is not supported in REST! – Mohith Shrivastava Sep 26 '13 at 17:05
4

With Salesforce Actions, you can use the REST api to send emails that count against the "DailyWorkflowEmails" limit instead of the "SingleEmail" limit, which is usually significantly larger. This functionality became available with api 32.0.

$ curl -H 'Authorization: Bearer SessionId' -H 'X-PrettyPrint:1' -H 'Content-Type: application/json' -d @jsonfile BaseURL/services/data/v36.0/actions/standard/emailSimple

./jsonfile

{
  "inputs" : [
    {
      "emailBody" : "This is the body of the email",
      "emailAddresses" : "user1@email.com,user2@email.com",
      "emailSubject" : "An email from salesforce",
      "senderType" : "CurrentUser"
    }
  ]
}
  • Just a quick note. This doesn't send HTML emails if that is what you are looking for. – divinedragon Jul 11 '18 at 15:48
3

You can create a REST service (using the appropriate annotations) to accept an email via REST and send it through that interface. Brief example here:

@RestResource(urlMapping='/sendEmail')
global class SendEmailController {
    @HttpPost
    global static String doPost() {
        String result = 'error: email not sent';
        try {
            // send email here
            result = 'success: email sent successfully';
        } catch(Exception e) {

        }
        return result;
    }
}

You'll be subject to the 1000 per day email limit, though, so you might consider an alternative means of sending emails (like, from your domain's MTA, if configured correctly).

-1

yes you can below is php function which uses rest api to send email to use this you will need to register and get keys https://www.sakshamappinternational.com/sakshamapp-es/

function cURLSakshamappESEmail($to_name,$to_email, $subject, $body_text, $body_html, $from_name, $from_email) {



    $ch = curl_init();


    curl_setopt($ch, CURLOPT_URL, 'http://app.sakshamapp.com/api_call/v1/transactional_emails.php');
    curl_setopt($ch, CURLOPT_POST, 1);


    $data = 'ses_publicKey='.urlencode(get_option('ses_publicKey')).
            '&ses_privateKey='.urlencode(get_option('ses_privateKey')).
            '&from_email='.urlencode($from_email).
            '&from_name='.urlencode($from_name).
            '&to_email='.urlencode($to_email).
            '&to_name='.urlencode($to_name).
            '&subject='.urlencode($subject);

    if($body_html)  $data .= '&body_html='.urlencode($body_html);
    if($body_text)  $data .= '&body_text='.urlencode($body_text);





    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);


        $header = "Content-Type: application/x-www-form-urlencoded\r\n";
        $header .= "Content-Length: ".strlen($data)."\r\n\r\n";


    $result = curl_exec($ch);


    curl_close($ch);


     global $wpdb;

    $wpdb->insert( 
     SESELOGTBL,
    array( 
        'from_email' => $from_email,
        'from_name'  => $from_name,
        'to_name'  => $to_name,
        'to_email'  => $to_email,
        'subject'  => $subject,
        'status'  => $result 
     ), 
    array( 
        '%s', 
        '%s', 
        '%s', 
        '%s', 
        '%s', 
        '%s', 
        ) 
    );


    return ($result === false) ? NULL : $result;


}
  • This is nothing to do with standard REST API provided by Salesforce out of the box. – Andrii Muzychuk Oct 8 '17 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.