Using the Mandrill API with macro sc_call_api

In this tutorial we will be showing how to use the sc_call_api macro with the Mandrill API.


To use the Mandrill API, you must create an account on the website and obtain your access key. More information on the Mandrill website:

Creating the Control application

1.We create the necessary fields for the mail submission form.

First we create 6 fields for the email header
  • api_key : Mandrill API Key
  • from_email : Sender’s Email
  • from_name: Sender’s Name
  • mail_to: Recipient email
  • to_cc: Copy of the email for another recipient
  • to_bcc: Blind copy of the email for another recipient

1.1. Next, we need to create a radio type field for the message type and subject.

In the lookup edition of the Radio field “msg_type” we select the Manual lookup method with the values ​​”template” and “message”

1.2. We create other fields used for the Selection of the Template.

  • template_name: The name or immutable slug of a model that exists in the user’s account.
  • template_editable: The name of the editable region “mcedit” to inject content into the email.
  • template_injection: The content to be injected into the body of the email
  • msg : HTML Edit field used to create the body of the email

2. Now we must create a code that hides the unnecessary fields according to the type of Message that is selected, and for that we will create the hide_fields method


if ({msg_type} == 'template') {
    sc_field_display({template_name}, 'on');
	sc_field_display({template_editable}, 'on');
	sc_field_display({template_injection}, 'on');
	sc_field_display({msg}, 'off');
} else {
    sc_field_display({template_name}, 'off');
	sc_field_display({template_editable}, 'off');
	sc_field_display({template_injection}, 'off');
	sc_field_display({msg}, 'on');

3. This function will be called in an Ajax onClick event of the msg_type field and also in the OnLoad event of the Control Form.

4. In the onValidateSuccess event we will include the following code.

function explodeMail($str, $type)
   $arr			= explode(',', $str);
   $arr_return	= [];
   foreach ($arr as $item) {
       $arr_return[] = array(
                               'name'	=> '',
                               'type'	=> $type,
                               'email'	=> $item
   return $arr_return;

$arr_to		= explodeMail({mail_to}, 'to');
$arr_cc		= (!empty({to_cc})) ? explodeMail({to_cc}, 'cc') : $arr_cc = array();
$arr_bcc	= (!empty({to_bcc})) ? explodeMail({to_bcc}, 'bcc') : $arr_bcc = array();

$arr_merge		= array_merge($arr_to, $arr_cc, $arr_bcc);
$txt_no_tags	= strip_tags({msg});

$var_profile	= ''; // Enter your profile if you have created it on "Tool >> API menu" or leave it blank
$var_config		= array( 'settings' => ['gateway' => 'mandrill', 'api_key'=> {api_key}] );

$mandrill = sc_call_api($var_profile, $var_config);

$template_name 		= {template_name}; // the immutable name or slug of a template that exists in the user's account.
$template_content 	= array( 
// an array of template content to send. Each item in the array should be a struct with two keys - name: the name of the content block to set the content for, and content: the actual content to put into the block
		'name'		=> {template_editable}, // the name of the mc:edit editable region to inject into
		'content'	=> {template_injection} // the content to inject

$var_msg = array(

			   'from_email'						=> {from_email},
			   'from_name'						=> {from_name},
				'html'							=> {msg},
				'text'							=> $txt_no_tags,
				'to'							=> $arr_merge,			   
				'subject'						=> {subject},
				'important'						=> true,			
				//'attachments'					=> array('/var/www/arquivo.txt','/var/www/arquivo2.txt'),
				//'headers'						=> array('Reply-To' => ''),
				//'track_opens'					=> null,
				//'track_clicks'				=> null,
				'auto_text'						=> null,
				'auto_html'						=> null,
				'inline_css'					=> null,
				//'url_strip_qs'				=> null,
				//'tags'						=> array('password-resets'),
				//'subaccount'					=> 'customer-123',
				//'google_analytics_domains'	=> array(''),
				//'google_analytics_campaign'	=> '',
				'metadata'						=> array('website' => '')
				// Check another settings at


$async = false;
//$ip_pool = 'Main Pool';
//$send_at = 'example send_at';

if( {msg_type} == 'template' ) {
	$retorno = $mandrill->messages->sendTemplate($template_name, $template_content, $var_msg, $async);
	$retorno = $mandrill->messages->send($var_msg, $async);



Configuring the Layout

5. In Layout >> Blocks create a new block and configure it according to the image below

In Positioning of the Fields we configure it as follows.

6. Save the changes and run the Application.

To see more examples created with Scriptcase, go to: Examples: Complete systems and applications with Scriptcase

Tutorials in the same category

Tutorial: Cloud File Management

Some of the data storage services are ready to securely read and store files and images from your S...

Integration with WhatsApp

To integrate WhatsApp with ScriptCase, we will be using a specific API called Chat-API, it is a...

Tutorial: Blank Application

In this example a Blank application will be created, using for example jquery code to create an acc...

Using the Paypal and Pagseguro API

1 - We create an application of the grid type 2 - We edit the following fields and create a ...

Comment this post