// these headers can't be overwritten according to Sendgrid docs
// see https://sendgrid.api-docs.io/v3.0/mail-send/mail-send-errors#-Headers-Errors
$headersToBypass =
['x-sg-id', 'x-sg-eid', 'received', 'dkim-signature', 'content-transfer-encoding', 'from', 'to', 'cc', 'bcc', 'subject', 'content-type', 'reply-to'
];
foreach ($email->
getHeaders()->
all() as $name =>
$header) { if (\
in_array($name,
$headersToBypass, true
)) { continue;
} if ($header instanceof TagHeader
) { if (10 === \
count($categories)) { throw new TransportException(sprintf('Too many "%s" instances present in the email headers. Sendgrid does not accept more than 10 categories on an email.', TagHeader::
class));
} $categories[] =
mb_substr($header->
getValue(), 0, 255
);
} elseif ($header instanceof MetadataHeader
) { $customArguments[$header->
getKey()] =
$header->
getValue();
} else { $payload['headers'
][$header->
getName()] =
$header->
getBodyAsString();
} } if (\
count($categories) > 0
) { $payload['categories'
] =
$categories;
}