19declare(strict_types=1);
21use PHPMailer\PHPMailer\PHPMailer;
29 $mail =
new PHPMailer();
61 foreach ($mail->
getTo() as $recipients) {
62 $recipient_pieces = array_filter(array_map(
'trim', explode(
',', $recipients)));
63 foreach ($recipient_pieces as $recipient) {
64 if (!$this->
getMailer()->addAddress($recipient)) {
70 foreach ($mail->
getCc() as $carbon_copies) {
71 $cc_pieces = array_filter(array_map(
'trim', explode(
',', $carbon_copies)));
72 foreach ($cc_pieces as $carbon_copy) {
73 if (!$this->
getMailer()->addCC($carbon_copy)) {
79 foreach ($mail->
getBcc() as $blind_carbon_copies) {
80 $bcc_pieces = array_filter(array_map(
'trim', explode(
',', $blind_carbon_copies)));
81 foreach ($bcc_pieces as $blind_carbon_copy) {
82 if (!$this->
getMailer()->addBCC($blind_carbon_copy)) {
90 if ($mail->
getFrom()->hasReplyToAddress() && !$this->getMailer()->addReplyTo(
91 $mail->
getFrom()->getReplyToAddress(),
92 $mail->
getFrom()->getReplyToName()
96 if ($mail->
getFrom()->hasEnvelopFromAddress()) {
100 if (!$this->
getMailer()->setFrom($mail->
getFrom()->getFromAddress(), $mail->
getFrom()->getFromName(),
false)) {
105 if (!$this->
getMailer()->addAttachment($attachment[
'path'], $attachment[
'name'])) {
111 if (!$this->
getMailer()->addEmbeddedImage($image[
'path'], $image[
'cid'], $image[
'name'])) {
127 'Trying to delegate external email delivery:' .
128 ' Initiated by: {login} ({id}) ' .
129 '| To: {to} | CC: {cc} | BCC: {bcc} | Subject: {subject} ' .
130 '| From: {from_address} / {from_name} ' .
131 '| ReplyTo: {reply_to_address} / {reply_to_name} ' .
132 '| EnvelopeFrom: {envelope_from}',
136 'to' => implode(
', ', $mail->
getTo()),
137 'cc' => implode(
', ', $mail->
getCc()),
138 'bcc' => implode(
', ', $mail->
getBcc()),
140 'from_address' => $mail->
getFrom()->getFromAddress(),
141 'from_name' => $mail->
getFrom()->getFromName(),
142 'reply_to_address' => $mail->
getFrom()->getReplyToAddress(),
143 'reply_to_name' => $mail->
getFrom()->getReplyToName(),
144 'envelope_from' => $mail->
getFrom()->getEnvelopFromAddress(),
149 ->debug(
'Mail Alternative Body: {body}', [
'body' => $this->
getMailer()->AltBody]);
151 ->debug(
'Mail Body: {body}', [
'body' => $this->
getMailer()->Body]);
155 $this->mailer->Debugoutput =
static function (
string $message, $level):
void {
156 if (str_contains($message,
'Invalid address') ||
157 str_contains($message,
'Message body empty')) {
168 ->info(
'Successfully delegated external mail delivery');
170 if ($this->
getMailer()->ErrorInfo !==
'') {
172 '... with most recent errors: {error}',
173 [
'error' => $this->
getMailer()->ErrorInfo]
178 'Could not deliver external email: {error}',
179 [
'error' => $this->
getMailer()->ErrorInfo]
183 $this->event_handler->raise(
'components/ILIAS/Mail',
'externalEmailDelegated', [
static getLogger(string $a_component_id)
Get component logger.
__construct(protected ilSetting $settings, private readonly ilAppEventHandler $event_handler)
setMailer(PHPMailer $mailer)