diff --git a/.travis.yml b/.travis.yml index c1bfdd6..5c7108f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 @@ -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 diff --git a/composer.json b/composer.json index 91ab373..56c803f 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,6 @@ "source": "https://github.com/pear/Mail" }, "require": { - "pear/net_smtp": "*" + "pear/net_smtp": "^1.7" } } \ No newline at end of file diff --git a/config/schema/netsmtp.schema.yml b/config/schema/netsmtp.schema.yml index 9a4a328..f6902cc 100755 --- a/config/schema/netsmtp.schema.yml +++ b/config/schema/netsmtp.schema.yml @@ -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.' + diff --git a/netsmtp.info.yml b/netsmtp.info.yml index 9ac9e16..b870ec4 100755 --- a/netsmtp.info.yml +++ b/netsmtp.info.yml @@ -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: diff --git a/netsmtp.module b/netsmtp.module index 79aba2b..4a08eea 100755 --- a/netsmtp.module +++ b/netsmtp.module @@ -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; } } diff --git a/src/Plugin/Mail/NetSmtpMail.php b/src/Plugin/Mail/NetSmtpMail.php index 77976a6..a8515f5 100755 --- a/src/Plugin/Mail/NetSmtpMail.php +++ b/src/Plugin/Mail/NetSmtpMail.php @@ -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. */ @@ -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'); } /** @@ -82,7 +86,6 @@ protected function catchAddressesInto($string) { $ret = []; if (empty($string)) { - // Please bitch... Not my problem. return NULL; } @@ -154,15 +157,14 @@ 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; @@ -170,7 +172,7 @@ protected function getInstance($module, $key) { } 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; } @@ -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, ]; @@ -213,7 +215,6 @@ protected function getInstance($module, $key) { return NULL; } } - // Finally! We did it I guess. return $smtp; } @@ -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; } diff --git a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php index 6e37316..a812f2b 100755 --- a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php +++ b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php @@ -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. @@ -28,13 +30,13 @@ class CanSendEmailToMailtrapSmtp { * * @var string */ - private $smtpToken; + private $apiToken; /** * Inbox ID. * * @var string */ - private $smtpInboxId; + private $apiInboxId; /** * Mail manager. * @@ -44,16 +46,50 @@ 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'); } /** @@ -61,38 +97,46 @@ public function __construct(DrupalKernel $kernel) { */ 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())); } } diff --git a/src/Tests/Integration/runtests.php b/src/Tests/Integration/runtests.php index 840b6f9..c5aa0c5 100644 --- a/src/Tests/Integration/runtests.php +++ b/src/Tests/Integration/runtests.php @@ -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();