【问题标题】:Laravel with mailcatcher thrown exception Cannot assign requested addressLaravel 与 mailcatcher 抛出异常无法分配请求的地址
【发布时间】:2021-10-12 10:16:48
【问题描述】:

在 laravel ("laravel/framework": "^8.54",) 中使用 mailcatcher 时遇到错误

"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,

所以,我像往常一样发送电子邮件,我检查了使用的默认邮件smpt

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

在代码中

use Illuminate\Support\Facades\Mail;
//

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

我的 .env

MAIL_MAILER=smtp
MAIL_HOST=localhost
MAIL_PORT=8003
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=from_admin@admin.com
MAIL_TO_ADDRESS=to_admin@admin.com
MAIL_FROM_NAME="${APP_NAME}"
MAIL_TO_NAME=admin

当我调试它时 \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]

看起来端口正确

当我打开http://localhost:8100/时可以使用mailcatcher

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   

我做错了什么?

更新

从 php 容器执行 ping

/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

新错误

  "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,

【问题讨论】:

    标签: php laravel docker-compose mailcatcher


    【解决方案1】:

    compose 中的所有 docker 服务都在一个网络中工作(默认情况下)。如果您想从一项服务连接到另一项服务,请使用他们的名称。而且,因为您连接到专用网络 - 使用标准端口

    MAIL_HOST=mailcatcher
    MAIL_PORT=1025
    

    【讨论】:

    • 没用,我更新了我的问题
    • @shuba.ivan 更新
    • 你是对的,谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多