/usr/libexec/postfix/smtp does not send mail using Laravel on macOS - php

I have a Laravel 8 project that I want to send emails with. I develop it on macOS and I want to test it, but it does not work, but won't give any errors. (I replaced sensitive data with ********)
.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=********#gmail.com
MAIL_PASSWORD=********
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=********#gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Note: I validated those settings, I also tested other working SMTP settings. So it's not a configuration issue in this file
mail.php
/// ...
'default' => env('MAIL_MAILER', 'mail'),
/// ...
'mailers' => [
'smtp' => [
'transport' => 'mail',
'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,
],
/// ...
],
/// ...
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
resource_path('views/emails'),
],
],
/// ...
MailController.php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as Controller;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
public function testMail(Request $request)
{
$email = '********#live.com';
$data = [
'title' => 'Test mail'
];
Mail::send(['html' => 'emails.testMail'], $data,
function ($message) use ($email, $data) {
$message->to($email)
->subject($data['title']);
});
$response = [
'error' => 0,
'to' => $email,
'data' => $data,
];
return response()->json($response, 200);
}
}
Note: There is a blade template containing only HTML for test purposes at resources/views/emails/testMail.blade.php. I also added the appropriate GET-route.
When I access the endpoint in Postman, I get
{
"error": 0,
"to": "********#live.com",
"data": {
"title": "Test mail"
}
}
Little Snitch popped up, asking if I want to allow /usr/libexec/postfix/smtp to access smtp.gmail.com, so I know the call goes through. I gave the process all rights to access any server and retried, but the mail will never reach my destination.
Yes, I checked the SPAM folder. Yes, I checked other credentials. Yes I tested manual delivery between both accounts. Yes, I tested with Little Snitch disabled, entirely. Yes, I checked the laravel.log and it reveals nothing.
I noticed, that I won't get any error from Laravel when I put in wrong information in the .env file (except when I change MAIL_MAILER).
How can I get the output from the SMTP process to find out what the issue is? How can I fix the issue?

Related

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
MAIL_USERNAME=mr.xxxxxxx#gmail.com
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' => 'muhamadramadhan95#gmail.com', '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.
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", 'example#gmail.com'); //*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
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
MAIL_USERNAME=postmaster#yourdomain.com
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: hello#yourdomain.com
MAIL_FROM_NAME: John
That's it! Hope this will helps you!
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.

Laravel and Mailgun doesn't work correctly

Before production version I used mailtrap to test my emails and everything worked like it should be, but today I uploaded my website to a public server and decided to use mailgun, I know its not so simple like mailtrap.io, but still. Anyway I verified my account and can now send 10k emails per month. Not bad, but the thing is when I try to send an email I get a notification that the email was sent, but there is no email in any inbox.
My .env file
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=myEmail
MAIL_PASSWORD=myPassword
MAIL_ENCRYPTION=tls
My services.php file
return [
'mailgun' => [
'domain' => env('myDomain'),
'secret' => env('secretKey'),
],
'ses' => [
'key' => env('SES_KEY'),
'secret' => env('SES_SECRET'),
'region' => 'us-east-1',
],
'sparkpost' => [
'secret' => env('SPARKPOST_SECRET'),
],
'stripe' => [
'model' => App\User::class,
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
];
One of the functions:
public function store(StoreListingContactFormRequest $request, Area $area, Listing $listing){
Mail::to($listing->user)->queue(
new ListingContactCreated($listing,
$request->name,
$request->email,
$request->number,
$request->message
)
);
return back()->withSuccess("Teie sõnum on edukalt saadetud firmale {$listing->user->name}");
}
Another one:
public function __construct()
{
$this->middleware(['auth']);
}
public function index(Area $area, Listing $listing){
return view('listings.share.index', compact('listing'));
}
public function store(StoreListingShareFormRequest $request, Area $area, Listing $listing){
collect(array_filter($request->emails))->each(function($email) use ($listing, $request){
Mail::to($email)->queue(
new ListingShared($listing, $request->user(), $request->messages)
);
});
return redirect()->route('listings.show',[$area, $listing])->withSuccess('Kuulutus on jagatud edukalt!');
}
seems the error is here
MAIL_USERNAME=myEmail
MAIL_PASSWORD=myPassword
MAIL_USERNAME should be not your email but Default SMTP Login from your domain settings page. And Default Password on the same page for MAIL_PASSWORD
'domain' => env('myDomain'),
here you need to enter not your site's domain but the domain you've registered on mailgun, something like mg.exmaple.com

unable to send mail using laravel php

I am unable to send a test mail. I am using laravel php.
my .env file:
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=ca**********4b
MAIL_PASSWORD=04**********7b
MAIL_ENCRYPTION=tls
my mail.php:
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'mailtrap.io'),
'port' => env('MAIL_PORT', 2525),
'from' => ['address' =>'******#gmail.com', 'name' => '****'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('ca**********4b'),
'password' => env('04**********7b'),
'sendmail' => '/usr/sbin/sendmail -bs',
my controller:
<?php
namespace App\Http\Controllers;
use Request;
use Response;
use App\Users;
use Mail;
class MailController extends Controller
{
public function basic_email(){
$data = array('name'=>"Virat Gandhi");
Mail::send(['text'=>'welcome'], $data, function($message) {
$message->to('******#gmail.com', '*****')->subject
('Laravel Basic Testing Mail');
$message->from('"******#gmail.com','Virat Gandhi');
});
return "Basic Email Sent. Check your inbox.";
}
}
i am getting this error:
Swift_TransportException in AbstractSmtpTransport.php line 383:
Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required
"
tried clearing cache by php artisan config:cache
what do i need to try more?
I believe it's gonna be firewall issue, have you tried to connect to gmail provider ? it's a good way to catch the error

Failed to send an email in laravel 5

My code is like this :
public function sendMail(array $data)
{
$data = explode('#', $data['id']);
$email_from = Auth::user()->email;
$email_to = $data[4];
$subject = 'Send Email Test';
$data_user = ['user_name' => $data[1], 'full_name' => $data[2].' '.$data[3] ];
$sent = Mail::send('backend.auth.success_approved', $data_user, function ($mail) use ($email_to, $email_from, $subject)
{
$mail->from($email_from)
->to($email_to)
->subject($subject);
});
}
My configuration in mail.php :
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'ssl://secure.emailsrvr.com'),
'port' => env('MAIL_PORT', 465),
'from' => ['address' => 'myemail#chel.com', 'name' => 'myname'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME', 'myemail#chel.com'),
'password' => env('MAIL_PASSWORD', 'mypassword'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
There is error message :
Swift_TransportException in StreamBuffer.php line 265: Connection could not be established with host ssl://secure.emailsrvr.com [php_network_getaddresses: getaddrinfo failed: The requested name is valid, but no data of the requested type was found. #0].
How to solve this problem?
Thank you.
From the error message I believe that for some reason the domain (secure.emailsrvr.com) of the mail server cannot be resolved.
If you are on a shared hosting you should ask your hosting provider, if you are on a dedicated server or vps you should ping the hostname and see if it can be resolved.
I've done the following things, it worked for me.
Create an email account on the server. Now we have MAIL_USERNAME and MAIL_PASSWORD.
Make the following changes to create global variables in .env file of Laravel framework.
MAIL_DRIVER=smtp
MAIL_HOST=yourhost
MAIL_PORT=465
MAIL_ENCRYPTION=ssl
MAIL_USERNAME=youremail#something.com
MAIL_PASSWORD=yourpassword
Or else you can add the above changes in your config/mail.php. It'll work.
some email parameters in laravel 5+, are defined in .env file, sometimes laravel dont recognize other parameters outside .env file,
check first if your parameters are sent, if not try to send an email to your personal account and try to change the parameters in the .env file

Laravel Password remind - connection refused 61

I am stumped.
I am using Laravel's Password::remind, which has already been written for me, so there is nothing that I have changed:
try {
$reset = Password::remind($credentials);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
When I submit the form, then I receive the following exception:
Exception
Connection could not be established with host localhost [Connection refused #61]
Which points to my throw exception line above
In my app/config/mail.php file, I have tried everything from mail to sendmail, from localhost to smtp.gmail.com - whatever I change in this config file, Laravel still thinks that it is localhost. Even tried "/usr/sbin/sendmail -t -i"
I have restarted apache and fpm - the error does not change.
When I try mail(email, title, message) - it works just fine. Of course, my goal is to not just send an email but to use Laravel's Password::remind - function where it sends an email with a link for the user to reset their password.
I have changed the /usr/local/etc/php/5.5/php.ini file, both the smtp and smtp_port
What do I need to do, this seems so straight forward in their documentation and no one else has complained about this issue for connection refused # 61. There are other connection refused and they have nothing to do with the built in Password::remind. This is driving me nuts.
I am running fpm-nginx.
Thanks in advance
Just to be on the safe side in respect to any configuration issues, I suggest you to try your application in a closed environment such as Homestead. This way, i.e. by relying on a fresh virtual machine, you might be able to figure out whether it is a configuration issue on the level of the different applications (apache, php, etc.) you are using. Otherwise, you would have to reinspect your code again. You can find more information on Homestead here: http://laravel.com/docs/4.2/homestead
OK, there were a couple of configurations that had to be in place and I am posting this answer in case anyone else using Yosemite is having this issue.
First, from my searching for the error "Connection refused #61" this is usually related to connectivity with a database as Korush suggested above. However, if I typed in an email that was not part of the database, Laravel would come back with a message that such and such email was not found, which told me that it was connected to the database, from the stand point of searching the email that was entered.
However, if a person does not have a "password_reminders" table in their localhost database, then a person would receive a connection refused error - be sure that you have this for Laravel to use in your localhost db:
CREATE TABLE password_reminders (
email VARCHAR(50) NOT NULL,
token VARCHAR(100) NOT NULL,
created_at TIMESTAMP
)
Second, Laravel can use the mail server on your system. In my case, I am using Yosemite, which has "postfix" available in the terminal:
sudo postfix start
Here is my local config which allows Laravel to use the "password_reminders" table, which is located in app/config/database.php:
'local' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'yourdb',
'username' => 'yourusername',
'password' => 'yourpassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Within the app/config/mail.php:
'driver' => 'smtp',
'host' => 'localhost',
'port' => 25,
'from' => array('address' => 'service#yourdomain.com', 'name' => 'Your Company'),
'encryption' => '',
'username' => null,
'password' => null,
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
I still need to figure out how to get the redirects and messages to display, but this is working for the emailing a link to reset the password:
public function request()
{
$message = "";
$session = null;
$request = array('email' => Input::get('USER_EMAIL'));
Password::remind(Input::only('USER_EMAIL'), function($message)
{
$message->subject('Password Reminder');
});
if ($request == 'reminders.sent') {
$session = 'message';
$success = true;
$message = 'Email with further instruction has been sent to "' . Input::get('USER_EMAIL'). '".';
return Password::remind($request);
} elseif ($request == 'reminders.user') {
$session = 'error';
$success = false;
$message = 'Email Address: ' . Input::get('USER_EMAIL') . ' WAS NOT FOUND!';
return Password::remind($request);
}else{
$message = 'Not meeting either condition "' . Input::get('USER_EMAIL') . '".';
return Password::remind($request);
}
Session::flash($session, $message);
return Redirect::to('/').with($session, $message);
}
Here are my routes related to password remind:
Route::get('password/reset', array(
'uses' => 'PasswordController#remind',
'as' => 'password.remind'
));
Route::post('password/reset', array(
'uses' => 'PasswordController#request',
'as' => 'password.request'
));
Route::get('password/reset/{token}', array(
'uses' => 'PasswordController#reset',
'as' => 'password.reset'
));
Route::post('password/reset/{token}', array(
'uses' => 'PasswordController#update',
'as' => 'password.update'
));
Maybe it is trying to send an e-mail and it is not working.

Categories