Mailer Error: SMTP Error: Could not connect to SMTP host - laravel

How to resolve this error? I do all possible things to resolve this issue but it cannot resolve. Here is my .env file code
MAIL_DRIVER=smtp
MAIL_HOST=fluorine.cloudhosting.co.uk
MAIL_PORT=465
MAIL_USERNAME=contacts#cleansafeltd.com
MAIL_PASSWORD=Hggjgjgghv123
MAIL_ENCRYPTION=ssl
Here is the code of mail.php.
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'fluorine.cloudhosting.co.uk'),
'port' => env('MAIL_PORT', 465),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'contacts#cleansafeltd.com'),
'name' => env('MAIL_FROM_NAME', 'Contact'),
],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
Here is the mailController
public function sendMail($to_email, $template_id, $users_insertId = 0) {
set_time_limit(0);
$mail = new PHPMailer(true);
$mail->IsSendmail();
$mail->IsSMTP(true);
$mail->Host = "fluorine.cloudhosting.co.uk";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "website#cleansafeltd.com";
$mail->Password = "Star9000!";
$mail->Port = "465";
$mail->IsHTML(true);
}
I do all thin with tls or port 587 also but not solved.

Using your credentials (by the way, you shouldn't post your password in public places, you better change it), I was able to send myself a mail using the following code
Mail::to('mymail#domain')->send(new \App\Mail\TestMail);
\App\Mail\TestMail is a Mailable class, defined as follows
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* #return $this
*/
// Optional params are commented out.
// replyTo changes the address one gets when clicking the reply button
public function build()
{
return $this->from(env('MAIL_USERNAME'), 'An alias name')
// ->to('mail#domain', 'alias name for recipient')
// ->cc('mail2#domain', 'alias name for cc')
// ->bcc('mail3#domain', 'alias name for bcc')
// ->replyTo('mail4#domain', 'alias for replyTo')
->subject('This is a test')
->view('mails.sample_mail'); // This is a laravel view.
}
}

Try this one:
Setup your gmail account
Go to settings -> Forwarding and POP/IMAP
- Check -> POP download: Enable POP for all mail (even mail that's already been downloaded)
- Check -> IMAP access: Enable IMAP
- Save
Go to Google Account -> security -> Less secure app access
Set to on instead of off
Set this to your .env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=your email username
MAIL_PASSWORD=your email password
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=your email address
MAIL_FROM_NAME=Your email name
Now you can send email with your smtp gmail account

Related

Mail Error:- Failed to authenticate on SMTP server with username "support#myDomain.io" using 3 possible authenticators

Google mail is running perfectly for sending mail but domain-mail (support.myDomain.io) returns following errors
Swift_TransportException Failed to authenticate on SMTP server with username "support#xamer.io" using 3 possible authenticators. Authenticator LOGIN returned Expected response code 235 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials o28-20020aa7979c000000b005809a3c1b6asm11901153pfp.201 - gsmtp ". Authenticator PLAIN returned Expected response code 235 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials o28-20020aa7979c000000b005809a3c1b6asm11901153pfp.201 - gsmtp ". Authenticator XOAUTH2 returned Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials o28-20020aa7979c000000b005809a3c1b6asm11901153pfp.201 - gsmtp ".
env code :-
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=support#myDomain.io
MAIL_PASSWORD=******
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=support#myDomain.io
MAIL_FROM_NAME="${APP_NAME}"
controller code :-
Mail::to($data->email)->send(new EmailVerify($data));
app/Mail/EmailVerify.php :-
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class EmailVerify extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject('Email from '.env('APP_NAME'))->view('mail.emailVerify')->with(['data' => $this->data]);
}
}
config/mail.php :-
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Mailer
|--------------------------------------------------------------------------
|
| This option controls the default mailer that is used to send any email
| messages sent by your application. Alternative mailers may be setup
| and used as needed; however, this mailer will be used by default.
|
*/
'default' => env('MAIL_MAILER', 'smtp'),
/*
|--------------------------------------------------------------------------
| Mailer Configurations
|--------------------------------------------------------------------------
|
| Here you may configure all of the mailers used by your application plus
| their respective settings. Several examples have been configured for
| you and you are free to add your own as your application requires.
|
| Laravel supports a variety of mail "transport" drivers to be used while
| sending an e-mail. You will specify which one you are using for your
| mailers below. You are free to add additional mailers as required.
|
| Supported: "smtp", "sendmail", "mailgun", "ses",
| "postmark", "log", "array"
|
*/
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'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,
],
'ses' => [
'transport' => 'ses',
],
'mailgun' => [
'transport' => 'mailgun',
],
'postmark' => [
'transport' => 'postmark',
],
'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
'array' => [
'transport' => 'array',
],
],
/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
/*
|--------------------------------------------------------------------------
| Markdown Mail Settings
|--------------------------------------------------------------------------
|
| If you are using Markdown based email rendering, you may configure your
| theme and component paths here, allowing you to customize the design
| of the emails. Or, you may simply stick with the Laravel defaults!
|
*/
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
when I am using google mail with same code is running perfectly
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=mymail.gmail.com
MAIL_PASSWORD=*******
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mymail.gmail.com
MAIL_FROM_NAME="${APP_NAME}"
I am trying to send mail from my domain email (support#myDomain.com) in laravel.
I googled it a lot, but could not find a proper answer. I would be very glad if any of you can help me.
Did you turn on the "Allow less secure apps" on? if not go to this link
https://myaccount.google.com/security#connectedapps
Look at the Sign-in & security -> Apps with account access menu.
You must have to turn the option "Allow less secure apps" ON.
If it still doesn't work try one of these:
Go to https://accounts.google.com/UnlockCaptcha , and click continue and unlock your account for access through other media/sites.
Use double quote in .env your password: "your password"
also don't forget to clear the cache
php artisan config:clear

Laravel 9 Symfony mailer not working - mail sent from app but not received in gmail

In Laravel 8 I was able to send an email verification email. And now in Laravel 9 email sent message appears but I don't receive an email.
I have used my correct mail host provided by domain with port, username, and password as previously and checked enough times. I have also changed ssl to tls but no success.
.env file
MAIL_MAILER=smtp
MAIL_HOST=mail.getalow.com
MAIL_PORT=465
MAIL_USERNAME=********
MAIL_PASSWORD=*********
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS="support#getalow.com"
MAIL_FROM_NAME="${APP_NAME}"
I also added 'verify_peer' => false in mail.php config file but no success.
mail.php
'smtp' => [
'transport' => 'smtp',
'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,
'verify_peer' => false, // <------ IT HAS TO BE HERE
],
my User.php model also has implements MustVerifyEmail as I do everytime.
class User extends Authenticatable implements MustVerifyEmail
In registration controller event(new Registered($user)); is presented
RegistrationController.php
// Register a new user
public function register(RegistrationRequest $request) {
$user = User::create($request->validated())->assignRole(['User', 'Commenter']);;
event(new Registered($user));
Auth::login($user);
return to_route('home')
->with(Helpers::Toast(
'Welcome ' . $user->name,
'Your account has been created.',
'success',
'5000'
));
}
I tried it from localhost to mailtrap it works. But when I upload it to server with all details of server in env file as above. I receive success message as in registration controller, but I don't receive activation email on my registered email.
I am using shared hosting, cpanel as hosting provider
What am I missing please help. I stucked on it for 4 days.
You said you are using cPanel shared hosting server.
There is no problem in your Laravel 9 code. The problem is on your DNS setting of your email in cPanel. You have to change CNAME, MX, TXT records on cPanel as provided by your hosting provider.
For example:
If you are using ohodomain hosting provider.
Go to account settings.
Select your domain from list of your domains.
Go to Manage Email.
There you will see the error with its solution that you have to solve.
Add the provided CNAME, MX, TXT records on your cPanel's Zone Editor.

How to fix this issue? Couldn't open stream {imap.gmail.com:993/imap/ssl}. [AUTHENTICATIONFAILED] Invalid credentials

I am using laravel-IMAP to integrate Gmail with my laravel project and package I follow is Webklex/laravel-imap.
Issue is when i test my code the error poped-up, I followed to many links to figure-out my issue, one solution i find is to enable IMAP access: Enable IMAP, but it is not a solution for all the users, I want to fetch the emails of that user's those who will use this application with doing enable imap access. Is there any other solution to overcome this problem?
Controller
class TestController extends Controller
{
public function index(){
$oClient = new Client([
'host' => 'imap.gmail.com',
'port' => 993,
'encryption' => 'ssl',
'validate_cert' => true,
'username' => '**********',
'password' => '**********',
'protocol' => 'imap'
]);
/* Alternative by using the Facade
$oClient = Webklex\IMAP\Facades\Client::account('default');
*/
//Connect to the IMAP Server
$oClient->connect();
//Get all Mailboxes
/** #var \Webklex\IMAP\Support\FolderCollection $aFolder */
$aFolder = $oClient->getFolders();
//Loop through every Mailbox
/** #var \Webklex\IMAP\Folder $oFolder */
foreach($aFolder as $oFolder){
//Get all Messages of the current Mailbox $oFolder
/** #var \Webklex\IMAP\Support\MessageCollection $aMessage */
$aMessage = $oFolder->messages()->all()->get();
/** #var \Webklex\IMAP\Message $oMessage */
foreach($aMessage as $oMessage){
echo $oMessage->getSubject().'<br />';
echo 'Attachments: '.$oMessage->getAttachments()->count().'<br />';
echo $oMessage->getHTMLBody(true);
}
}
}
}

Laravel and Amazon SES

I am setting up Amazon SES for the first time. Following the documentation on Laravel website I have installed a package and started to set up mail.
mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'ses'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
services.php
'ses' => [
'key' => env('SES_KEY'),
'secret' => env('SES_SECRET'),
'region' => 'eu-west-1',
],
.env
MAIL_DRIVER=ses
SES_KEY=ASKFKGDRJ3
SES_SECRET=kdfsjjdsfjdfsjdfsj
MAIL_HOST=email.eu-west-1.amazonaws.com
MAIL_PORT=587
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
Mail/WelcomeEmail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('test#gmail.com')
->view('emails.welcomeEmail');
}
}
welcomeEmail.blade.php
<p>This is a test email from test email address, let me know on slack if you receive it</p>
Controller:
public function map(Request $request)
{
Mail::to($request->user())->send(new WelcomeEmail());
return view('profile.map');
}
And error:
Error executing "SendRawEmail" on "https://email.eu-west-1.amazonaws.com"; AWS HTTP error: Client error: `POST https://email.eu-west-1.amazonaws.com` resulted in a `403 Forbidden` response:
<ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
<Error>
<Type>Sender</Type>
<Code>SignatureDo (truncated...)
SignatureDoesNotMatch (client): The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.
The Canonical String for this request should have been
'POST
/
aws-sdk-invocation-id:7a73507566587348bba7c543661be161
aws-sdk-retry:0/0
host:email.eu-west-1.amazonaws.com
x-amz-date:20170726T195108Z
aws-sdk-invocation-id;aws-sdk-retry;host;x-amz-date
7a1f353a7f93f014d66ee19fb4b9661a79fea8411d1f97af2799c0cc04dc57dc'
The String-to-Sign should have been
'AWS4-HMAC-SHA256
20170726T195108Z
20170726/eu-west-1/ses/aws4_request
c2422180627319d05721ed6a2dc3973f7a508c34e4b2f9699d0a7bbf0c56d6a8'
- <ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
<Error>
<Type>Sender</Type>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.
The Canonical String for this request should have been
'POST
/
aws-sdk-invocation-id:7a73507566587348bba7c543661be161
aws-sdk-retry:0/0
host:email.eu-west-1.amazonaws.com
x-amz-date:20170726T195108Z
aws-sdk-invocation-id;aws-sdk-retry;host;x-amz-date
7a1f353a7f93f014d66ee19fb4b9661a79fea8411d1f97af2799c0cc04dc57dc'
The String-to-Sign should have been
'AWS4-HMAC-SHA256
20170726T195108Z
20170726/eu-west-1/ses/aws4_request
c2422180627319d05721ed6a2dc3973f7a508c34e4b2f9699d0a7bbf0c56d6a8'
</Message>
</Error>
<RequestId>c458f296-723b-11e7-a686-515a08ffcc2f</RequestId>
</ErrorResponse>
However, I am sure that SES_key and secret is correct, domain is verified, and email also, what am I missing?
Yes, it not says that the email I am sending to is not verified?
This means you're in the SES "sandbox".
http://docs.aws.amazon.com/ses/latest/DeveloperGuide/request-production-access.html
During development:
You can only send mail to the Amazon SES mailbox simulator and to verified email addresses and domains.
You can only send mail from verified email addresses and domains.
You can send a maximum of 200 messages per 24-hour period.
Amazon SES can accept a maximum of one message from your account per second.
Moving from sandbox to production (where you can send email to anyone) is easy enough - just fill out the form at https://aws.amazon.com/ses/extendedaccessrequest/.
it's means your in sandbox mode and in sandbox mode only verified email get maild so either move from sandbox to production or verify your mail for testing mail but at last you have to move in production

Sending mail with SwiftMailer in Laravel 4

I am receiving a the following error:
Cannot send message without a recipient
This is while trying to send a email using swiftmailer. My code works in localhost and has all the parameters needed from sender to receiver but it throws an error saying it cannot send without recipient.
Here is my code:
public function email()
{
Mail::send('emails.auth.mail', array('token'=>'SAMPLE'), function($message){
$message = Swift_Message::newInstance();
$email = $_POST['email']; $name = $_POST['name']; $subject = $_POST['subject']; $msg = $_POST['msg'];
$message = Swift_Message::newInstance()
->setFrom(array($email => $name))
->setTo(array('name#gmail.com' => 'Name'))
->setSubject($subject)
->setBody($msg);
$transport = Swift_MailTransport::newInstance('smtp.gmail.com', 465, 'ssl');
//$transport->setLocalDomain('[127.0.0.1]');
$mailer = Swift_Mailer::newInstance($transport);
//Send the message
$result = $mailer->send($message);
if($result){
var_dump('worked');
}else{
var_dump('Did not send mail');
}
}
}
You can do this without adding your the SMTP information in your Mail::send() implementation.
Assuming you have not already, head over to app/config/mail.php and edit the following to suit your needs:
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'your_username',
'password' => 'your_password',
Then your code should be as simple as:
public function email()
{
Mail::send('emails.auth.mail', array('token'=>'SAMPLE'), function($message)
{
$message->from( Input::get('email'), Input::get('name') );
$message->to('name#gmail.com', 'Name')->subject( Input::get('subject') );
});
}
So, hopefully the issue is one of configuration. Do you have other environments setup that might be over-riding the app/config/mail.php configuration settings in your server where it's not working?
Please make sure you have configured all the necessary configuration correctly on
/app/config/mail.php. Ensure all the configuration is correct for the environment where the email is not working correctly.
If you want to use your Gmail account as an SMTP server, set the following in app/config/mail.php:
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'your-email#gmail.com',
'password' => 'your-password',
When switching to online server, you want to protect this file or switch provider so as not to expose your gmail credentials. Port 587 is for mailgun not gmail.

Resources