Has anyone been able to use the multipart/form-data schema to send emails in PHP ? I’ve been trying for some time now to get it right, but I just can’t get it to work. The JSON schema works just fine, but due to the nature of the business of our users, bigger emails also needs to be supported.
This is currently what’s holding back our migration from v2 to v3.
I think the biggest hurdle is getting the actual message content in the request, the attachments are, from what I think, done correctly. But the message data (to, from, body, …) is where I think I’m doing things wrong.
I have tried the following :
Using a library to build multipart :
https://docs.php-http.org/en/latest/components/multipart-stream-builder.html
$fields = [
'subject' => $subject,
'to' => [
'to' => ['name' => $recipientName, 'email' => $recipientEmailAddress],
],
'body' => $body,
];
if ($ccAddresses) {
$fields['cc'] = $this->addAddresses($ccAddresses);
}
if ($bccAddresses) {
$fields['bcc'] = $this->addAddresses($bccAddresses);
}
$url = 'https://api.us.nylas.com/v3/grants/'.$grantId.'/messages/send';
$streamFactory = Psr17FactoryDiscovery::findStreamFactory();
$builder = new MultipartStreamBuilder($streamFactory);
$builder->addData(json_encode($fields));
foreach ($attachments as $attachment) {
$builder->addResource($attachment['filename'], $attachment['content'], ['filename' => $attachment['filename']]);
}
$multipartStream = $builder->build();
$boundary = $builder->getBoundary();
$request = new Request('POST', $url, [
'Authorization' => 'Bearer ' . $this->getApiKey(),
'Content-Type' => 'multipart/form-data; boundary="'.$boundary.'"'
],
$multipartStream->getContents()
);
/** @var \GuzzleHttp\Psr7\Response $response */
$response = Psr18ClientDiscovery::find()->sendRequest($request);
A cURL approach :
$fields = [
'subject' => $subject,
'to' => [
'to' => ['name' => $recipientName, 'email' => $recipientEmailAddress],
],
'body' => $body,
];
if ($ccAddresses) {
$fields['cc'] = $this->addAddresses($ccAddresses);
}
if ($bccAddresses) {
$fields['bcc'] = $this->addAddresses($bccAddresses);
}
$url = 'https://api.us.nylas.com/v3/grants/'.$grantId.'/messages/send';
// curl
$curl = curl_init();
$boundary = uniqid();
$delimiter = '-------------' . $boundary;
$post_data = $this->buildDataFiles($boundary, $fields, $attachments);
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
//CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_HTTPHEADER => array(
//"Authorization: Bearer $TOKEN",
"Content-Type: multipart/form-data; boundary=" . $delimiter,
"Content-Length: " . strlen($post_data),
'authorization' => 'Bearer ' . $this->getApiKey(),
),
));
$errorMessage = null;
$response = curl_exec($curl);
$info = curl_getinfo($curl);
$err = curl_error($curl);
curl_close($curl);
We’re currently on Symfony 3.4 and I tried using some of Symfony’s FormDataPArt as well, but no luck :
Can anyone help guide me to get it right ?