Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Mail\Transport;
4
 
5
use Aws\Exception\AwsException;
6
use Aws\Ses\SesClient;
7
use Exception;
8
use Symfony\Component\Mailer\Header\MetadataHeader;
9
use Symfony\Component\Mailer\SentMessage;
10
use Symfony\Component\Mailer\Transport\AbstractTransport;
11
use Symfony\Component\Mime\Message;
12
 
13
class SesTransport extends AbstractTransport
14
{
15
    /**
16
     * The Amazon SES instance.
17
     *
18
     * @var \Aws\Ses\SesClient
19
     */
20
    protected $ses;
21
 
22
    /**
23
     * The Amazon SES transmission options.
24
     *
25
     * @var array
26
     */
27
    protected $options = [];
28
 
29
    /**
30
     * Create a new SES transport instance.
31
     *
32
     * @param  \Aws\Ses\SesClient  $ses
33
     * @param  array  $options
34
     * @return void
35
     */
36
    public function __construct(SesClient $ses, $options = [])
37
    {
38
        $this->ses = $ses;
39
        $this->options = $options;
40
 
41
        parent::__construct();
42
    }
43
 
44
    /**
45
     * {@inheritDoc}
46
     */
47
    protected function doSend(SentMessage $message): void
48
    {
49
        $options = $this->options;
50
 
51
        if ($message->getOriginalMessage() instanceof Message) {
52
            foreach ($message->getOriginalMessage()->getHeaders()->all() as $header) {
53
                if ($header instanceof MetadataHeader) {
54
                    $options['Tags'][] = ['Name' => $header->getKey(), 'Value' => $header->getValue()];
55
                }
56
            }
57
        }
58
 
59
        try {
60
            $result = $this->ses->sendRawEmail(
61
                array_merge(
62
                    $options, [
63
                        'Source' => $message->getEnvelope()->getSender()->toString(),
64
                        'Destinations' => collect($message->getEnvelope()->getRecipients())
65
                                ->map
66
                                ->toString()
67
                                ->values()
68
                                ->all(),
69
                        'RawMessage' => [
70
                            'Data' => $message->toString(),
71
                        ],
72
                    ]
73
                )
74
            );
75
        } catch (AwsException $e) {
76
            $reason = $e->getAwsErrorMessage() ?? $e->getMessage();
77
 
78
            throw new Exception(
79
                sprintf('Request to AWS SES API failed. Reason: %s.', $reason),
80
                is_int($e->getCode()) ? $e->getCode() : 0,
81
                $e
82
            );
83
        }
84
 
85
        $messageId = $result->get('MessageId');
86
 
87
        $message->getOriginalMessage()->getHeaders()->addHeader('X-Message-ID', $messageId);
88
        $message->getOriginalMessage()->getHeaders()->addHeader('X-SES-Message-ID', $messageId);
89
    }
90
 
91
    /**
92
     * Get the string representation of the transport.
93
     *
94
     * @return string
95
     */
96
    public function __toString(): string
97
    {
98
        return 'ses';
99
    }
100
 
101
    /**
102
     * Get the Amazon SES client for the SesTransport instance.
103
     *
104
     * @return \Aws\Ses\SesClient
105
     */
106
    public function ses()
107
    {
108
        return $this->ses;
109
    }
110
 
111
    /**
112
     * Get the transmission options being used by the transport.
113
     *
114
     * @return array
115
     */
116
    public function getOptions()
117
    {
118
        return $this->options;
119
    }
120
 
121
    /**
122
     * Set the transmission options being used by the transport.
123
     *
124
     * @param  array  $options
125
     * @return array
126
     */
127
    public function setOptions(array $options)
128
    {
129
        return $this->options = $options;
130
    }
131
}