Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect method when user defined headers are set. #3

Merged
merged 1 commit into from
Jul 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}