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

kuns25's avatar

Laravel Mail Working locally but not on server

Hii

     I have created a simple contact form to send mail  on website owner email id ,  everything is working fine on my localserver im getting the mail , but when uploaded on hosting its giving me below error
Connection could not be established with host smtp.mailtrap.io [Connection refused #111]

When i googled this error i came to know instead of smtp mail driver use sendmail or mailgun , yeah when i did this i didn' get error and got the success msg , but not receiving the mail .

also i have tried config:clear cmd .

please tell me where am i going wrong.

My code

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=25
MAIL_USERNAME=myusername
MAIL_PASSWORD=mypass
MAIL_ENCRYPTION=

mailcontroller

    Mail::send(new SendMail);
        return back()->with('msg','Thank You For Contacting us,we will get back to you soon');

sendmail.php

 public function build(Request $request)
    {
       return $this->view('email',['fname'=>$request->fname,'lname'=>$request->lname,'msg'=>$request->msg,'mobile'=>$request->mobile,'email'=>$request->email])->to('[email protected]')->subject('Website Contact Form');
   }

Thanks in advance

0 likes
12 replies
munazzil's avatar

Use mail port 587`` or either465```,

     MAIL_PORT= 587 or 465
rawilk's avatar

If you're using mailtrap, the port is typically 2525, but just check your settings and it will tell you which port to use, along with any other credentials you need to use.

1 like
kuns25's avatar

I've tried with gmail as well was having same issue . I've already used laravel mail in many project never had any big issue . Should i use laravel notification or event

Nash's avatar

Try with MAIL_ENCRYPTION=tls as well

kuns25's avatar

no sir its not working. i searched again that laravel not sending mail not giving error , in answers its written try smtp instead of sendmail , but again Swift_TransportException Connection could not be established with host smtp.mailtrap.io [Connection refused #111] error

kuns25's avatar

to test ive installed telescope laravel itself is not sending mail i can't find anything in mail of telescope

D9705996's avatar
D9705996
Best Answer
Level 51

You need to identify if the issue is mailgun or your code. Follow the setup from below

https://laravel.com/docs/5.8/mail#introduction

And try sending a mailable through tinker. If it works it's your code, otherwise it's your server/network setup.

If it's your network( which I suspect from it works in localhost), from the command line of your server (not your pc) try telnet smtp.mailgun.org 25 replacing the detials with your setup. If this errors you have a network issue that we cant really help with ;(

1 like
kuns25's avatar

It was my hosting issue , i was using godaddy , when i try it on siteground it worked .

Please or to participate in this conversation.