How to fix this error : Connection could not be established with host "mailpit:1025"
I've initially posted this on StackOverflow, I've had answers that didn't help, and my post got hidden because of a duplicate that didn't answer my problem at all. Full error : Connection could not be established with host "mailpit:1025": stream_socket_client(): php_network_getaddresses: getaddrinfo for mailpit failed: Temporary failure in name resolution I'm doing a website using laravel 10 and docker, and when trying to use Laravel's Mail, i get this error. Here is my current configuration and code (just the mail parts) :
.env :
MAIL_DRIVER=smtp
MAIL_ACCOUNT=gmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=finderwebsite1@gmail.com
MAIL_PASSWORD=g***************
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS="finderwebsite1@gmail.com"
MAIL_FROM_NAME="Finder"
SignUp mail class :
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class SignUp extends Mailable
{
use Queueable, SerializesModels;
private $data=[];
public function __construct($data)
{
$this->data=$data;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Sign Up',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.SignUpView',
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
public function build(){
return $this->from('finderwebsite1@gmail.com','Finder')
->subject($this->data['subject'])->view('emails.SignUpView')->with('data',$this->data);
}
}
MailController :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\SignUp;
class MailController extends Controller
{
public function signUp(){
$data=[
'subject'=>'bienvenue sur Finder !',
'body'=>'tranquille ? bien ? !'
];
try{
Mail::to('hernandez.mathieu19@gmail.com')->send(new SignUp($data));
return response()->json(['Great ! your soul is mine ^w^']);
}catch(exception $e){
return $e;
}
}
}
}
web.php :
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\MailController;
...
Route::get('/welcome-mail', [MailController::class,'signUp']);
...
And I simply go to localhost/welcome-mail.
I have tried multiple solutions like using Mailtrap, using smtp, just localhost, but this error is still there. I've also followed different tutorials, but I have a feeling the issue is with Docker (just a guess). I've also tried manually changing config.php, which seems to be using mailpit. Maybe i didn't change it well.
Dockerfile :
FROM php:8.2-apache
# Install dependencies
RUN apt-get update && \
apt-get install -y \
libzip-dev \
zip
# Enable mod_rewrite
RUN a2enmod rewrite
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql zip
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
# Copy the application code
COPY . /var/www/html
# Set the working directory
WORKDIR /var/www/html
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Install project dependencies
RUN composer install
# Set permissions
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache
configs/mail.php :
...
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'url' => env('MAIL_URL'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'local_domain' => env('MAIL_EHLO_DOMAIN'),
],
...
cache/config.php :
...
'mail' =>
array (
'default' => 'smtp',
'mailers' =>
array (
'smtp' =>
array (
'transport' => 'smtp',
'url' => NULL,
'host' => 'mailpit',
'port' => '1025',
'encryption' => NULL,
'username' => NULL,
'password' => NULL,
'timeout' => NULL,
),
'ses' =>
array (
'transport' => 'ses',
),
'postmark' =>
array (
'transport' => 'postmark',
),
'mailgun' =>
array (
'transport' => 'mailgun',
),
'sendmail' =>
array (
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs -i',
),
'log' =>
array (
'transport' => 'log',
'channel' => NULL,
),
'array' =>
array (
'transport' => 'array',
),
'failover' =>
array (
'transport' => 'failover',
'mailers' =>
array (
0 => 'smtp',
1 => 'log',
),
),
'roundrobin' =>
array (
'transport' => 'roundrobin',
'mailers' =>
array (
0 => 'ses',
1 => 'postmark',
),
),
),
'from' =>
array (
'address' => 'hello@example.com',
'name' => 'Laravel',
),
'markdown' =>
array (
'theme' => 'default',
'paths' =>
array (
0 => '/var/www/html/resources/views/vendor/mail',
),
),
),
...
I don't know if I should try to modify cache/config.php again.
@540mat can you clear your config cache ( php artisan config:clear
) because it looks like you have some old settings cached.
Ps. I have updated your post to get the docblocks working :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community