Tutorial: 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.

Note:

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

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

Code:

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
	array(
		'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' => 'message.reply@example.com'),
				//'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('example.com'),
				//'google_analytics_campaign'	=> 'message.from_email@example.com',
				'metadata'						=> array('website' => 'www.scriptcase.net')
		
				// Check another settings at https://mandrillapp.com/api/docs/messages.php.html#method=send

	);


$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);
}else{
	$retorno = $mandrill->messages->send($var_msg, $async);
}

//echo"<pre>";
//print_r($retorno);
//echo"</pre>";

sc_alert({lang_othr_grid_export_email_sent});

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