Home > Enterprise >  Laravel with mailcatcher thrown exception Cannot assign requested address
Laravel with mailcatcher thrown exception Cannot assign requested address

Time:10-14

I faced with error when use mailcatcher with laravel ( "laravel/framework": "^8.54",)

"message": "Connection could not be established with host localhost :stream_socket_client(): unable to connect to localhost:8003 (Cannot assign requested address)",
  "exception": "Swift_TransportException",
  "file": "/var/www/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php",
  "line": 261,

So, I sent email like usual, I checked used default mail smpt

config/mail.php

'mailers' => [
    'smtp' => [
        'transport' => 'smtp',
        'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
        'port' => env('MAIL_PORT', 587),
        'encryption' => env('MAIL_ENCRYPTION', 'tls'),
        'username' => env('MAIL_USERNAME'),
        'password' => env('MAIL_PASSWORD'),
        'timeout' => null,
        'auth_mode' => null,
    ],

in code

use Illuminate\Support\Facades\Mail;
//

Mail::send((new ContactUsEmail($contactUs)));

my .env

MAIL_MAILER=smtp
MAIL_HOST=localhost
MAIL_PORT=8003
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
[email protected]
[email protected]
MAIL_FROM_NAME="${APP_NAME}"
MAIL_TO_NAME=admin

When I debugg it in \Swift_Transport_StreamBuffer

params = {array} [8]
 protocol = ""
 host = "localhost"
 port = {int} 8003
 timeout = {int} 30
 blocking = {int} 1
 tls = false
 type = {int} 1
 stream_context_options = {array} [0]

looks like port correct

mailcatcher is available when I open http://localhost:8100/

docker-compose yml:

php-larabostad:
    build:
        context: .
        dockerfile: ./php/Dockerfile
    restart: on-failure
    volumes:
        - "../:/var/www"
        - "./php/php.ini:/usr/local/etc/php/conf.d/custom.ini"
        - "./data/php:/var/larabostad:z"
    networks:
        - php-larabostad-networks
        - mysql-larabostad-networks
        - mailcatcher-larabostad-networks


mailcatcher:
    image: schickling/mailcatcher:latest
    container_name: myapp-mailcatcher
    ports:
        - "8003:1025"
        - "8100:1080"
    networks:
        - mailcatcher-larabostad-networks

docker-compose ps

docker_nginx-larabostad_1   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:443->443/tcp,:::443->443/tcp,                            
                                                                     0.0.0.0:80->80/tcp,:::80->80/tcp                                 
docker_php-larabostad_1     bash /usr/local/bin/docker ...   Up      9000/tcp                                                         
myapp-mailcatcher           mailcatcher --no-quit --fo ...   Up      0.0.0.0:8003->1025/tcp,:::8003->1025/tcp,                        
                                                                     0.0.0.0:8100->1080/tcp,:::8100->1080/tcp                         
mysql8                      docker-entrypoint.sh mysql ...   Up      0.0.0.0:3306->3306/tcp,:::3306->3306/tcp, 33060/tcp   

what I'm doing wrong ?

UPDATE

executed ping from php container

/var/www# ping mailcatcher
PING mailcatcher (172.23.0.2) 56(84) bytes of data.
64 bytes from myapp-mailcatcher.docker_mailcatcher-larabostad-networks (172.23.0.2): icmp_seq=1 ttl=64 time=0.237 ms
64 bytes from myapp-mailcatcher.docker_mailcatcher-larabostad-networks (172.23.0.2): icmp_seq=2 ttl=64 time=0.138 ms

new error

  "message": "Connection could not be established with host mailcatcher :stream_socket_client(): unable to connect to mailcatcher:8003 (Connection refused)",
  "exception": "Swift_TransportException",
  "file": "/var/www/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php",
  "line": 261,

CodePudding user response:

All docker services in compose working in one network (by default). If you want to connect from one service to other - use their name. And, because you connect into private network - use standard port

MAIL_HOST=mailcatcher
MAIL_PORT=1025
  • Related