Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MonsieurRado's avatar

Sending mail every hour issue

Hi, I can't manage to send emails every hour with laravel.

Here is my code:

file \app\console\commands\RappelDepot.php


namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use App\Mail\RappelDepotFichiers;

class RappelDepot extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rappel:dixjours';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Envoie un mail de rappel 10 jours après la fin d une formation';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Mail::to('[email protected]')->send(new RappelDepotFichiers());
    }
}

kernel.php :

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\RappelDepot::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('rappel:dixjours')
        ->hourly();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

when i run this command : php artisan rappel:dixjours , i get this error:

[email protected] (php/7.3/production/stable) ~/espaceformation $ php artisan rappel:dixjours

   Swift_TransportException

  Connection could not be established with host smtp.mailtrap.io :stream_socket_client(): unable to connect to tcp://smtp.mailtrap.io:2525 (Connection refused)

  at vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:261
    257▕         }
    258▕         $streamContext = stream_context_create($options);
    259▕
    260▕         set_error_handler(function ($type, $msg) {
  ➜ 261▕             throw new Swift_TransportException('Connection could not be established with host '.$this->params['host'].' :'.$msg);
    262▕         });
    263▕         try {
    264▕             $this->stream = stream_socket_client($host.':'.$this->params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
    265▕         } finally {

  1   [internal]:0
      Swift_Transport_StreamBuffer::{closure}("stream_socket_client(): unable to connect to tcp://smtp.mailtrap.io:2525 (Connection refused)", "/home/bxxxxxxxxxh/exxxxxxxxxxxxn/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php")

      +12 vendor frames
  14  app/Console/Commands/RappelDepot.php:42
      Illuminate\Mail\PendingMail::send(Object(App\Mail\RappelDepotFichiers))

.env file : (every other mail i send in controllers are working fine)

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=xxxxxxxxxxxxxx38
MAIL_PASSWORD=fxxxxxxxxxxxxxx2
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

What am I doing wrong ?

Thank you

0 likes
3 replies
MonsieurRado's avatar

Hi, thank you for your answer. The firewall is disabled

guybrush_threepwood's avatar

Have you tried connecting using telnet to rule out any connectivity issues?

Connect to your server via SSH then type: telnet smtp.mailtrap.io 2525

You should see:

220 smtp.mailtrap.io ESMTP ready

Type quit then enter to exit.

I would also try disabling encryption to narrow the problem down:

MAIL_ENCRYPTION=null

Or try a different port supported by Mailtrap:

These are ports 25, 465, 587 and 2525

Please or to participate in this conversation.