1

This question already asked but that not solving my issue.

Am trying to send email in my laravel project. But i got error message like

Swift_TransportException: Connection could not be established with host mailtrap.io [Connection timed out #110] in file /home/bala/git/mobile-api/modules/jobs/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php on line 269

This is my code:

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587   
MAIL_USERNAME=mygmail@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls

AppJobsController.php

<?php

namespace App\Http\Controllers\Jobs;

require app_path().'/../../../lib/vendor/autoload.php';
include_once(app_path().'/../../../lib/utils.php');
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Routing\ResponseFactory;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Input;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\View;
use App\Models\Jobs\JobExperience;
use App\Models\Jobs\JobSalary;
use App\Models\Jobs\JobTitle;
use App\Models\Jobs\JobDetails;
use App\Models\Jobs\JobApply;
use App\Models\Jobs\ModuleItems;
use Illuminate\Http\Response;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Validator;
use Config;
use File;
use DB;
use Mail;

class AppJobsController extends Controller
{
    public function jobApply(Request $request)
    {
        $JobApply = new JobApply;
        $JobApply->job_id = $request->job_id;
        $JobApply->attachment = $images[0];
        $JobApply->user_id = $this->user['uid'];
        if ($JobApply->save()) 
        {
            $jobDetails = JobDetails::find($request->job_id);
            $JobTitle = JobTitle::find($jobDetails->jobtitle_id);

            $content =  array(
                    'JobTitle' => $JobTitle->title,
                    'CompanyName' => $jobDetails->company_name,
                    'postedDate' => date('d-m-Y', strtotime($jobDetails->created_at)),
                    'user' => 'App User'
                );

            $subject = 'Applying For '.$JobTitle->title;

            $toUser = $jobDetails->email_id;

            $pathToFile = $this->uploadlocation.'/'.$images[0];

            Mail::send('email.sendResumes', $content, function ($message) use ($pathToFile, $toUser, $subject)
            {
                $message->from('noreply@gmail.com', 'Job Appy');
                $message->subject($subject);
                $message->to($toUser); 
            });
        }
    }
}

email.sendResumes

<div>
        I would like to apply for {{$JobTitle}} within your company {{$CompanyName}}, adversited in Thedal on {{$postedDate}}.<br><br>

        I believe this position would provide a great opportunity to further my chosen career. My willingness to work hard and build my skill base will make me a good candidate for the job.<br><br>

        I look forward to hearing from you at your convenience.<br><br>

        Thanks & Regards,<br><br>

        {{$user}}
</div>

I tried this question answer but i not solve my issue.

Thanks in advance..!

Ramesh S
  • 841
  • 3
  • 15
  • 35
  • whats in your mail.config ? the application is not able to connect to mail server. probably a wrong port or server ? From your error i can see the app is still using mailtrap.io, but your host is smtp.gmail.com. Please chec themail.config too. & flush the config cache : php artisan cache:clear – Shan Jan 19 '19 at 08:42

2 Answers2

1

I think you should restart your server.

Rakibul Hasan
  • 61
  • 2
  • 9
1

Steps to configure gmail:

MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your@gmail.com
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls

For app password Read Under the Section How to generate an App password:

After completion of .env edit please enter this command in your terminal for clear cache:

php artisan config:cache
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64