Dani
Dani

Reputation: 11

Sending a template email from Mandrill using API is delivers empty 'to' /recipient mail

I am using mandrill API for email template for my WordPress web site users, and through custom plugin I retrieve the emails of users and send through mandrill API. In my API log I am receiving empty response for 'to' or recipient email? Any solution?

function nt_email_survey_report($id=0){
  global $wpdb;
  error_log('nt_email_survey_report : starts');
  error_log('nt_email_survey_report : id='.$id);
  if ($id>0)  $current_user_id=$id;
  else $current_user_id=get_current_user_id();
  $user_session_token=get_user_session($current_user_id);
  
   error_log('current_user_id :'.$current_user_id);
   
  $sql='SELECT email_report FROM rbl_nt_surv_sessions
        where session_token="'.$user_session_token.'"' ;
    //echo 'sql='.$sql; 
    $email_report = $wpdb->get_var($sql);
    if ( $wpdb->last_error ) {
      echo 'wpdb error: ' . $wpdb->last_error;
    }
        
  if ($email_report!=1) return;
  
  $email=nt_get_user_email($current_user_id);
  error_log('>>>> email :'.$email);

  $profile=get_personality($user_session_token);
  
  $about_personality=get_about_personality($profile);
  $about_personality=nl2br( $about_personality);
  $about_personality=preg_replace('/\s\s+/', ' ', $about_personality);
  $about_personality = preg_replace('/[\x00-\x1F\x7F]/', '', $about_personality);
  $about_personality=addslashes($about_personality);  
  $p_arr=get_people_examples($profile);
  // $c_arr=get_character_examples($pt);
  if ($profile){
    $html='<div class="people_examples">';
    $html.='<ul>';
    foreach ( $p_arr as $p ) {
                $html.='<li>'.$p->people.'</li>';
        
    }
    $html.='</ul>';
    $html.='</div>';
   }
   
   $example_of_people=addslashes($html);
   //$example_of_people='';
   
    $c_arr=get_character_examples($profile);
    if ($profile){
        $html='<div class="character_examples">';
        $html.='<ul>';
        foreach ( $c_arr as $c ) {
                    $html.='<li>'.$c->characters.'</li>';
            
        }
        $html.='</ul>';
        $html.='</div>';
   }
 

   $example_of_characters=addslashes($html);
  // $example_of_characters='';
   $primary_wbs=get_primary_wbs($user_session_token);
   $p_wbs_training_link= get_wbs_training_link($primary_wbs);
   $primary_wbs_description=get_wbs_desc($primary_wbs);
   //$primary_wbs_description=str_replace( array('"', '\n' , '\r','\t'), ' ', $primary_wbs_description);
   //$primary_wbs_description=str_replace('\n','',$primary_wbs_description);
   $primary_wbs_description=nl2br( $primary_wbs_description);
   $primary_wbs_description=preg_replace('/\s\s+/', ' ', $primary_wbs_description);
   $primary_wbs_description = preg_replace('/[\x00-\x1F\x7F]/', '', $primary_wbs_description);
  
   
   $secondary_wbs=get_secondary_wbs($user_session_token);
   $s_wbs_training_link= get_wbs_training_link($secondary_wbs);
   $secondary_wbs_description=get_wbs_desc($secondary_wbs);
   
   $secondary_wbs_description=nl2br( $secondary_wbs_description);
   $secondary_wbs_description=preg_replace('/\s\s+/', ' ', $secondary_wbs_description);
   $secondary_wbs_description = preg_replace('/[\x00-\x1F\x7F]/', '', $secondary_wbs_description);
   $html=nt_get_skills_graph_shortcode($current_user_id);
   $survey_skills_graph=addslashes($html);
   //$survey_skills_graph='';
   //$secondary_wbs_description='';
   $primary_wbs_analysis_table=addslashes(nt_get_skill_gap_analysis_table($primary_wbs));
   $secondary_wbs_analysis_table=addslashes(nt_get_skill_gap_analysis_table($secondary_wbs));
    
  // global $current_user;
  // get_currentuserinfo();

  // $email = (string) $current_user->user_email;
  
   
   $url = "https://mandrillapp.com/api/1.0/messages/send-template";
    
   $curl = curl_init($url);
   curl_setopt($curl, CURLOPT_URL, $url);
   curl_setopt($curl, CURLOPT_POST, true);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = <<<DATA
    {
        "key": "md-t-yFg3****Upwy*******A",
        "template_name": "REPORT",
        "template_content": [],
        "message": {
            "from_email":"[email protected]",
            "from_name":"Wealth Advantage",
            "subject": "Wealth Building Strategy Results!",
            "to": [
                    {
                    "email": "$email",
                    "type": "to"
                    }
            ],
            "global_merge_vars": [
                {               
                "name": "personality_type",
                "content": "$profile"
                },
                {
                "name": "about_personality",
                "content": "$about_personality"
                },
                {
                
                "name": "example_of_people",
                "content": "$example_of_people"
                },
                {
                "name": "example_of_characters",
                "content": "$example_of_characters"
                },
                {               
                "name": "primary_wbs",
                "content": "$primary_wbs"
                },
                {               
                "name": "secondary_wbs",
                "content": "$secondary_wbs"
                },
                {               
                "name": "primary_wbs_description",
                "content": "$primary_wbs_description"
                },
                
                {               
                "name": "p_wbs_training_link",
                "content": "$p_wbs_training_link"
                },
                {               
                "name": "s_wbs_training_link",
                "content": "$s_wbs_training_link"
                },
                {               
                "name": "secondary_wbs_description",
                "content": "$secondary_wbs_description"
                },
                {               
                "name": "primary_wbs_analysis_table",
                "content": "$primary_wbs_analysis_table"
                },
                {               
                "name": "secondary_wbs_analysis_table",
                "content": "$secondary_wbs_analysis_table"
                },
                {               
                "name": "survey_skills_graph",
                "content": "$survey_skills_graph"
                }
                
                
                
            ]
        },
        "async": false
}
DATA;
error_log('data : '.print_r($data,true));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
//var_dump($resp);
error_log('result: '.$resp);


error_log('nt_email_survey_report : END');
}

I tried putting static mail in 'to' field, it works fine, but with dynamic mails coming from database, this happen, its WordPress plugin.

Upvotes: 0

Views: 88

Answers (0)

Related Questions