Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed unused code inherited from version 4.0 #73

Merged
merged 7 commits into from
Dec 6, 2024
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
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
77 changes: 22 additions & 55 deletions config/mail.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,111 +3,78 @@
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
* Note that many of these options can be set programmatically too, when sending mail messages actually that is
* what you'll usually do, these configs provide just defaults 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
//the key is the mail service name, this is the default one, which does not extend any configuration
'default' => [
//tells which other mail service configuration to extend
'extends' => null,

/**
* 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' => Symfony\Component\Mailer\Transport\SendmailTransport::class,

//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,
]
],
],
],

//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
alexmerlin marked this conversation as resolved.
Show resolved Hide resolved
*
* 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 class used for authentication
//the value can be one of smtp, plain, login or crammd5
'connection_class' => 'login',

'port' => 587,
'connection_config' => [

//the smtp authentication identity
'username' => '',

//the smtp authentication credential
'password' => '',

//the encryption type to be used, ssl or tls
//null should be used to disable SSL
'ssl' => 'tls',
]
//to disable auto_tls set tls key to false
//it's not recommended to disable TLS while connecting to an SMTP server
'tls' => null,
],
],
],
// option to log the SENT emails
'log' => [
'sent' => getcwd() . '/log/mail/sent.log'
'sent' => getcwd() . '/log/mail/sent.log',
],

/**
* You can define other mail services here, with the same structure as the default block
* you can even extend from the default block, and overwrite only the differences
*/
],
];
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
8 changes: 4 additions & 4 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()`.
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.
23 changes: 0 additions & 23 deletions src/Factory/MailOptionsAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
namespace Dot\Mail\Factory;

use Dot\Mail\Options\MailOptions;
use Laminas\Stdlib\ArrayUtils;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

use function array_key_exists;
use function explode;
use function is_array;
use function is_string;
use function trim;

class MailOptionsAbstractFactory extends AbstractMailFactory
{
Expand All @@ -35,25 +31,6 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $
$specificConfig = [];
}

/**
* Merge any extended mail service config into this one
*/
do {
$extendsConfigKey = isset($specificConfig['extends']) && is_string($specificConfig['extends'])
? trim($specificConfig['extends'])
: null;

unset($specificConfig['extends']);

if (
$extendsConfigKey !== null
&& array_key_exists($extendsConfigKey, $config)
&& is_array($config[$extendsConfigKey])
) {
$specificConfig = ArrayUtils::merge($config[$extendsConfigKey], $specificConfig);
}
} while ($extendsConfigKey !== null);

return new MailOptions($specificConfig);
}
}
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
17 changes: 3 additions & 14 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,10 +21,9 @@
class MailOptions extends AbstractOptions
{
protected array $eventListeners = [];
protected array $saveSentMessageFolder = [];
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 Expand Up @@ -89,14 +88,4 @@ public function setEventListeners(array $eventListeners): void
{
$this->eventListeners = $eventListeners;
}

public function getSaveSentMessageFolder(): array
{
return $this->saveSentMessageFolder;
}

public function setSaveSentMessageFolder(array $saveSentMessageFolder): void
{
$this->saveSentMessageFolder = $saveSentMessageFolder;
}
}
18 changes: 0 additions & 18 deletions src/Options/SmtpOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
class SmtpOptions extends AbstractOptions
{
protected string $name = 'localhost';
protected string $connectionClass = 'smtp';
protected array $connectionConfig = [];
protected string $host = '127.0.0.1';
protected int $port = 25;
Expand All @@ -36,23 +35,6 @@ public function setName(string $name): static
return $this;
}

/**
* Get connection class
*/
public function getConnectionClass(): string
{
return $this->connectionClass;
}

/**
* Set connection class
*/
public function setConnectionClass(string $connectionClass): static
{
$this->connectionClass = $connectionClass;
return $this;
}

/**
* Get connection configuration array
*/
Expand Down
Loading
Loading