Skip to content

Commit

Permalink
replaced SmtpTransport with EsmtpTransport
Browse files Browse the repository at this point in the history
Signed-off-by: sergiu <[email protected]>
  • Loading branch information
SergiuBota1 committed Dec 5, 2024
1 parent 47a49b1 commit 29a544e
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 64 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ return [
'dot_mail' => [
'default' => [
//...
'transport' => Symfony\Component\Mailer\Transport\Smtp\SmtpTransport::class,
'transport' => 'sendmail',
//...
]
]
]
```

### Mail - SMTP
### Mail - ESMTP

If you want your application to send mails on e.g. registration, contact, then edit the file `config/autoload/mail.local.php`. Set the `transport`, `message_options` and `smtp_options` keys like below.

Expand All @@ -55,7 +55,8 @@ Under `smtp_options` key:

- `host` - the mail server's hostname or IP address
- `port` - the mail server's port
- `connection_config` - fill in the `username`, `password` and `ssl` keys with the login details of the email used in `from` above
- `connection_config` - fill in the `username` and `password` keys with the login details of the email used in `from` above
- if you want to disable auto_tls set `tls` key to false

Note: all other keys can be left as is.

Expand All @@ -65,7 +66,7 @@ return [
'dot_mail' => [
'default' => [
//...
'transport' => Symfony\Component\Mailer\Transport\Smtp\SmtpTransport::class,
'transport' => 'esmtp'
'message_options' => [
'from' => '',
//...
Expand All @@ -76,7 +77,7 @@ return [
'connection_config' => [
'username' => '',
'password' => '',
'ssl' => '',
'tls' => null,
]
]
//...
Expand Down
81 changes: 81 additions & 0 deletions config/mail.global.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

return [

/**
* Dotkernel mail module configuration
* Note that many of these options can be set programmatically too, when sending mail messages actually that is
* what you'll usually do, these config provide just default and options that remain the same for all mails
*/
'dot_mail' => [
//the key is the mail service name, this is the default one, which does not extends any configuration
'default' => [
//message configuration
'message_options' => [
//from email address of the email
'from' => '',
//from name to be displayed instead of from address
'from_name' => '',
//reply-to email address of the email
'reply_to' => '',
//replyTo name to be displayed instead of the address
'reply_to_name' => '',
//destination email address as string or a list of email addresses
'to' => [],
//copy destination addresses
'cc' => [],
//hidden copy destination addresses
'bcc' => [],
//email subject
'subject' => '',
//body options - content can be plain text, HTML
'body' => [
'content' => '',
'charset' => 'utf-8',
],
//attachments config
'attachments' => [
'files' => [],
'dir' => [
'iterate' => false,
'path' => 'data/mail/attachments',
'recursive' => false,
],
],
],
/**
* the mail transport to use
* can be any class implementing Symfony\Component\Mailer\Transport\TransportInterface
*
* for standard mail transports, you can use these aliases
* - sendmail => Symfony\Component\Mailer\Transport\SendmailTransport
* - esmtp => Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport
*
* defaults to sendmail
**/
'transport' => 'sendmail',
//options that will be used only if esmtp adapter is used
'smtp_options' => [
//hostname or IP address of the mail server
'host' => '',
//port of the mail server - 587 or 465 for secure connections
'port' => 587,
'connection_config' => [
//the smtp authentication identity
'username' => '',
//the smtp authentication credential
'password' => '',
//to disable auto_tls set tls key to false
//it's not recommended to disable TLS while connecting to an SMTP server over the Internet
'tls' => null,
],
],
],
// option to log the SENT emails
'log' => [
'sent' => getcwd() . '/log/mail/sent.log',
],
],
];
29 changes: 15 additions & 14 deletions config/mail.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

declare(strict_types=1);

use Symfony\Component\Mailer\Transport\SendmailTransport;

return [

/**
Expand All @@ -14,17 +12,6 @@ return [
'dot_mail' => [
//the key is the mail service name, this is the default one, which does not extends any configuration
'default' => [
/**
* the mail transport to use
* can be any class implementing Symfony\Component\Mailer\Transport\TransportInterface
*
* for standard mail transports, you can use these aliases
* - sendmail => Symfony\Component\Mailer\Transport\SendmailTransport
* - smtp => Symfony\Component\Mailer\Transport\Smtp\SmtpTransport
*
* defaults to sendmail
**/
'transport' => SendmailTransport::class,
//message configuration
'message_options' => [
//from email address of the email
Expand Down Expand Up @@ -58,7 +45,18 @@ return [
],
],
],
//options that will be used only if Symfony\Component\Mailer\Transport\Smtp\SmtpTransport adapter is used
/**
* the mail transport to use
* can be any class implementing Symfony\Component\Mailer\Transport\TransportInterface
*
* for standard mail transports, you can use these aliases
* - sendmail => Symfony\Component\Mailer\Transport\SendmailTransport
* - esmtp => Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport
*
* defaults to sendmail
**/
'transport' => 'sendmail',
//options that will be used only if esmtp adapter is used
'smtp_options' => [
//hostname or IP address of the mail server
'host' => '',
Expand All @@ -69,6 +67,9 @@ return [
'username' => '',
//the smtp authentication credential
'password' => '',
//to disable auto_tls set tls key to false
//it's not recommended to disable TLS while connecting to an SMTP server over the Internet
'tls' => null,
],
],
],
Expand Down
4 changes: 2 additions & 2 deletions docs/book/v5/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ $this->mailService->getMessage()->addTo("[email protected]");
## Transport configuration

`dot-mail` uses the `transport` key under the main `dot_mail` configuration key to select the email transport.
It has four email transport classes available by default (`SmtpTransport`), one of which is to be added under the `dot_mail.transport` key for use.
It has two email transport classes available (by default `sendmail`), one of which is to be added under the `dot_mail.transport` key for use.

Sending email with the `Smtp` transport requires valid data for the values under `dot-mail.default.smtp_options`, which is only used in this case.
Sending email with the `Esmtp` transport requires valid data for the values under `dot-mail.default.smtp_options`, which is only used in this case.

> The configured path must be a writable directory
Expand Down
6 changes: 3 additions & 3 deletions docs/book/v5/transports.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

`dot-mail` can use any transport class that implements `Symfony\Component\Mailer\Transport\TransportInterface`, with the standard transport available being:

- `Symfony\Component\Mailer\Transport\Smtp\SmtpTransport,`
- `Symfony\Component\Mailer\Transport\SendmailTransport,`
- `esmtp,`
- `sendmail,`

> Feel free to use any custom transport you desire, provided it implements the mentioned `TransportInterface`.
PHP's `mail()` function is a wrapper over `Sendmail`, and as such has a different behaviour on Windows than on *nix systems. Using sendmail on Windows **will not work in combination with** `addBcc()`.

- Note: emails sent using the sendmail transport will be more often delivered to SPAM.

`Smtp` connects to the configured SMTP host in order to handle sending emails.
`Esmtp` connects to the configured SMTP host in order to handle sending emails.
17 changes: 9 additions & 8 deletions src/Factory/MailServiceAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mailer\Transport\TransportInterface;

use function explode;
Expand Down Expand Up @@ -177,13 +177,14 @@ protected function createTransport(ContainerInterface $container): TransportInte

protected function setupTransportConfig(TransportInterface $transport): TransportInterface
{
if ($transport instanceof SmtpTransport) {
$user = $this->mailOptions->getSmtpOptions()->getConnectionConfig()['username'];
$pass = $this->mailOptions->getSmtpOptions()->getConnectionConfig()['password'];
$port = $this->mailOptions->getSmtpOptions()->getPort();
$host = $this->mailOptions->getSmtpOptions()->getHost();

$transport = Transport::fromDsn('smtp://' . $user . ':' . $pass . '@' . $host . ':' . $port);
if ($transport instanceof EsmtpTransport) {
$user = $this->mailOptions->getSmtpOptions()->getConnectionConfig()['username'];
$pass = $this->mailOptions->getSmtpOptions()->getConnectionConfig()['password'];
$tls = $this->mailOptions->getSmtpOptions()->getConnectionConfig()['tls'] === false ? 'false' : null;
$port = $this->mailOptions->getSmtpOptions()->getPort();
$host = $this->mailOptions->getSmtpOptions()->getHost();
$transport = Transport::fromDsn('smtp://' . $user . ':' . $pass . '@' . $host . ':' . $port
. '?auto_tls=' . $tls);
}

return $transport;
Expand Down
6 changes: 3 additions & 3 deletions src/Options/MailOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Laminas\Stdlib\AbstractOptions;
use Symfony\Component\Mailer\Transport\SendmailTransport;
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mailer\Transport\TransportInterface;

use function array_key_exists;
Expand All @@ -21,9 +21,9 @@
class MailOptions extends AbstractOptions
{
protected array $eventListeners = [];
protected TransportInterface|string $transport = SmtpTransport::class;
protected TransportInterface|string $transport = EsmtpTransport::class;
protected array $transportMap = [
'smtp' => [SmtpTransport::class],
'esmtp' => [EsmtpTransport::class],
'sendmail' => [SendmailTransport::class],
];
protected MessageOptions $messageOptions;
Expand Down
27 changes: 13 additions & 14 deletions test/CommonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;

trait CommonTrait
{
Expand Down Expand Up @@ -46,17 +45,6 @@ private function generateConfig(): array
return [
'dot_mail' => [
'default' => [
/**
* the mail transport to use
* can be any class implementing Symfony\Component\Mailer\Transport\TransportInterface
*
* for standard mail transports, you can use these aliases
* - sendmail => Symfony\Component\Mailer\Transport\SendmailTransport
* - smtp => Symfony\Component\Mailer\Transport\Smtp\SmtpTransport
*
* defaults to sendmail
**/
'transport' => SmtpTransport::class,
'message_options' => [
'from' => '',
'from_name' => '',
Expand All @@ -79,7 +67,18 @@ private function generateConfig(): array
],
],
],
//options that will be used only if Symfony\Component\Mailer\Transport\Smtp\SmtpTransport
/**
* the mail transport to use
* can be any class implementing Symfony\Component\Mailer\Transport\TransportInterface
*
* for standard mail transports, you can use these aliases
* - sendmail => Symfony\Component\Mailer\Transport\SendmailTransport
* - esmtp => Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport
*
* defaults to sendmail
**/
'transport' => 'esmtp',
//options that will be used only if Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport
// adapter is used
'smtp_options' => [
'host' => 'testHost',
Expand All @@ -89,11 +88,11 @@ private function generateConfig(): array
'username' => 'test',
//the smtp authentication credential
'password' => 'testPassword',
'tsl' => null,
],
],
],
'test' => 'string test',

// option to log the SENT emails
'log' => [
'sent' => $this->fileSystem->url() . '/log/mail/sent.log',
Expand Down
Loading

0 comments on commit 29a544e

Please sign in to comment.