Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ jobs:
with:
folder: php
project: ${{ github.event.repository.name }}
secrets: inherit
secrets:
DOC_TOKEN: ${{ secrets.DOC_TOKEN }}

14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ scheme://username:password/smtpserver:port

The options are:

| Part | Description |
|:-----------|:--------------------|
| Part | Description |
|:-----------|:--------------------------------------------------------------------------------------------------------|
| scheme | The email scheme: smtp, ssl, tls, mandrill and ses. Note that mandrill and ses use your own private api |
| username | The username |
| password | The password |
| smtpserver | The SMTP Host |
| port | The SMTP Port |
| username | The username |
| password | The password |
| smtpserver | The SMTP Host |
| port | The SMTP Port |

The protocols available are:

Expand Down Expand Up @@ -205,7 +205,7 @@ class MyWrapper extends \ByJG\Mail\Wrapper\BaseWrapper
return ['mywrapper'];
}

public function send(Envelope $envelope)
public function send(Envelope $envelope): \ByJG\Mail\SendResult
{
// Do how to send the email using your library
}
Expand Down
11 changes: 11 additions & 0 deletions src/SendResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace ByJG\Mail;

class SendResult
{
public function __construct(
public readonly bool $success,
public readonly ?string $id = null,
) {}
}
11 changes: 6 additions & 5 deletions src/Wrapper/AmazonSesWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ByJG\Mail\Envelope;
use ByJG\Mail\Exception\InvalidEMailException;
use ByJG\Mail\Exception\InvalidMessageFormatException;
use ByJG\Mail\SendResult;
use PHPMailer\PHPMailer\Exception;

class AmazonSesWrapper extends PHPMailerWrapper
Expand Down Expand Up @@ -37,12 +38,12 @@ public function getSesClient(): mixed
* ses://accessid:aswsecret@region
*
* @param Envelope $envelope
* @return bool
* @return SendResult
* @throws Exception
* @throws InvalidEMailException
* @throws InvalidMessageFormatException
* @throws Exception
*/
public function send(Envelope $envelope): bool
public function send(Envelope $envelope): SendResult
{
$this->validate($envelope);

Expand All @@ -53,14 +54,14 @@ public function send(Envelope $envelope): bool

$ses = $this->getSesClient();

$ses->sendRawEmail(
$result = $ses->sendRawEmail(
[
'RawMessage' => [
'Data' => $message,
]
]
);

return true;
return new SendResult(true, $result->get('MessageId'));
}
}
3 changes: 2 additions & 1 deletion src/Wrapper/MailWrapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace ByJG\Mail\Wrapper;

use ByJG\Mail\Envelope;
use ByJG\Mail\SendResult;

interface MailWrapperInterface
{
public static function schema(): array;

public function send(Envelope $envelope): bool;
public function send(Envelope $envelope): SendResult;
}
13 changes: 8 additions & 5 deletions src/Wrapper/MailgunApiWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ByJG\Mail\Envelope;
use ByJG\Mail\Exception\InvalidEMailException;
use ByJG\Mail\Exception\MailApiException;
use ByJG\Mail\SendResult;
use ByJG\WebRequest\Exception\MessageException;
use ByJG\WebRequest\Exception\NetworkException;
use ByJG\WebRequest\Exception\RequestException;
Expand Down Expand Up @@ -62,15 +63,15 @@ public function getRequestObject(): RequestInterface
* malgun://api:APIKEY@DOMAINNAME
*
* @param Envelope $envelope
* @return bool
* @return SendResult
* @throws ClientExceptionInterface
* @throws InvalidEMailException
* @throws MailApiException
* @throws MessageException
* @throws RequestException
* @throws NetworkException
* @throws ClientExceptionInterface
* @throws RequestException
*/
public function send(Envelope $envelope): bool
public function send(Envelope $envelope): SendResult
{
$this->validate($envelope);

Expand Down Expand Up @@ -118,7 +119,9 @@ public function send(Envelope $envelope): bool
throw new MailApiException('Mailgun: ' . $resultJson['message']);
}

return true;
$messageId = $resultJson['id'];
Comment thread
byjg marked this conversation as resolved.

return new SendResult(true, $messageId);
}

private function getApiUri()
Expand Down
11 changes: 6 additions & 5 deletions src/Wrapper/PHPMailerWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ByJG\Mail\Exception\InvalidEMailException;
use ByJG\Mail\Exception\MailApiException;
use ByJG\Mail\Override\PHPMailerOverride;
use ByJG\Mail\SendResult;
use ByJG\Mail\Util;
use InvalidArgumentException;
use PHPMailer\PHPMailer\Exception;
Expand Down Expand Up @@ -100,12 +101,12 @@ protected function prepareMailer(Envelope $envelope): PHPMailerOverride

/**
* @param Envelope $envelope
* @return bool
* @throws MailApiException
* @throws InvalidEMailException
* @return SendResult
* @throws Exception
* @throws InvalidEMailException
* @throws MailApiException
*/
public function send(Envelope $envelope): bool
public function send(Envelope $envelope): SendResult
{
$this->validate($envelope);

Expand All @@ -124,6 +125,6 @@ public function send(Envelope $envelope): bool
throw new MailApiException($mail->ErrorInfo);
}

return true;
return new SendResult(true, $mail->getLastMessageID());
Comment thread
icarofgomes marked this conversation as resolved.
}
}
9 changes: 5 additions & 4 deletions src/Wrapper/SendMailWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ByJG\Mail\Envelope;
use ByJG\Mail\Exception\InvalidEMailException;
use ByJG\Mail\Exception\InvalidMessageFormatException;
use ByJG\Mail\SendResult;
use PHPMailer\PHPMailer\Exception;

/**
Expand All @@ -24,12 +25,12 @@ public static function schema(): array

/**
* @param Envelope $envelope
* @return bool
* @return SendResult
* @throws Exception
* @throws InvalidEMailException
* @throws InvalidMessageFormatException
* @throws Exception
*/
public function send(Envelope $envelope): bool
public function send(Envelope $envelope): SendResult
{
$this->validate($envelope);

Expand All @@ -47,6 +48,6 @@ public function send(Envelope $envelope): bool
mail($toEmail, $envelope->getSubject(), $messageParts['body'], $messageParts['header']);
}

return true;
return new SendResult(true, $mail->getLastMessageID());
}
}
66 changes: 56 additions & 10 deletions tests/AmazonSesWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@
namespace Tests;

use Aws\Credentials\Credentials;
use ByJG\Mail\Exception\InvalidEMailException;
use ByJG\Mail\Exception\InvalidMessageFormatException;
use ByJG\Mail\SendResult;
use ByJG\Mail\Wrapper\AmazonSesWrapper;
use ByJG\Util\Uri;
use PHPMailer\PHPMailer\Exception;

class AmazonSesWrapperTest extends BaseWrapperTest
{
/**
* @param $envelope
* @return MockSender
* @return array
* @throws InvalidEMailException
* @throws InvalidMessageFormatException
* @throws Exception
*/
public function doMockedRequest($envelope)
public function doMockedRequest($envelope): array
{
$object = $this->getMockBuilder(AmazonSesWrapper::class)
->onlyMethods(['getSesClient'])
Expand All @@ -24,9 +31,9 @@ public function doMockedRequest($envelope)
->method('getSesClient')
->will($this->returnValue($mock));

$object->send($envelope);
$result = $object->send($envelope);

return $mock;
return [$mock, $result];
}

public function testGetSesClient()
Expand All @@ -46,9 +53,14 @@ public function testGetSesClient()
$this->assertEquals('2010-12-01', $sesClient->getApi()->getApiVersion());
}

protected function send($envelope, $rawEmail)
/**
* @throws Exception
* @throws InvalidMessageFormatException
* @throws InvalidEMailException
*/
protected function send($envelope, $rawEmail): SendResult
{
$mock = $this->doMockedRequest($envelope);
[$mock, $result] = $this->doMockedRequest($envelope);
$mimeMessage = $this->fixVariableFields(file_get_contents(__DIR__ . '/resources/' . $rawEmail . '.eml'));
$mock->result['RawMessage']['Data'] = $this->fixVariableFields($mock->result['RawMessage']['Data']);

Expand All @@ -59,29 +71,63 @@ protected function send($envelope, $rawEmail)
];

$this->assertEquals($expected, $mock->result);

return $result;
}

/**
* @throws Exception
* @throws InvalidMessageFormatException
* @throws InvalidEMailException
*/
public function testBasicEnvelope()
{
$envelope = $this->getBasicEnvelope();
$this->send($envelope, 'basicenvelope');
$result = $this->send($envelope, 'basicenvelope');

$this->assertTrue($result->success);
$this->assertEquals('EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000', $result->id);
}

/**
* @throws Exception
* @throws InvalidMessageFormatException
* @throws InvalidEMailException
*/
public function testFullEnvelope()
{
$envelope = $this->getFullEnvelope();
$this->send($envelope, 'fullenvelope');
$result = $this->send($envelope, 'fullenvelope');

$this->assertTrue($result->success);
$this->assertEquals('EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000', $result->id);
}

/**
* @throws Exception
* @throws InvalidMessageFormatException
* @throws InvalidEMailException
*/
public function testAttachmentEnvelope()
{
$envelope = $this->getAttachmentEnvelope();
$this->send($envelope, 'attachmentenvelope');
$result = $this->send($envelope, 'attachmentenvelope');

$this->assertTrue($result->success);
$this->assertEquals('EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000', $result->id);
}

/**
* @throws Exception
* @throws InvalidMessageFormatException
* @throws InvalidEMailException
*/
public function testEmbedImageEnvelope()
{
$envelope = $this->getEmbedImageEnvelope();
$this->send($envelope, 'embedenvelope');
$result = $this->send($envelope, 'embedenvelope');

$this->assertTrue($result->success);
$this->assertEquals('EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000', $result->id);
}
}
Loading