Skip to content
Open
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
20 changes: 14 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ mysql:
encoding: utf8

before_install:
- export MAILSYSTEM_VERSION="8.x-4.0"
- test ${DRUPAL_VERSION} == "8" || export MAILSYSTEM_VERSION="8.x-4.x-dev"
- git config --global github.accesstoken $GITHUB_OAUTH_TOKEN
- composer self-update

Expand All @@ -38,17 +40,23 @@ install:
- composer global require --no-interaction "hirak/prestissimo:^0.3"
- phpenv rehash

- mysql -e 'create database drupal;'

- cd ..
- drush dl drupal-$DRUPAL_VERSION --drupal-project-rename=drupal
- mv drupal-netsmtp drupal/modules
- cd drupal
- export DRUPAL_DIR=$(pwd)

# Install drupal default profile
- drush --verbose site-install --db-url=mysql://root:@127.0.0.1/drupal --yes
# create new site, stubbing sendmail path with true to prevent delivery errors and manually resolving drush path
- mysql -e 'create database drupal;'
- php -d sendmail_path=`which true` ~/.composer/vendor/bin/drush.php --yes core-quick-drupal --profile=testing --no-server --verbose --db-url=mysql://root:@127.0.0.1/drupal

- drush dl mailsystem-$MAILSYSTEM_VERSION -y
- drush en -y netsmtp

- drush dl composer_manager
- php modules/composer_manager/scripts/init.php
- composer drupal-update

- drush cr

script:
- cd modules/drupal-netsmtp/src/Tests/Integration && php -f runtests.php
- cd modules/drupal-netsmtp/src/Tests/Integration && drush scr runtests.php
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"source": "https://github.com/pear/Mail"
},
"require": {
"pear/net_smtp": "*"
"pear/net_smtp": "^1.7"
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the empty line at the end of file.

37 changes: 34 additions & 3 deletions config/schema/netsmtp.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,38 @@ netsmtp.settings:

netsmtp_catch:
type: sequence
label: 'Re-route emails'
label: 'Re-route emails'
sequence:
type: email
label: 'Re-route email'
providers:
type: sequence
label: List of modules
sequence:
type: sequence
label: List of keys for a given module
sequence:
type: email
label: 'Re-route email'
type: mapping
label: Per module/key setting.
mapping:
hostname:
type: string
label: 'Hostname'
port:
type: string
label: 'port'
use_ssl:
type: string
label: 'ssl or tls'
description: 'true/false for "ssl" & tls to use "tls".'
username:
type: string
label: 'Username'
password:
type: string
label: 'Password'
localhost:
type: string
label: 'Localhost'
description: 'The value to give when sending EHLO or HELO.'

4 changes: 2 additions & 2 deletions netsmtp.info.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Net SMTP
name: "Net SMTP"
type: module
description: 'SMTP connector using Net_SMTP PEAR library.'
package: Mail
package: "Mail"
core: 8.x
configure: netsmtp.settings
dependencies:
Expand Down
4 changes: 2 additions & 2 deletions netsmtp.module
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use Drupal\Core\Url;
function netsmtp_mail($key, &$message) {
switch ($key) {
case 'test_message':
$uuid = \Drupal\Component\Uuid\Php::generate();
$uuid = @\Drupal\Component\Uuid\Php::generate();
$message['subject'] = 'uniq-mailtrap-id:' . $uuid;
\Drupal::state()->set('netsmtp.last_message_id', $message['subject']);
$message['body'][] = t('Testing Net Smtp mailer.');
$message['body'][] = t('Testing NetSMTP mailer');
break;
}
}
Expand Down
33 changes: 18 additions & 15 deletions src/Plugin/Mail/NetSmtpMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class NetSmtpMail implements MailInterface {
* Default provider key.
*/
const PROVIDER_DEFAULT = 'default';

/**
* Provider config key.
*/
const PROVIDER_CONFIG_KEY = 'providers';

/**
* Default SSL port.
*/
Expand All @@ -53,17 +59,15 @@ class NetSmtpMail implements MailInterface {
/**
* Configuration handler.
*
* @var \Drupal\Core\Config\ImmutableConfig
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
private $config;
protected $config;

/**
* NetSmtpMail constructor.
*/
public function __construct() {
// Fuck hate PEAR.
$this->PEAR = new PEAR();
$this->config = \Drupal::config('netsmtp.settings');
$this->config = \Drupal::configFactory()->get('netsmtp.settings');
}

/**
Expand All @@ -82,7 +86,6 @@ protected function catchAddressesInto($string) {
$ret = [];

if (empty($string)) {
// Please bitch... Not my problem.
return NULL;
}

Expand Down Expand Up @@ -154,23 +157,22 @@ protected function setError($e, $type = 'error') {
protected function getInstance($module, $key) {
$is_tls = FALSE;

// SMTP server configurations per module, key.
$server_id_list = [
$module . '.' . $key,
$module,
$key,
self::PROVIDER_DEFAULT,
self::PROVIDER_CONFIG_KEY . '.' . $module . '.' . $key,
self::PROVIDER_CONFIG_KEY . '.' . $module,
self::PROVIDER_CONFIG_KEY . '.' . $key,
self::PROVIDER_CONFIG_KEY . '.' . self::PROVIDER_DEFAULT
];

foreach ($server_id_list as $provider) {
foreach($server_id_list as $provider) {
$provider_config = $this->config->get($provider);
if (!is_null($provider_config)) {
break;
}
}

if (empty($provider_config) && isset($provider)) {
$this->setError(sprintf("Provider '%s' does not exists, fallback on default", $provider), 'warning');
$this->setError(sprintf("Provider '%s' does not exists", $provider), 'warning');
return NULL;
}

Expand All @@ -181,9 +183,9 @@ protected function getInstance($module, $key) {

$info = array_filter($provider_config) + [
'port' => NULL,
'username' => NULL,
'use_ssl' => FALSE,
'password' => '',
'username' => NULL,
'localhost' => NULL,
];

Expand Down Expand Up @@ -213,7 +215,6 @@ protected function getInstance($module, $key) {
return NULL;
}
}
// Finally! We did it I guess.
return $smtp;
}

Expand All @@ -229,6 +230,8 @@ public function format(array $message) {
* {@inheritdoc}
*/
public function mail(array $message) {
$this->PEAR = new PEAR();

if (!$smtp = $this->getInstance($message['module'], $message['key'])) {
return FALSE;
}
Expand Down
80 changes: 62 additions & 18 deletions src/Tests/Integration/CanSendEmailToMailtrapSmtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use GuzzleHttp\Client;

define('NETSMTP_MAILTRAP_API_ENDPOINT', 'https://mailtrap.io/api/v1');
define('NETSMTP_MAILTRAP_SMTP_HOSTNAME', 'mailtrap.io');
define('NETSMTP_MAILTRAP_SMTP_PORT', 465);

/**
* Class CanSendEmailToMailtrapSmtp.
Expand All @@ -28,13 +30,13 @@ class CanSendEmailToMailtrapSmtp {
*
* @var string
*/
private $smtpToken;
private $apiToken;
/**
* Inbox ID.
*
* @var string
*/
private $smtpInboxId;
private $apiInboxId;
/**
* Mail manager.
*
Expand All @@ -44,55 +46,97 @@ class CanSendEmailToMailtrapSmtp {

/**
* CanSendEmailToMailtrapSmtp constructor.
*
* @param DrupalKernel $kernel
* Drupal Kernel.
*/
public function __construct(DrupalKernel $kernel) {
public function __construct() {
// Prepare the initial properties.
$this->kernel = $kernel;
$this->mailManager = $this->kernel->getContainer()->get('plugin.manager.mail');
$this->smtpInboxId = getenv('MAILTRAP_INBOX_ID');
$this->smtpToken = getenv('MAILTRAP_TOKEN');
$this->apiInboxId = getenv('MAILTRAP_INBOX_ID');
$this->apiToken = getenv('MAILTRAP_TOKEN');
$this->smtpUserName = getenv('MAILTRAP_SMTP_USERNAME');
$this->smtpPassword = getenv('MAILTRAP_SMTP_PASSWORD');

// We need this before initMailManager(),
// to properly construct a netsmtp_mail plugin.
$this->createSmtpConfig(
$this->smtpUserName,
$this->smtpPassword
);
$this->initMailManager();
}

/**
* Construct mailtrap smtp config object.
*
* This object later will be used by NetSmtpMail::__construct().
*/
private function createSmtpConfig() {
\Drupal::configFactory()
->getEditable('mailsystem.settings')
->set('defaults.sender', 'netsmtp_mail')
->set('defaults.formatter','php_mail')
->save();

\Drupal::configFactory()
->getEditable('netsmtp.settings')
->set('providers.netsmtp.test_message.hostname', NETSMTP_MAILTRAP_SMTP_HOSTNAME)
->set('providers.netsmtp.test_message.port', NETSMTP_MAILTRAP_SMTP_PORT)
->set('providers.netsmtp.test_message.use_ssl', FALSE)
->set('providers.netsmtp.test_message.username', $this->smtpUserName)
->set('providers.netsmtp.test_message.password', $this->smtpPassword)
->save();
}

/**
* Get current mail manager.
*/
private function initMailManager() {
$this->mailManager = \Drupal::getContainer()->get('plugin.manager.mail');
}

/**
* Test email sending.
*/
public function testSendEmail() {
try {
$result = $this->mailManager->mail('netsmtp', 'test_message');
$message_key = \Drupal::state()->get('netsmtp.last_message_id');
$this->mailManager->mail('netsmtp', 'test_message', 'netsmtp@example.com', []);
}
catch (\Exception $e) {
throw new \RuntimeException(sprintf('Can\'t send an email. Details: %s', $e->getMessage()));
catch (\RuntimeException $e) {
file_put_contents('php://stderr', $e->getTraceAsString());
file_put_contents('php://stderr', sprintf('Can\'t send an email. Details: %s', $e->getMessage()));
exit(1);
}

$message_key = \Drupal::state()->get('netsmtp.last_message_id');

$inbox_url = implode('/', [
NETSMTP_MAILTRAP_API_ENDPOINT,
'inboxes',
$this->smtpInboxId,
$this->apiInboxId,
]);

$client = new Client([
'base_uri' => $inbox_url . '/',
'headers' => ['Api-Token' => $this->apiToken],
]);

$response = $client->request('GET', 'messages', [
'query' => ['search' => $message_key],
'headers' => ['Api-Token' => $this->smtpToken],
]);

$data = \GuzzleHttp\json_decode($response->getBody()->getContents());
$mail = reset($data);

if (404 == $response->getStatusCode()) {
throw new \ErrorException(sprintf('Can\'t find a email with email subject: %s', $message_key));
echo sprintf('Can\'t find a email with email subject: %s', $message_key);
exit(1);
}

if ($mail->subject != $message_key) {
throw new \ErrorException(sprintf('There is no email with email subject: %s', $message_key));
sprintf('There is no email with email subject: %s', $message_key);
exit(1);
}
var_dump('mail_id: ' . $mail->id);
$response = $client->request('GET', "messages/$mail->id/body.raw");
var_dump($data = ($response->getBody()->getContents()));
}

}
23 changes: 1 addition & 22 deletions src/Tests/Integration/runtests.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,5 @@
* Test runner.
*/

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

$drupal_dir = getenv('DRUPAL_DIR');
var_dump($drupal_dir);

static $kernel;

if (!isset($kernel) || !($kernel instanceof DrupalKernel)) {
require_once $drupal_dir . '/core/includes/database.inc';
require_once $drupal_dir . '/core/includes/schema.inc';

$autoloader = require_once $drupal_dir . '/autoload.php';
$request = Request::createFromGlobals();

$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();
$kernel->prepareLegacyRequest($request);
}

require_once './CanSendEmailToMailtrapSmtp.php';
$mailer = new \Drupal\netsmtp\Tests\Integration\CanSendEmailToMailtrapSmtp($kernel);
$mailer = new \Drupal\netsmtp\Tests\Integration\CanSendEmailToMailtrapSmtp();
$mailer->testSendEmail();