Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
TYPO3
sg_mail
Commits
7a8d385f
Commit
7a8d385f
authored
Jun 07, 2019
by
Torsten Oppermann
Browse files
[TASK] Code cleanups, encapsulate some code for better readability
parent
a7e8c34d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Classes/Service/MailTemplateService.php
View file @
7a8d385f
...
...
@@ -177,12 +177,12 @@ class MailTemplateService {
$this
->
persistenceManager
=
$this
->
objectManager
->
get
(
PersistenceManager
::
class
);
// use defaultMailFromAddress if it is provided in LocalConfiguration.php; use the sg_mail TS setting as fallback
if
(
filter_var
(
$GLOBALS
[
'TYPO3_CONF_VARS'
][
'MAIL'
][
'defaultMailFromAddress'
],
FILTER_VALIDATE_EMAIL
))
{
if
(
\
filter_var
(
$GLOBALS
[
'TYPO3_CONF_VARS'
][
'MAIL'
][
'defaultMailFromAddress'
],
FILTER_VALIDATE_EMAIL
))
{
$this
->
fromAddress
=
$GLOBALS
[
'TYPO3_CONF_VARS'
][
'MAIL'
][
'defaultMailFromAddress'
];
}
else
{
$this
->
fromAddress
=
$tsSettings
[
'mail'
][
'default'
][
'from'
];
if
(
!
filter_var
(
$tsSettings
[
'mail'
][
'default'
][
'from'
],
FILTER_VALIDATE_EMAIL
))
{
if
(
!
\
filter_var
(
$tsSettings
[
'mail'
][
'default'
][
'from'
],
FILTER_VALIDATE_EMAIL
))
{
$this
->
fromAddress
=
'noreply@example.org'
;
}
else
{
$this
->
fromAddress
=
$tsSettings
[
'mail'
][
'default'
][
'from'
];
...
...
@@ -194,12 +194,11 @@ class MailTemplateService {
}
$this
->
mailMessage
->
setFrom
(
$this
->
fromAddress
,
$this
->
fromName
);
$this
->
bccAddresses
=
GeneralUtility
::
trimExplode
(
','
,
$tsSettings
[
'mail'
][
'default'
][
'bcc'
]);
$this
->
ccAddresses
=
GeneralUtility
::
trimExplode
(
','
,
$tsSettings
[
'mail'
][
'default'
][
'cc'
]);
foreach
(
$this
->
bccAddresses
as
$index
=>
$email
)
{
if
(
!
filter_var
(
$email
,
FILTER_VALIDATE_EMAIL
))
{
if
(
!
\
filter_var
(
$email
,
FILTER_VALIDATE_EMAIL
))
{
unset
(
$this
->
bccAddresses
[
$index
]);
}
}
...
...
@@ -348,7 +347,7 @@ class MailTemplateService {
*/
public
function
setMarkers
(
array
$markers
):
MailTemplateService
{
$this
->
markers
=
$markers
;
foreach
(
$markers
as
$key
=>
$currentMarker
)
{
foreach
(
$markers
as
$key
=>
$currentMarker
)
{
if
(
!
\
is_array
(
$currentMarker
)
||
!
isset
(
$currentMarker
[
'markerLabel'
]))
{
continue
;
}
...
...
@@ -469,9 +468,8 @@ class MailTemplateService {
*/
public
static
function
getDefaultTemplateMarker
(
$translationKey
,
array
$marker
,
$extensionKey
=
'sg_mail'
):
array
{
$languagePath
=
'LLL:EXT:'
.
$extensionKey
.
'/Resources/Private/Language/locallang.xlf:'
.
$translationKey
;
// Need the key for translations
if
(
trim
(
$extensionKey
)
===
''
)
{
if
(
\
trim
(
$extensionKey
)
===
''
)
{
return
[];
}
...
...
@@ -540,9 +538,8 @@ class MailTemplateService {
$isTemplateBlacklisted
=
self
::
isTemplateBlacklisted
(
$this
->
extensionKey
,
$this
->
templateName
,
$siteRootId
);
if
(
$isTemplateBlacklisted
)
{
// @TODO: This needs to be changed, because if the template is blacklisted, the email can not be sent
$success
=
TRUE
;
return
$success
;
// @TODO throw error or log ?
return
FALSE
;
}
/** @var Template $template */
...
...
@@ -574,8 +571,8 @@ class MailTemplateService {
// only standard template file is considered since version 4.1
$defaultTemplateFile
=
$templatePath
.
'template.html'
;
if
(
file_exists
(
$defaultTemplateFile
))
{
$defaultTemplateContent
=
file_get_contents
(
$defaultTemplateFile
);
if
(
\
file_exists
(
$defaultTemplateFile
))
{
$defaultTemplateContent
=
\
file_get_contents
(
$defaultTemplateFile
);
}
else
{
// use configured default html template
/** @var TypoScriptSettingsService $typoScriptSettingsService */
...
...
@@ -585,16 +582,15 @@ class MailTemplateService {
$tsSettings
[
'mail'
][
'defaultHtmlTemplate'
]
);
if
(
file_exists
(
$defaultTemplateFile
))
{
$defaultTemplateContent
=
file_get_contents
(
$defaultTemplateFile
);
if
(
\
file_exists
(
$defaultTemplateFile
))
{
$defaultTemplateContent
=
\
file_get_contents
(
$defaultTemplateFile
);
}
else
{
return
$success
;
return
FALSE
;
}
}
}
}
elseif
(
filter_var
(
$template
->
getToAddress
(),
FILTER_VALIDATE_EMAIL
))
{
$this
->
setToAddresses
(
trim
(
$template
->
getToAddress
()));
}
elseif
(
\
filter_var
(
$template
->
getToAddress
(),
FILTER_VALIDATE_EMAIL
))
{
$this
->
setToAddresses
(
\
trim
(
$template
->
getToAddress
()));
}
if
(
$isPreview
)
{
...
...
@@ -605,7 +601,7 @@ class MailTemplateService {
foreach
(
$markerArray
as
$marker
)
{
$markerPath
=
GeneralUtility
::
trimExplode
(
'.'
,
$marker
[
'marker'
]);
$temporaryMarkerArray
=
[];
foreach
(
array_reverse
(
$markerPath
)
as
$index
=>
$markerPathSegment
)
{
foreach
(
\
array_reverse
(
$markerPath
)
as
$index
=>
$markerPathSegment
)
{
if
(
$index
===
0
)
{
if
(
$marker
[
'markerLabel'
])
{
$markerPathSegment
=
$marker
[
'markerLabel'
];
...
...
@@ -623,7 +619,7 @@ class MailTemplateService {
}
}
/** @noinspection SlowArrayOperationsInLoopInspection */
$previewMarker
=
array_merge_recursive
(
$previewMarker
,
$temporaryMarkerArray
);
$previewMarker
=
\
array_merge_recursive
(
$previewMarker
,
$temporaryMarkerArray
);
}
$this
->
setIgnoreMailQueue
(
TRUE
);
$this
->
setMarkers
(
$previewMarker
);
...
...
@@ -665,8 +661,8 @@ class MailTemplateService {
// insert <br> tags, but replace every instance of three or more successive breaks with just two.
$emailBody
=
$emailView
->
render
();
$emailBody
=
nl2br
(
$emailBody
);
$emailBody
=
preg_replace
(
'/(<br[\s]?[\/]?>[\s]*){3,}/'
,
'<br><br>'
,
$emailBody
);
$emailBody
=
\
nl2br
(
$emailBody
);
$emailBody
=
\
preg_replace
(
'/(<br[\s]?[\/]?>[\s]*){3,}/'
,
'<br><br>'
,
$emailBody
);
$mail
=
$this
->
addMailToMailQueue
(
$this
->
extensionKey
,
$this
->
templateName
,
$subject
,
$emailBody
,
$this
->
priority
,
...
...
@@ -751,59 +747,58 @@ class MailTemplateService {
$mailRepository
=
$this
->
objectManager
->
get
(
MailRepository
::
class
);
/** @var Mail $mailToSend */
$mailToSend
=
$mailRepository
->
findOneByUid
(
$uid
);
if
(
!
$mailToSend
||
$mailToSend
->
getBlacklisted
())
{
return
FALSE
;
}
if
(
$mailToSend
&&
!
$mailToSend
->
getBlacklisted
())
{
$this
->
mailMessage
->
setBody
(
$mailToSend
->
getMailBody
(),
'text/html'
);
$plaintextService
=
GeneralUtility
::
makeInstance
(
PlaintextService
::
class
);
$plaintextBody
=
$plaintextService
->
makePlain
(
$mailToSend
->
getMailBody
());
$this
->
mailMessage
->
addPart
(
$plaintextBody
,
'text/plain'
);
$toAddresses
=
trim
(
$mailToSend
->
getToAddress
());
$addressesArray
=
GeneralUtility
::
trimExplode
(
','
,
$toAddresses
,
TRUE
);
if
(
\
count
(
$addressesArray
)
>
1
)
{
$toAddresses
=
$addressesArray
;
}
$this
->
mailMessage
->
setTo
(
$toAddresses
);
$this
->
mailMessage
->
setFrom
(
$mailToSend
->
getFromAddress
(),
$mailToSend
->
getFromName
());
$this
->
mailMessage
->
setSubject
(
$mailToSend
->
getMailSubject
());
$this
->
mailMessage
->
setBody
(
$mailToSend
->
getMailBody
(),
'text/html'
);
$plaintextService
=
GeneralUtility
::
makeInstance
(
PlaintextService
::
class
);
$plaintextBody
=
$plaintextService
->
makePlain
(
$mailToSend
->
getMailBody
());
$this
->
mailMessage
->
addPart
(
$plaintextBody
,
'text/plain'
);
$toAddresses
=
\
trim
(
$mailToSend
->
getToAddress
());
$addressesArray
=
GeneralUtility
::
trimExplode
(
','
,
$toAddresses
,
TRUE
);
if
(
\
count
(
$addressesArray
)
>
1
)
{
$toAddresses
=
$addressesArray
;
}
$this
->
mailMessage
->
setTo
(
$toAddresses
);
$this
->
mailMessage
->
setFrom
(
$mailToSend
->
getFromAddress
(),
$mailToSend
->
getFromName
());
$this
->
mailMessage
->
setSubject
(
$mailToSend
->
getMailSubject
());
if
(
$mailToSend
->
getBccAddresses
())
{
$this
->
mailMessage
->
setBcc
(
GeneralUtility
::
trimExplode
(
','
,
$mailToSend
->
getBccAddresses
()));
}
if
(
$mailToSend
->
getBccAddresses
())
{
$this
->
mailMessage
->
setBcc
(
GeneralUtility
::
trimExplode
(
','
,
$mailToSend
->
getBccAddresses
()));
}
if
(
$mailToSend
->
getCcAddresses
())
{
$this
->
mailMessage
->
setCc
(
GeneralUtility
::
trimExplode
(
','
,
$mailToSend
->
getCcAddresses
()));
}
if
(
$mailToSend
->
getCcAddresses
())
{
$this
->
mailMessage
->
setCc
(
GeneralUtility
::
trimExplode
(
','
,
$mailToSend
->
getCcAddresses
()));
}
if
(
$mailToSend
->
getReplyTo
())
{
$this
->
mailMessage
->
setReplyTo
(
$mailToSend
->
getReplyTo
());
}
$attachments
=
$mailToSend
->
getAttachments
();
if
(
$attachments
->
count
()
>
0
)
{
foreach
(
$attachments
as
$attachment
)
{
/**
* @var FileReference $attachment
*/
$file
=
$attachment
->
getOriginalResource
()
->
getOriginalFile
();
$this
->
mailMessage
->
attach
(
\
Swift_Attachment
::
newInstance
(
$file
->
getContents
(),
$file
->
getName
(),
$file
->
getMimeType
())
);
}
}
$dateTime
=
new
DateTime
();
if
((
int
)
$mailToSend
->
getSendingTime
()
===
0
)
{
$mailToSend
->
setSendingTime
(
$dateTime
->
getTimestamp
());
}
$mailToSend
->
setLastSendingTime
(
$dateTime
->
getTimestamp
());
$success
=
$this
->
mailMessage
->
send
();
if
(
$success
)
{
$mailRepository
->
update
(
$mailToSend
);
}
else
{
$this
->
mailMessage
->
getFailedRecipients
();
if
(
$mailToSend
->
getReplyTo
())
{
$this
->
mailMessage
->
setReplyTo
(
$mailToSend
->
getReplyTo
());
}
$attachments
=
$mailToSend
->
getAttachments
();
if
(
$attachments
->
count
()
>
0
)
{
foreach
(
$attachments
as
$attachment
)
{
/**
* @var FileReference $attachment
*/
$file
=
$attachment
->
getOriginalResource
()
->
getOriginalFile
();
$this
->
mailMessage
->
attach
(
\
Swift_Attachment
::
newInstance
(
$file
->
getContents
(),
$file
->
getName
(),
$file
->
getMimeType
())
);
}
return
$success
;
}
return
NULL
;
$dateTime
=
new
DateTime
();
if
((
int
)
$mailToSend
->
getSendingTime
()
===
0
)
{
$mailToSend
->
setSendingTime
(
$dateTime
->
getTimestamp
());
}
$mailToSend
->
setLastSendingTime
(
$dateTime
->
getTimestamp
());
$success
=
$this
->
mailMessage
->
send
();
if
(
$success
)
{
$mailRepository
->
update
(
$mailToSend
);
}
else
{
$this
->
mailMessage
->
getFailedRecipients
();
}
return
$success
;
}
/**
...
...
@@ -820,23 +815,34 @@ class MailTemplateService {
$fromName
=
$GLOBALS
[
'TYPO3_CONF_VARS'
][
'MAIL'
][
'defaultMailFromName'
];
}
$fromMail
=
\
trim
(
$template
->
getFromMail
());
if
(
!
filter_var
(
$fromMail
,
FILTER_VALIDATE_EMAIL
))
{
$fromMail
=
$this
->
getValidFromMail
(
\
trim
(
$template
->
getFromMail
()));
$this
->
setFromAddress
(
$fromMail
,
$fromName
);
$this
->
setCcAddresses
(
$template
->
getCc
());
$this
->
setBccAddresses
(
$template
->
getBcc
());
$this
->
setReplyToAddress
(
$template
->
getReplyTo
());
$this
->
setFromName
(
$fromName
);
}
/**
* Sets the fromMail property of the mailTemplateService.
* Checks validity and uses all available fallbacks
*
* @param string $fromMail
* @return string
*/
private
function
getValidFromMail
(
$fromMail
):
string
{
$fromMail
=
\
trim
(
$fromMail
);
if
(
!
\
filter_var
(
$fromMail
,
FILTER_VALIDATE_EMAIL
))
{
$fromMail
=
$this
->
fromAddress
;
}
if
(
!
filter_var
(
$fromMail
,
FILTER_VALIDATE_EMAIL
))
{
if
(
!
\
filter_var
(
$fromMail
,
FILTER_VALIDATE_EMAIL
))
{
$fromMail
=
$GLOBALS
[
'TYPO3_CONF_VARS'
][
'MAIL'
][
'defaultMailFromAddress'
];
if
(
!
filter_var
(
$GLOBALS
[
'TYPO3_CONF_VARS'
][
'MAIL'
][
'defaultMailFromAddress'
],
FILTER_VALIDATE_EMAIL
))
{
if
(
!
\
filter_var
(
$GLOBALS
[
'TYPO3_CONF_VARS'
][
'MAIL'
][
'defaultMailFromAddress'
],
FILTER_VALIDATE_EMAIL
))
{
$fromMail
=
'noreply@example.com'
;
}
}
$this
->
setFromAddress
(
$fromMail
,
$fromName
);
$this
->
setCcAddresses
(
$template
->
getCc
());
$this
->
setBccAddresses
(
$template
->
getBcc
());
$this
->
setReplyToAddress
(
$template
->
getReplyTo
());
$this
->
setFromName
(
$fromName
);
$this
->
setReplyToAddress
(
$template
->
getReplyTo
());
return
$fromMail
;
}
/**
...
...
@@ -863,7 +869,7 @@ class MailTemplateService {
$valueAsString
=
$value
?
'true'
:
'false'
;
$allMarker
.
=
$key
.
': '
.
$valueAsString
.
PHP_EOL
;
}
elseif
(
\
is_object
(
$value
))
{
if
(
method_exists
(
$value
,
'__toString'
))
{
if
(
\
method_exists
(
$value
,
'__toString'
))
{
$allMarker
.
=
$key
.
': '
.
$value
->
__toString
()
.
PHP_EOL
;
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment