Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
621 lars 1
<?php
2
 
3
namespace Illuminate\Mail\Transport;
4
 
5
use Aws\Exception\AwsException;
6
use Aws\SesV2\SesV2Client;
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 SesV2Transport extends AbstractTransport
14
{
15
    /**
16
     * The Amazon SES V2 instance.
17
     *
18
     * @var \Aws\SesV2\SesV2Client
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 V2 transport instance.
31
     *
32
     * @param  \Aws\SesV2\SesV2Client  $ses
33
     * @param  array  $options
34
     * @return void
35
     */
36
    public function __construct(SesV2Client $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->sendEmail(
61
                array_merge(
62
                    $options, [
63
                        'ReplyToAddresses' => [$message->getEnvelope()->getSender()->toString()],
64
                        'Destination' => [
65
                            'ToAddresses' => collect($message->getEnvelope()->getRecipients())
66
                                    ->map
67
                                    ->toString()
68
                                    ->values()
69
                                    ->all(),
70
                        ],
71
                        'Content' => [
72
                            'Raw' => [
73
                                'Data' => $message->toString(),
74
                            ],
75
                        ],
76
                    ]
77
                )
78
            );
79
        } catch (AwsException $e) {
80
            $reason = $e->getAwsErrorMessage() ?? $e->getMessage();
81
 
82
            throw new Exception(
83
                sprintf('Request to AWS SES V2 API failed. Reason: %s.', $reason),
84
                is_int($e->getCode()) ? $e->getCode() : 0,
85
                $e
86
            );
87
        }
88
 
89
        $messageId = $result->get('MessageId');
90
 
91
        $message->getOriginalMessage()->getHeaders()->addHeader('X-Message-ID', $messageId);
92
        $message->getOriginalMessage()->getHeaders()->addHeader('X-SES-Message-ID', $messageId);
93
    }
94
 
95
    /**
96
     * Get the Amazon SES V2 client for the SesV2Transport instance.
97
     *
98
     * @return \Aws\SesV2\SesV2Client
99
     */
100
    public function ses()
101
    {
102
        return $this->ses;
103
    }
104
 
105
    /**
106
     * Get the transmission options being used by the transport.
107
     *
108
     * @return array
109
     */
110
    public function getOptions()
111
    {
112
        return $this->options;
113
    }
114
 
115
    /**
116
     * Set the transmission options being used by the transport.
117
     *
118
     * @param  array  $options
119
     * @return array
120
     */
121
    public function setOptions(array $options)
122
    {
123
        return $this->options = $options;
124
    }
125
 
126
    /**
127
     * Get the string representation of the transport.
128
     *
129
     * @return string
130
     */
131
    public function __toString(): string
132
    {
133
        return 'ses-v2';
134
    }
135
}