Sending mail to contact when added via API

Following scenario:

we want to add contacts via API.

As an result, we want to send out the Opt-In mail automatically after the creation of a new contact via API.

Adding contacts via API is not the problem, but how to invoke the mail to the contact for the opt-in confirmation ?

You could simply add a workflow to do this, I believe.

I am not sure if record creation from API triggers “on save” workflows, but I assume it does.

You could also have a logic hook.

I already created a workflow and it sends the desired opt-in mail, but the problem is, that the value of “confirm_opt_in_token” is not calculated, e.g. empty.

I see. In fact, sending an opt-in email is not as simple as sending a generic email. Some extra processing is required, and it’s not happening when sent from a Workflow.

Maybe your only chance is to use a Logic hook instead of a Workflow, and inside that hook do the rest of the work (create token etc) by copying it from some other place in the code where that is already done. With luck, you only have to call a function and it will do everything for you.

I am not sure where that might be. Try a few searches in the code, you might find it.

Hi DirkDV,

we solved this for leads with a custom entrypoint. The confirm_opt_in_token is not calculated when created by api/web.
Our leads are created by WordPress in SuiteCRM, there we have a workflow defined when lead is saved which sends the confirm optin mail. The mail includes a link to a custom entrypoint with it’s lead id.

If you want you can get the code for this and modify it for contacts.

The other solution could be using a workflow which triggers at saving e-mail addresses.
when confirm_opt_in_token isnull then calculate it with suitecrm formula (copied from SugarEmailAddress.php):

confirm_opt_in_token = md5(time() . md5($this->email_address) . md5(rand(0, 9999999))) . md5(rand(0, 9999999))

Rolf

If you have code, please do share it here (inside the Forum’s “code” tags)

Thanks!

Ok, here is the code, for Contacs change the code and folders from Leads to Contacts
Steps:

  • add fields to Leads (optin:bool, optin_dt:datetime, optin_url:varchar, optedin_url ) to custom/modules/Leads/vardefs
  • place file “LeadsOptin.php” to custom/modules/Leads/.
  • place file “ep_leadsoptin.php” to custom/Extension/application/Ext/EntryPointRegistry
  • create workflow for sending optin mail, see next

Workflow: create workflow for module Leads on new record with condition optin=false and run always

  • action: modify record, set field optin_url = https:///index.php?entryPoint=Leads_Optin
  • action: modify record, set field optedin_url = /thanks_optin
  • action send mail, sending optin mail with template
  • edit email template, add link $lead_optin_url&eid=$lead_id for optin
  • additional actions if neccessary

That’s the custom otpin process.

Rolf


// custom/Extension/application/Ext/EntryPointRegistry/ep_LeadsOptin.php
<?php
  $entry_point_registry['LeadsOptin'] = array(
      'file' => 'custom/modules/Leads/LeadsOptin.php',
      'auth' => FALSE,
  );
 ?>

// FILE: custom/modules/Leads/LeadsOptin.php
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

global $timedate;

/**
* Entrypoint for OptIn
* Author: Rolf Riethmüller / Dipesh Patel 
* (c) itons GmbH
* do not sell anywhere
*
* custom fields for Leads
* - optin: bool, set by entrypoint, indicates if optin is done
* - optin_url: varchar, set by workflow, this is the url to use in E-Mail template for optin
*    ( https://<your suitecrm site>/index.php?entryPoint=LeadsOptin)
* - optin_dt: datetime, set by entrypoint, datetime when optin is done
* - optedin_url: varchar, set by workflow, is used to call page on website to say thanks for optin
* - lead_stage:enum, not neccessary, is used for our sales pipe 
*/

$oeplFlg = TRUE;
$RetMsg= '';

$OptinID = trim( $_REQUEST['eid'] );
if(empty($OptinID))
{
	//die('Unauthorized access.');
	$oeplFlg = FALSE;
	$RetMsg = 'Optin: access denied';
}

$objLead = BeanFactory::getBean('Leads');
$objLead->retrieve( $OptinID );

if(empty($objLead->id))
{
	$oeplFlg = FALSE;
	$RetMsg = 'Optin: Lead-ID not found:' . $OptinID;
}

if($objLead->optin == '1')
{
	$oeplFlg = FALSE;
}

if($oeplFlg == TRUE)
{
	$objLead->optin_dt		= $timedate->nowDb();
	$objLead->lead_stage	= 'optin';
	$objLead->optin 		= '1';
	$objLead->processed 	= true; // Restrict to call logic hook during save record
	$objLead->save();

} // End of Lead save IF conditions


if( empty($objLead->optin_url) ) {
	$URL_OptedIn= '' ; //put here url after optin to say thanks
}
else {
	$URL_OptedIn= $objLead->optin_url;
}

if (!preg_match("~^(?:f|ht)tps?://~i", $URL_OptedIn)) {
	$URL_OptedIn = "https://" . $URL_OptedIn;
}

header('location: ' . $URL_OptedIn);

exit;
?>
1 Like