Lines
100.00%
24 / 24
Methods
100.00%
2 / 2
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| setAuthConfig | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| send | 100.00% | 20 / 20 | 100.00% | 1 / 1 | 9 | ||
| 31 | class Smtp implements MailUserAgentInterface | |
| 32 | { | |
| 33 | private string $user; | |
| 34 | ||
| 35 | private MailerInterface $mailer; | |
| 36 | ||
| 37 | public function setAuthConfig( | |
| 38 | string $server, | |
| 39 | string $user = '', | |
| 40 | #[\SensitiveParameter] | |
| 41 | string $password = '', | |
| 42 | int $port = 25, | |
| 43 | bool $disableTlsPeerVerification = false, | |
| 44 | ): void { | |
| 45 | $dsn = sprintf('smtp://%s:%s@%s:%d', $this->user = $user, urlencode($password), $server, $port); | |
| 46 | ||
| 47 | if ($disableTlsPeerVerification) { | |
| 48 | $dsn .= '?verify_peer=0'; | |
| 49 | } | |
| 50 | ||
| 51 | $this->mailer = new Mailer(Transport::fromDsn($dsn)); | |
| 52 | } | |
| 53 | ||
| 54 | /** | |
| 55 | * @param array<string, int|string|null> $headers | |
| 56 | * @throws TransportExceptionInterface | |
| 57 | */ | |
| 58 | public function send(string $recipients, array $headers, string $body): int | |
| 59 | { | |
| 60 | $sender = ''; | |
| 61 | if ( | |
| 62 | 'WIN' !== strtoupper(string: substr(string: PHP_OS, offset: 0, length: 3)) | |
| 63 | && !ini_get(option: 'safe_mode') | |
| 64 | && array_key_exists('Return-Path', $headers) | |
| 65 | ) { | |
| 66 | $sender = str_replace(search: ['<', '>'], replace: '', subject: (string) $headers['Return-Path']); | |
| 67 | unset($headers['Return-Path']); | |
| 68 | } | |
| 69 | ||
| 70 | $email = new Email() | |
| 71 | ->from($sender === '' ? $this->user : $sender) | |
| 72 | ->to($recipients) | |
| 73 | ->subject((string) ($headers['Subject'] ?? '')) | |
| 74 | ->text($body) | |
| 75 | ->html($body); | |
| 76 | ||
| 77 | if (array_key_exists('CC', $headers) || array_key_exists('Cc', $headers)) { | |
| 78 | $email->cc((string) ($headers['CC'] ?? $headers['Cc'] ?? '')); | |
| 79 | } | |
| 80 | ||
| 81 | if (array_key_exists('Bcc', $headers)) { | |
| 82 | $email->bcc((string) $headers['Bcc']); | |
| 83 | } | |
| 84 | ||
| 85 | if (array_key_exists('Reply-To', $headers)) { | |
| 86 | $email->replyTo((string) $headers['Reply-To']); | |
| 87 | } | |
| 88 | ||
| 89 | $this->mailer->send($email); | |
| 90 | ||
| 91 | return 1; | |
| 92 | } | |
| 93 | } |