unable to send mail using laravel php - 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

Related

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

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?

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.

php laravel 5.2.20 send smtp mail

I want send smtp mail in laravel 5.2.20 in php
But i am getting this error
Whoops, looks like something went wrong.
1/1
Swift_TransportException in AbstractSmtpTransport.php line 383:
Expected response code 250 but got code "530", with message "530-5.5.1 Authentication Required. Learn more at
530 5.5.1 https://support.google.com/mail/answer/14257 ra1sm17783395pab.24 - gsmtp
"
My config/mail.php file:
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'example#gmail.com', 'name' => 'example'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('example#gmail.com'),
'password' => env('example_pwd'),
'sendmail' => '/usr/sbin/sendmail -bs',
This is my laravel 5.2.20 framework mail.php but i don't update anything in my .env file. In my laravel framework Dotenv.php file is their but .env is not here. Which one i need to update....
My controller code is below here:
$user = Users::findOrFail($id);
Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
//echo "<pre>";print_r($user->email);
//echo "<pre>";print_r($user->name);
//exit;
$m->from('example#gmail.com', 'Your Application');
$m->to($user->email, $user->name)->subject('Your Reminder!');
});
Ther error Authentication Required.
You need to put your username and password for SMTP settings, put them in .env file if you dont have one create it
cp .env.example .env
php artisan key:generate
MAIL_DRIVER=smtp
MAIL_HOST=your_SMTP HOST
MAIL_PORT=YOUR SMTP PORT
MAIL_USERNAME=USERNAME
MAIL_PASSWORD=PASSWORD
MAIL_ENCRYPTION=null

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

Internal server error using Swiftmailer on Silex

I'm working on a Silex project and I'm trying to send an email using the Swiftmailer provider but always get an internal server error(500).
Registering:
$app->register(new Silex\Provider\SwiftmailerServiceProvider(), array(
'swiftmailer.options' => array(
'host' => 'smtp.gmail.com',
'port' => '465',
'username' => 'my_email',
'password' => 'my_password')
));
If I call the mailer $app['mailer'] throw an internal server error.
If I try with the documentation example I got an internal server error:
$app->post('/feedback', function () use ($app) {
$request = $app['request'];
$message = \Swift_Message::newInstance()
->setSubject('[YourSite] Feedback')
->setFrom(array('noreply#yoursite.com'))
->setTo(array('feedback#yoursite.com'))
->setBody($request->get('message'));
$app['mailer']->send($message);
return new Response('Thank you for your feedback!', 201);
});
I don't know what's the problem, I have no troubles with the other providers.
Any ideas?
Would it help to add the following entries in your 'swiftmailer.options' array? Gmail by default uses ssl on port 465.
'encryption' => 'ssl',
'auth_mode' => 'login',
[Update]
If you define namespace in your code, need to add '\' in front of Silex\Provider..
$this->register(new Silex\Provider\SwiftmailerServiceProvider(), array(
to
$this->register(new \Silex\Provider\SwiftmailerServiceProvider(), array(

Categories