Skip to content

Commit

Permalink
Merge pull request #3 from xawiers/user-headers-bug
Browse files Browse the repository at this point in the history
Incorrect method when user defined headers are set.
  • Loading branch information
Nightbr authored Jul 31, 2017
2 parents ff53c48 + 606b914 commit 97b77e6
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions SwiftMailer/MessageFormat/BaseMessagePayload.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<?php

namespace Mailjet\MailjetSwiftMailer\SwiftMailer\MessageFormat;

use \Swift_Mime_Message;
abstract class BaseMessagePayload implements MessageFormatStrategyInterface {

abstract class BaseMessagePayload implements MessageFormatStrategyInterface
{
/**
* @param Swift_Mime_Message $message
*
* @return string
* @throws \ReflectionException
*/
protected static function getMessagePrimaryContentType(Swift_Mime_Message $message) {
protected static function getMessagePrimaryContentType(Swift_Mime_Message $message)
{
$contentType = $message->getContentType();
if (self::supportsContentType($contentType)) {
return $contentType;
Expand All @@ -22,83 +27,97 @@ protected static function getMessagePrimaryContentType(Swift_Mime_Message $messa
$propRef->setAccessible(true);
$contentType = $propRef->getValue($message);
}

return $contentType;
}

/**
* @return array
*/
private static function getSupportedContentTypes() {
return array(
private static function getSupportedContentTypes()
{
return [
'text/plain',
'text/html'
);
'text/html',
];
}

/**
* @param string $contentType
*
* @return bool
*/
protected static function supportsContentType($contentType) {
protected static function supportsContentType($contentType)
{
return in_array($contentType, self::getSupportedContentTypes());
}

/**
* Extract Mailjet specific header
* return an array of formatted data for Mailjet send API
* @param Swift_Mime_Message $message
*
* @param Swift_Mime_Message $message
* @param array $mailjetHeaders
*
* @return array
*/
protected static function prepareHeaders(Swift_Mime_Message $message, $mailjetHeaders) {
protected static function prepareHeaders(Swift_Mime_Message $message, $mailjetHeaders)
{
$messageHeaders = $message->getHeaders();

$mailjetData = array();

$mailjetData = [];

foreach (array_keys($mailjetHeaders) as $headerName) {
/** @var \Swift_Mime_Headers_MailboxHeader $value */
/** @var \Swift_Mime_Headers_UnstructuredHeader $value */
if (null !== $value = $messageHeaders->get($headerName)) {
// Handle custom headers
$mailjetData[$mailjetHeaders[$headerName]] = $value->getValue();
// remove Mailjet specific headers
$messageHeaders->removeAll($headerName);
}
}

return $mailjetData;
}

/**
* Extract user defined starting with X-*
*
* @param Swift_Mime_Message $message
*
* @return array
*/
protected static function findUserDefinedHeaders(Swift_Mime_Message $message) {
protected static function findUserDefinedHeaders(Swift_Mime_Message $message)
{
$messageHeaders = $message->getHeaders();
$userDefinedHeaders = array();
$userDefinedHeaders = [];
/* At this moment $messageHeaders is left with non-Mailjet specific headers
*
*
*/
/** @var \Swift_Mime_Headers_AbstractHeader $header */
foreach ($messageHeaders->getAll() as $header) {
if (0 === strpos($header->getFieldName(), 'X-')) {
$userDefinedHeaders[$header->getFieldName()] = $header->getValue();
$userDefinedHeaders[$header->getFieldName()] = $header->getFieldBody();
}
}

return $userDefinedHeaders;
}

/**
* Convert Swift_Mime_SimpleMessage into Mailjet Payload for send API
*
* @param Swift_Mime_Message $message
*
* @return array Mailjet Send Message
* @throws \Swift_SwiftException
*/
abstract public function getMailjetMessage(Swift_Mime_Message $message);

/**
* Returns the version of the message format
* @return version of the message format
*
* @return string version of the message format
*/
abstract public function getVersion();
}

0 comments on commit 97b77e6

Please sign in to comment.