validation-bugsnag-email

This commit is contained in:
RafficMohammed
2023-01-31 13:17:59 +05:30
parent 2ec836b447
commit 9dd3f53910
769 changed files with 20242 additions and 14060 deletions

View File

@@ -0,0 +1,180 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer\Bridge\Mailgun\Transport;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractApiTransport;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
/**
* @author Kevin Verschaeve
*/
class MailgunApiTransport extends AbstractApiTransport
{
private const HOST = 'api.%region_dot%mailgun.net';
private string $key;
private string $domain;
private ?string $region;
public function __construct(string $key, string $domain, string $region = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
$this->key = $key;
$this->domain = $domain;
$this->region = $region;
parent::__construct($client, $dispatcher, $logger);
}
public function __toString(): string
{
return sprintf('mailgun+api://%s?domain=%s', $this->getEndpoint(), $this->domain);
}
protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface
{
$body = new FormDataPart($this->getPayload($email, $envelope));
$headers = [];
foreach ($body->getPreparedHeaders()->all() as $header) {
$headers[] = $header->toString();
}
$endpoint = sprintf('%s/v3/%s/messages', $this->getEndpoint(), urlencode($this->domain));
$response = $this->client->request('POST', 'https://'.$endpoint, [
'auth_basic' => 'api:'.$this->key,
'headers' => $headers,
'body' => $body->bodyToIterable(),
]);
try {
$statusCode = $response->getStatusCode();
$result = $response->toArray(false);
} catch (DecodingExceptionInterface) {
throw new HttpTransportException('Unable to send an email: '.$response->getContent(false).sprintf(' (code %d).', $statusCode), $response);
} catch (TransportExceptionInterface $e) {
throw new HttpTransportException('Could not reach the remote Mailgun server.', $response, 0, $e);
}
if (200 !== $statusCode) {
throw new HttpTransportException('Unable to send an email: '.$result['message'].sprintf(' (code %d).', $statusCode), $response);
}
$sentMessage->setMessageId($result['id']);
return $response;
}
private function getPayload(Email $email, Envelope $envelope): array
{
$headers = $email->getHeaders();
$html = $email->getHtmlBody();
if (null !== $html && \is_resource($html)) {
if (stream_get_meta_data($html)['seekable'] ?? false) {
rewind($html);
}
$html = stream_get_contents($html);
}
[$attachments, $inlines, $html] = $this->prepareAttachments($email, $html);
$payload = [
'from' => $envelope->getSender()->toString(),
'to' => implode(',', $this->stringifyAddresses($this->getRecipients($email, $envelope))),
'subject' => $email->getSubject(),
'attachment' => $attachments,
'inline' => $inlines,
];
if ($emails = $email->getCc()) {
$payload['cc'] = implode(',', $this->stringifyAddresses($emails));
}
if ($emails = $email->getBcc()) {
$payload['bcc'] = implode(',', $this->stringifyAddresses($emails));
}
if ($email->getTextBody()) {
$payload['text'] = $email->getTextBody();
}
if ($html) {
$payload['html'] = $html;
}
$headersToBypass = ['from', 'to', 'cc', 'bcc', 'subject', 'content-type'];
foreach ($headers->all() as $name => $header) {
if (\in_array($name, $headersToBypass, true)) {
continue;
}
if ($header instanceof TagHeader) {
$payload[] = ['o:tag' => $header->getValue()];
continue;
}
if ($header instanceof MetadataHeader) {
$payload['v:'.$header->getKey()] = $header->getValue();
continue;
}
// Check if it is a valid prefix or header name according to Mailgun API
$prefix = substr($name, 0, 2);
if (\in_array($prefix, ['h:', 't:', 'o:', 'v:']) || \in_array($name, ['recipient-variables', 'template', 'amp-html'])) {
$headerName = $header->getName();
} else {
$headerName = 'h:'.$header->getName();
}
$payload[$headerName] = $header->getBodyAsString();
}
return $payload;
}
private function prepareAttachments(Email $email, ?string $html): array
{
$attachments = $inlines = [];
foreach ($email->getAttachments() as $attachment) {
$headers = $attachment->getPreparedHeaders();
if ('inline' === $headers->getHeaderBody('Content-Disposition')) {
// replace the cid with just a file name (the only supported way by Mailgun)
if ($html) {
$filename = $headers->getHeaderParameter('Content-Disposition', 'filename');
$new = basename($filename);
$html = str_replace('cid:'.$filename, 'cid:'.$new, $html);
$p = new \ReflectionProperty($attachment, 'filename');
$p->setValue($attachment, $new);
}
$inlines[] = $attachment;
} else {
$attachments[] = $attachment;
}
}
return [$attachments, $inlines, $html];
}
private function getEndpoint(): ?string
{
$host = $this->host ?: str_replace('%region_dot%', 'us' !== ($this->region ?: 'us') ? $this->region.'.' : '', self::HOST);
return $host.($this->port ? ':'.$this->port : '');
}
}

View File

@@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer\Bridge\Mailgun\Transport;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mime\Message;
use Symfony\Component\Mime\RawMessage;
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
trait MailgunHeadersTrait
{
public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
{
if ($message instanceof Message) {
$this->addMailgunHeaders($message);
}
return parent::send($message, $envelope);
}
private function addMailgunHeaders(Message $message): void
{
$headers = $message->getHeaders();
$metadata = [];
foreach ($headers->all() as $name => $header) {
if ($header instanceof TagHeader) {
$headers->addTextHeader('X-Mailgun-Tag', $header->getValue());
$headers->remove($name);
} elseif ($header instanceof MetadataHeader) {
$metadata[$header->getKey()] = $header->getValue();
$headers->remove($name);
}
}
if ($metadata) {
$headers->addTextHeader('X-Mailgun-Variables', json_encode($metadata));
}
}
}

View File

@@ -0,0 +1,95 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer\Bridge\Mailgun\Transport;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractHttpTransport;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
/**
* @author Kevin Verschaeve
*/
class MailgunHttpTransport extends AbstractHttpTransport
{
use MailgunHeadersTrait;
private const HOST = 'api.%region_dot%mailgun.net';
private string $key;
private string $domain;
private ?string $region;
public function __construct(string $key, string $domain, string $region = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
$this->key = $key;
$this->domain = $domain;
$this->region = $region;
parent::__construct($client, $dispatcher, $logger);
}
public function __toString(): string
{
return sprintf('mailgun+https://%s?domain=%s', $this->getEndpoint(), $this->domain);
}
protected function doSendHttp(SentMessage $message): ResponseInterface
{
$body = new FormDataPart([
'to' => implode(',', $this->stringifyAddresses($message->getEnvelope()->getRecipients())),
'message' => new DataPart($message->toString(), 'message.mime'),
]);
$headers = [];
foreach ($body->getPreparedHeaders()->all() as $header) {
$headers[] = $header->toString();
}
$endpoint = sprintf('%s/v3/%s/messages.mime', $this->getEndpoint(), urlencode($this->domain));
$response = $this->client->request('POST', 'https://'.$endpoint, [
'auth_basic' => 'api:'.$this->key,
'headers' => $headers,
'body' => $body->bodyToIterable(),
]);
try {
$statusCode = $response->getStatusCode();
$result = $response->toArray(false);
} catch (DecodingExceptionInterface) {
throw new HttpTransportException('Unable to send an email: '.$response->getContent(false).sprintf(' (code %d).', $statusCode), $response);
} catch (TransportExceptionInterface $e) {
throw new HttpTransportException('Could not reach the remote Mailgun server.', $response, 0, $e);
}
if (200 !== $statusCode) {
throw new HttpTransportException('Unable to send an email: '.$result['message'].sprintf(' (code %d).', $statusCode), $response);
}
$message->setMessageId($result['id']);
return $response;
}
private function getEndpoint(): ?string
{
$host = $this->host ?: str_replace('%region_dot%', 'us' !== ($this->region ?: 'us') ? $this->region.'.' : '', self::HOST);
return $host.($this->port ? ':'.$this->port : '');
}
}

View File

@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer\Bridge\Mailgun\Transport;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
/**
* @author Kevin Verschaeve
*/
class MailgunSmtpTransport extends EsmtpTransport
{
use MailgunHeadersTrait;
public function __construct(string $username, #[\SensitiveParameter] string $password, string $region = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
parent::__construct('us' !== ($region ?: 'us') ? sprintf('smtp.%s.mailgun.org', $region) : 'smtp.mailgun.org', 465, true, $dispatcher, $logger);
$this->setUsername($username);
$this->setPassword($password);
}
}

View File

@@ -0,0 +1,52 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer\Bridge\Mailgun\Transport;
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\TransportInterface;
/**
* @author Konstantin Myakshin <molodchick@gmail.com>
*/
final class MailgunTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
$user = $this->getUser($dsn);
$password = $this->getPassword($dsn);
$region = $dsn->getOption('region');
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();
if ('mailgun+api' === $scheme) {
return (new MailgunApiTransport($user, $password, $region, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
}
if ('mailgun+https' === $scheme || 'mailgun' === $scheme) {
return (new MailgunHttpTransport($user, $password, $region, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
}
if ('mailgun+smtp' === $scheme || 'mailgun+smtps' === $scheme) {
return new MailgunSmtpTransport($user, $password, $region, $this->dispatcher, $this->logger);
}
throw new UnsupportedSchemeException($dsn, 'mailgun', $this->getSupportedSchemes());
}
protected function getSupportedSchemes(): array
{
return ['mailgun', 'mailgun+api', 'mailgun+https', 'mailgun+smtp', 'mailgun+smtps'];
}
}