Skip to content

Commit

Permalink
➕ Add SMTP connections test
Browse files Browse the repository at this point in the history
  • Loading branch information
bdsoha committed Jan 16, 2025
1 parent 088c36d commit dc21d84
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM dunglas/frankenphp:${frankenphp_version}-php${php_version} AS base
WORKDIR /laravel
SHELL ["/bin/bash", "-eou", "pipefail", "-c"]

ENV SERVER_NAME=:80
ENV SERVER_NAME=:8000
ENV OCTANE_SERVER=frankenphp
ARG user=laravel

Expand Down
34 changes: 34 additions & 0 deletions src/common/test_smtp_connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

require 'vendor/autoload.php';

use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;

$app = require_once 'bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

try {
$dsn = sprintf(
'smtp://%s:%s@%s:%d',
config('mail.mailers.smtp.username'),
config('mail.mailers.smtp.password'),
config('mail.mailers.smtp.host'),
config('mail.mailers.smtp.port')
);

if (config('mail.mailers.smtp.encryption') === 'tls') {
$dsn .= '?encryption=tls';
}

$transport = Transport::fromDsn($dsn);
$mailer = new Mailer($transport);

$transport->start();

exit(0);
} catch (Exception $e) {
echo 'SMTP connection error: ' . $e->getMessage();
exit(1);
}
16 changes: 14 additions & 2 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#!/bin/bash

set -e
set -euo pipefail
trap 'echo "Error on line $LINENO"' ERR

: "${CONTAINER_MODE:=app}"
: "${CONTAINER_PORT:=8000}"
: "${CONTAINER_MANUAL_SETUP:=false}"
: "${CONTAINER_MODE:=app}"
: "${CONTAINER_WORKER_DELAY:=10}"
: "${CONTAINER_WORKER_SLEEP:=5}"
: "${CONTAINER_WORKER_TIMEOUT:=300}"
: "${CONTAINER_WORKER_TRIES:=3}"

: "${TEST_DB_CONNECTION:=true}"
: "${TEST_CACHE_CONNECTION:=true}"
: "${TEST_SMTP_CONNECTION:=false}"
: "${TEST_CONNECTION_TIMEOUT:=20}"

: "${APP_ENV:=production}"
Expand All @@ -21,6 +24,9 @@ ARTISAN="php -d variables_order=EGPCS /laravel/artisan"
_test_connection() {
local count=0
local type="${1}"
local status

echo "🆗 Testing ${type} connection..."

while [ "$count" -lt "$TEST_CONNECTION_TIMEOUT" ]; do
php -f "/common/test_${type}_connection.php" > /dev/null 2>&1
Expand Down Expand Up @@ -52,6 +58,12 @@ _test_connections() {
else
_test_connection "cache"
fi

if [ "$TEST_SMTP_CONNECTION" != "true" ]; then
echo "⏭ Skipping SMTP connection test..."
else
_test_connection "smtp"
fi
}

_migrate() {
Expand Down

0 comments on commit dc21d84

Please sign in to comment.