summaryrefslogtreecommitdiff
path: root/src/Mail/Transport/Transport.php
blob: b83620a6bfa912676cb0a0658bd34c6bff3dad6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

namespace Engelsystem\Mail\Transport;

use Swift_Events_EventListener;
use Swift_Mime_SimpleMessage as SimpleMessage;
use Swift_Transport as SwiftTransport;

abstract class Transport implements SwiftTransport
{
    /**
     * Test if this Transport mechanism has started.
     *
     * @return bool
     */
    public function isStarted(): bool
    {
        return true;
    }

    /**
     * Start this Transport mechanism.
     */
    public function start()
    {
    }

    /**
     * Stop this Transport mechanism.
     */
    public function stop()
    {
    }

    /**
     * Check if this Transport mechanism is alive.
     *
     * If a Transport mechanism session is no longer functional, the method
     * returns FALSE. It is the responsibility of the developer to handle this
     * case and restart the Transport mechanism manually.
     *
     * @example
     *
     *   if (!$transport->ping()) {
     *      $transport->stop();
     *      $transport->start();
     *   }
     *
     * The Transport mechanism will be started, if it is not already.
     *
     * It is undefined if the Transport mechanism attempts to restart as long as
     * the return value reflects whether the mechanism is now functional.
     *
     * @return bool TRUE if the transport is alive
     */
    public function ping(): bool
    {
        return true;
    }

    /**
     * Register a plugin in the Transport.
     *
     * @param Swift_Events_EventListener $plugin
     */
    public function registerPlugin(Swift_Events_EventListener $plugin)
    {
    }

    /**
     * Returns a unified list of all recipients
     *
     * @param SimpleMessage $message
     * @return array
     */
    protected function allRecipients(SimpleMessage $message): array
    {
        return array_merge(
            (array)$message->getTo(),
            (array)$message->getCc(),
            (array)$message->getBcc()
        );
    }

    /**
     * Returns a concatenated list of mail recipients
     *
     * @param  SimpleMessage $message
     * @return string
     */
    protected function getTo(SimpleMessage $message): string
    {
        return $this->formatTo($this->allRecipients($message));
    }

    /**
     * @param array $recipients
     * @return string
     */
    protected function formatTo(array $recipients)
    {
        $list = [];
        foreach ($recipients as $address => $name) {
            $list[] = $name ? sprintf('%s <%s>', $name, $address) : $address;
        }

        return implode(',', $list);
    }
}