Allow SMTP servers with self-signed certificates

Created on 23 July 2019, almost 5 years ago
Updated 8 July 2023, 12 months ago

Hi,

For a few times I've had to deal with clients that have badly configured SMTP servers, and I don't have any power over it, so I have to use the server/accounts that they provide to me.

The most of the times, the main problem is servers that have self-signed certificates, and this module doesn't have any option to allow it to make connections to those kinds of servers.

I took a look at the module, and I found that it uses fsockopen() ( https://www.php.net/manual/en/function.fsockopen.php ) to create the socket to the SMTP server ( https://git.drupalcode.org/project/smtp/blob/8.x-1.0-beta4/src/PHPMailer... ). So I changed it to use stream_socket_client() ( https://www.php.net/manual/en/function.stream-socket-client.php ) that has the same arguments as the fsockopen(), but has an extra argument that is the $context.

Creating a $context like this, allows us to connect to servers with self signed certificates.

$options = [
    'ssl' => [
        'verify_peer_name' => false,
        'verify_peer'      => false,
        'allow_self_signed' => true,
    ]
];

$context = stream_context_create($options);

So what I've changed, is:

* Added a new checkbox to the module configuration form, called smtp_self_signed. With a warning telling people to enable it only if they know what they are doing;
* Added the same variable to smtp.settings.yml and smtp.schema.yml;
* Added the glue needed to pass the new option from the form to the PHPMailer.php and SMTP.php files;
* Using stream_socket_client() instead of fsockopen() to create the connection;
* When smtp_self_signed is set to True, create a context that allows connections to servers with self-signed certificates;

The attached patch is based on the latest branch "8.x-1.x" on git. This is my first time submitting a patch to add a new feature to a Drupal module, so be gentle ;-)

Regards,
Pedro de Oliveira @ Javali

✨ Feature request
Status

Needs work

Version

1.0

Component

Code

Created by

πŸ‡΅πŸ‡ΉPortugal falsovsky

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024