mailjet/mailjet-apiv3-php

Problems with mailjet api & php wrapper

Closed this issue · 2 comments

Hi! I need help please, with an integration of api mailjet & PHP
On a website build in wordpress i integer the mailjet php wrapper and construct email marketing builder. With the api mailjet i can 1) i can create a campaign and attach contact list; 2) i can add content html; but in the last step (3) don´t can send inmediately a campaign, just page is broken. Something happens in this last step that doesn't get executed and the page breaks.
I share the code:

// API Mailjet credentials
$MJ_APIKEY_PUBLIC = '**************';
$MJ_APIKEY_PRIVATE = '**************';
$MJ_SUBJECT = esc_attr( get_the_title() );
$MJ_FROMEMAIL = '**************@**************.com';
$MJ_FROMDESC = '**************';
$titulo_campania = 'Newsletter '.date('d-m-y');

// Connect to mailjet url on proyect
require get_template_directory().'/newsletter/mailjet-apiv3/vendor/autoload.php'; 
use \Mailjet\Resources;

// Build a template
$contenido_html = '';
if (file_exists( get_template_directory().'/newsletter/armado-template.php')) {
	include_once ( get_template_directory().'/newsletter/armado-template.php');
}

if( !empty($_POST["enviar_campania_btn"]) ){
	
	$template_html = $inicio_html.$contenido_html.$fin_html;

	$mj = new \Mailjet\Client($MJ_APIKEY_PUBLIC, $MJ_APIKEY_PRIVATE, true, ['version' => 'v3']);
	$body = [
		'Locale' => "es_ES",
		'Sender' => $MJ_FROMDESC,
		'SenderName' => $MJ_FROMDESC, 
		'SenderEmail' => $MJ_FROMEMAIL,
		'Subject' => $MJ_SUBJECT,
		'ContactsListID' => "**************",
		'Title' => $titulo_campania 
	];
	$config = [
	  'campaign' => [
		'Locale' => "es_ES",
		'Sender' => $MJ_FROMDESC,
		'SenderName' => $MJ_FROMDESC,
		'SenderEmail' => $MJ_FROMEMAIL,
		'Subject' => $MJ_SUBJECT,
		'ContactsListID' => "**************",
		'Title' => $titulo_campania 
	  ],
	  'detailcontent' => [
		'Headers' => "object",
		'Html-part' => $template_html,
		'MJMLContent' => '
		<mjml>
			<mj-body>
				<mj-section>
				  <mj-column>
					<mj-image width="100px" src="/assets/img/logo-small.png"></mj-image>
					<mj-divider border-color="#F45E43"></mj-divider>
					<mj-text font-size="20px" color="#F45E43" font-family="helvetica">Hello World</mj-text>
				  </mj-column>
				</mj-section>
			</mj-body>
		</mjml>', // MJML test example
		'Text-part' => "Contenido de texto" // Text test example
	  ]
	];
	
	# Step 1: Crear campaña
	$response = $mj->post(Resources::$Campaigndraft, ['body' => $body]);
	$response->success() && var_dump($response->getData());
	echo 'Step 1: <br />';
	print("Estado: " . $response->getReasonPhrase());
	$campaignId = $response->getData()[0]['ID'];
	// Result -> Created
	
	# Step 2: Agregar contenido
	$response2 = $mj->post(Resources::$CampaigndraftDetailcontent, ['id' => $campaignId, 'body' => $config['detailcontent']]);
	$response2->success() && var_dump($response2->getData());
	echo 'Step 2: <br />';
	print("Estado: " . $response2->getReasonPhrase());
	// Result -> Created
	
	// Print Campaign ID
	echo '<br />'.$campaignId.'<br />';
	// Result -> OK
	
	# Step 3: Enviar campaña  
	$response4 = $mj->post(Resources::$CampaigndraftSend, ['id' => $campaignId]);
	$response4->success() && var_dump($response4->getData());
	echo 'Step 4: <br />';
	print("Estado: " . $response4->getReasonPhrase());
	// Result -> Nothing, the page is broken on this line exactly

}

if(!empty($contenido_html)){ 
	// Show template
	echo $inicio_html; 
	echo $contenido_html; 
	
	// Here i show the form to execute after conditional code 
	
	echo $fin_html; 
}

I did several tests. The last test, I only executed steps 1 and 2. And then (already with the id of the created campaign), I only executed step 3 with that id. The answer was:
<b>Fatal error</b>: Uncaught TypeError: Argument 5 passed to Mailjet\Request::__construct() must be of the type array or null, string given, called in [...]
The fatal error is generated by this line: "$response = $mj->post(Resources::$CampaigndraftSend, ['id' => ******]);"

Thanks very much!

Hello @Palimass
So as I see your error says that body was a string. Please, could you check again your code, because looks like you passed not an array as body for method. In example code it looks good, but this error is pretty straightforward. Maybe you used json_encode before for some reason and that;s why it can throw an error

Close this issue according to inactivity