Siti Rahmah
Siti Rahmah

Reputation: 267

How to send email with laravel?

The application has no problem, I do not change the configuration. A month later i tried the program gets an error.

Error messages :

Swift_TransportException in StreamBuffer.php line 269: Connection could not be established with host smtp.gmail.com [ #0]

This configuration of the env:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl

This configuration of mail.php :

<?php
return [

'driver' => env('MAIL_DRIVER', 'smtp'),

'host' => env('MAIL_HOST', 'smtp.gmail.com'),

'port' => env('MAIL_PORT', 465),

'from' => ['address' => '[email protected]', 'name' => 'Ramadhan'],

'encryption' => env('MAIL_ENCRYPTION', 'ssl'),

'username' => env('MAIL_USERNAME'),

'password' => env('MAIL_PASSWORD'),

'sendmail' => '/usr/sbin/sendmail -bs',

];

Please help, thanks a lot.

Upvotes: 2

Views: 2119

Answers (3)

Sibasankar Bhoi
Sibasankar Bhoi

Reputation: 589

You can use sendgrid. Its very simple.

step-1: Add SendGrid to your composer.json

"require":  
{ 
"sendgrid/sendgrid": "~6.0"
}

step-2:

in .env file set your sendgrid api key

SENDGRID_API_KEY= Your Sendgrid API key 

step-3:

Add following code in your controller

$from = new \SendGrid\Email(null, "your email id");//place senders email id
                        $subject = "checking Email service"; //*your subject goes here*
                        $to = new \SendGrid\Email("Example User", '[email protected]');  //*place reciever email id*
                        $content = new \SendGrid\Content("text/html", $otp);
                        $mail = new \SendGrid\Mail($from, $subject, $to, $content);

                        $apiKey = env('SENDGRID_API_KEY');// set in .env file
                        $sg = new \SendGrid($apiKey);

                        $response = $sg->client->mail()->send()->post($mail);
                        return json_encode(['code' => 200, 'status' => 'Success', 'message' => 'mail sent Sucessfully]);

for better understanding follow below link

https://github.com/sendgrid/sendgrid-php

Upvotes: 2

Hiren Gohel
Hiren Gohel

Reputation: 5042

Try using mailgun. Here i've post the steps to use it:

Step 1: Get the Mailgun API, Sign up to Mailgun, Add Your Domain

Step 2: Configure Laravel Application

In your config/services.php file, add the following:

'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
],

Next, you will need to go to your .env file, and replace the “MAIL_USERNAME”, “MAIL_PASSWORD”, “MAILGUN_DOMAIN” AND “MAILGUN_SECRET” with your own.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=2525
[email protected]
MAIL_PASSWORD=yourmailgunpassword
MAIL_ENCRYPTION=null

MAILGUN_DOMAIN=yourdomain.com
MAILGUN_SECRET=key-YourMailGunSecret

Last step, in the same file, you will need to also add, and replace the value with your own:

MAIL_FROM_ADDRESS: [email protected]
MAIL_FROM_NAME: John

That's it! Hope this will helps you!

Upvotes: 0

Sapnesh Naik
Sapnesh Naik

Reputation: 11636

Try changing encryption and port in your .env file:

MAIL_ENCRYPTION=tls
MAIL_PORT=587

and then run:

$php artisan config:clear

Or, If that doesn't work either then try the hack below:

Add the following lines to _establishSocketConnection() method in Swift/Transport/StreamBuffer.php on line 263:

$options['ssl']['verify_peer'] = FALSE;

$options['ssl']['verify_peer_name'] = FALSE;

Note: The hack specified above is just a workaround and could be overwritten anytime the Swift package updates. So keep that in mind if you try to use this method.

Upvotes: 0

Related Questions