Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
addMailboxHeader example
public
function
testSenderFromHeaders
(
)
{
$headers
=
new
Headers
(
)
;
$headers
->
addPathHeader
(
'Return-Path',
$return
=
new
Address
(
'return@symfony.com', 'return'
)
)
;
$headers
->
addMailboxListHeader
(
'To',
[
'to@symfony.com'
]
)
;
$e
= Envelope::
create
(
new
Message
(
$headers
)
)
;
$this
->
assertEquals
(
$return
,
$e
->
getSender
(
)
)
;
$headers
=
new
Headers
(
)
;
$headers
->
addMailboxHeader
(
'Sender',
$sender
=
new
Address
(
'sender@symfony.com', 'sender'
)
)
;
$headers
->
addMailboxListHeader
(
'To',
[
'to@symfony.com'
]
)
;
$e
= Envelope::
create
(
new
Message
(
$headers
)
)
;
$this
->
assertEquals
(
$sender
,
$e
->
getSender
(
)
)
;
$headers
=
new
Headers
(
)
;
$headers
->
addMailboxListHeader
(
'From',
[
$from
=
new
Address
(
'from@symfony.com', 'from'
)
, 'some@symfony.com'
]
)
;
$headers
->
addMailboxListHeader
(
'To',
[
'to@symfony.com'
]
)
;
$e
= Envelope::
create
(
new
Message
(
$headers
)
)
;
$this
->
assertEquals
(
$from
,
$e
->
getSender
(
)
)
;
}
if
(
!
$headers
->
has
(
'MIME-Version'
)
)
{
$headers
->
addTextHeader
(
'MIME-Version', '1.0'
)
;
}
if
(
!
$headers
->
has
(
'Date'
)
)
{
$headers
->
addDateHeader
(
'Date',
new
\
DateTimeImmutable
(
)
)
;
}
// determine the "real" sender
if
(
!
$headers
->
has
(
'Sender'
)
&& \
count
(
$froms
=
$headers
->
get
(
'From'
)
->
getAddresses
(
)
)
> 1
)
{
$headers
->
addMailboxHeader
(
'Sender',
$froms
[
0
]
)
;
}
if
(
!
$headers
->
has
(
'Message-ID'
)
)
{
$headers
->
addIdHeader
(
'Message-ID',
$this
->
generateMessageId
(
)
)
;
}
// remove the Bcc field which should NOT be part of the sent message
$headers
->
remove
(
'Bcc'
)
;
return
$headers
;
}
}
public
function
onMessage
(
MessageEvent
$event
)
: void
{
if
(
$this
->sender
)
{
$event
->
getEnvelope
(
)
->
setSender
(
$this
->sender
)
;
$message
=
$event
->
getMessage
(
)
;
if
(
$message
instanceof Message
)
{
if
(
!
$message
->
getHeaders
(
)
->
has
(
'Sender'
)
&& !
$message
->
getHeaders
(
)
->
has
(
'From'
)
)
{
$message
->
getHeaders
(
)
->
addMailboxHeader
(
'Sender',
$this
->sender
)
;
}
}
}
if
(
$this
->recipients
)
{
$event
->
getEnvelope
(
)
->
setRecipients
(
$this
->recipients
)
;
}
}
public
static
function
getSubscribedEvents
(
)
: array
{
if
(
!
$headers
->
has
(
'MIME-Version'
)
)
{
$headers
->
addTextHeader
(
'MIME-Version', '1.0'
)
;
}
if
(
!
$headers
->
has
(
'Date'
)
)
{
$headers
->
addDateHeader
(
'Date',
new
\
DateTimeImmutable
(
)
)
;
}
// determine the "real" sender
if
(
!
$headers
->
has
(
'Sender'
)
&& \
count
(
$froms
=
$headers
->
get
(
'From'
)
->
getAddresses
(
)
)
> 1
)
{
$headers
->
addMailboxHeader
(
'Sender',
$froms
[
0
]
)
;
}
if
(
!
$headers
->
has
(
'Message-ID'
)
)
{
$headers
->
addIdHeader
(
'Message-ID',
$this
->
generateMessageId
(
)
)
;
}
// remove the Bcc field which should NOT be part of the sent message
$headers
->
remove
(
'Bcc'
)
;
return
$headers
;
}
$message
=
new
Message
(
)
;
$message
->
getHeaders
(
)
->
addMailboxListHeader
(
'From',
[
'fabien@symfony.com'
]
)
;
$this
->
assertNull
(
$message
->
getPreparedHeaders
(
)
->
get
(
'Sender'
)
)
;
$message
=
new
Message
(
)
;
$message
->
getHeaders
(
)
->
addMailboxListHeader
(
'From',
[
'fabien@symfony.com', 'lucas@symfony.com'
]
)
;
$this
->
assertEquals
(
'fabien@symfony.com',
$message
->
getPreparedHeaders
(
)
->
get
(
'Sender'
)
->
getAddress
(
)
->
getAddress
(
)
)
;
$message
=
new
Message
(
)
;
$message
->
getHeaders
(
)
->
addMailboxListHeader
(
'From',
[
'fabien@symfony.com', 'lucas@symfony.com'
]
)
;
$message
->
getHeaders
(
)
->
addMailboxHeader
(
'Sender', 'thomas@symfony.com'
)
;
$this
->
assertEquals
(
'thomas@symfony.com',
$message
->
getPreparedHeaders
(
)
->
get
(
'Sender'
)
->
getAddress
(
)
->
getAddress
(
)
)
;
}
public
function
testToString
(
)
{
$message
=
new
Message
(
)
;
$message
->
getHeaders
(
)
->
addMailboxListHeader
(
'From',
[
'fabien@symfony.com'
]
)
;
$expected
= <<<EOF From: fabien@symfony.com MIME-Version: 1.0 Date: %s Message-ID: <%s@symfony.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable