Laravel 5.1 mail not sending and no error reported - php

Below are my mail code and configuration.
The issue which I'm facing is that mail() is not working, but I'm getting the output as mail sent (from code,when 'pretend' => false,) and not sent ('pretend' => true, and I also get "server.INFO: Pretending to mail message to:soandso#mail.com " in the log file ).What could be the issue? Same code is working for another site hosted with the same provider( GoDaddy)
$mail= Mail::send($view, $data, function ($message) use ($email, $subject, $data) {
if (isset($data['senderEmail'])) {
$message->from($data['senderEmail'], $data['senderFullname']);
}else{
$defult=\DB::table('settings')->select('value')->where('option','=','defaultMail')->first();
$message->from($defult->value, 'mailFrom');
}
if (isset($data['attachment'])) {
$message->attach($data['attachment']);
}
$message->to($email)->subject($subject);
});
if($mail){
dd("mail sent");
}else{
dd("not sent");
}
.env file
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=XXXXXXXX#gmail.com
MAIL_PASSWORD="XXXXXXXX"
MAIL_ENCRYPTION=tls
SENDERS_EMAIL=XXXXXXXX#gmail.com
SENDERS_NAME=afsal
config/mail.php
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,

Related

mail is not sending yahoo small business

I am trying to send email using Laravel via yahoo small business but I am getting This error please help me.
This is what I am getting an error(Image link)
My .env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.bizmail.yahoo.com
MAIL_PORT=465
MAIL_USERNAME=****#yahoo.com
MAIL_PASSWORD=*******
MAIL_ENCRYPTION=null
My Send email Code
public function index()
{
$this->sendMail('mails.subscribe', '******#****.**', '******#**.**',
'Testing', 'Test');
}
public function sendMail($bladeName, $from, $to, $subject, $body)
{
Mail::send(['html' => $bladeName], ['user' => $body], function
($message) use ($to, $subject, $from) {
$message->to($to)->subject($subject);
$message->from($from, 'Test');
});
}
Try to change the smtp port to : 587
Sorry for my other answer I overlooked your code.
your mail function is wrong that's why you are not connecting
try :
Mail::send('emails.welcome', ['key' => 'value'], function($message)
{
$message->to('foo#example.com', 'John Smith')->subject('Welcome!');
});
One more :
Mail::send('emails.welcome', $data, function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
$message->attach($pathToFile);
});

Laravel Notifications not sending

I set up a contact form which sends an email on completion using Laravel notifications, however it doesn't look like anything is being sent.
ROUTES
Route::post('/contact', 'ContactController#store');
CONTROLLER
public function store()
{
request()->validate([
'name' => 'required|max:255',
'email' => 'required|email|unique:contacts|max:255',
'message' => 'required|max:2000',
]);
$contact = Contact::create(
request()->only([
'name',
'email',
'message',
])
);
User::first()->notify(new SendContactNotification($contact));
return back()->with('success', 'Thank you, I will be in touch as soon as I can');
}
NOTIFICATION
protected $contact;
public function __construct($contact)
{
$this->contact = $contact;
}
public function toMail($notifiable)
{
return (new MailMessage)
->line($this->contact->name)
->line($this->contact->email)
->line($this->contact->message);
}
I do get the success message when I run it. However, nothing appears in my Mailtrap. Here's the mail section of the sanitised .env file:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=username
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS='admin#test.com'
MAIL_FROM_NAME='admin'
I can't see what I have done wrong. I also tried type hinting the contact in the notification like so:
public function __construct(Contact $contact)
{
$this->contact = $contact;
}
That didn't work unfortunately. I also thought it might be something to do with my computer not being set up to send emails using php, but I was under the impression that the env file would take care of that.
The contacts are being stored in the database ok, but no emails are being sent. Would anyone be able to help?
It was the port in the env file, I changed it to:
MAIL_PORT=465
and it worked!
I knew port 2525 wasn't working because of this answer: https://stackoverflow.com/a/45418259/5497241

Sending mail with lumen | Timeout

I have required composer require illuminate/mail,
.env looks fine :
MAIL_DRIVER=smtp
MAIL_HOST=smtp-mail.outlook.com
MAIL_PORT=587
MAIL_USERNAME=******#hotmail.com
MAIL_PASSWORD=*****
MAIL_ENCRYPTION=tls
Controller :
use Illuminate\Support\Facades\Mail;
....
....
....
$random = str_random(40);
Mail::send('email.verify', ['user' => $applicant_id, 'random'=>$random], function ($m) use ($findUser) {
$m->to($findUser->user->email)->subject('Account Verification');
});
It always gives me "message": "Maximum execution time of 30 seconds exceeded",, I tried it with zoho mail also.

What is the best way to sent out email in Laravel 5?

I have a problem sending out email in my Laravel 5 Application.
Here is my mail function
email = test#gmail.com
Mail::send('emails.activation', array(
'username'=>$user->username,
'name'=>$user->name,
'code'=>$user->code,
'email'=>$user->email
),
function($message){
$message->from(env('MAIL_USERNAME'),'Site');
$message->to('email', 'name' )->subject('Site Activation ');
});
I keep getting
Update
Mail Configuration in .env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=donotreply#site.com
MAIL_PASSWORD=***********
New Error
I'm very curios on what is going on.
What did I do wrong ?
This means there is something wrong in email address fields such as From, Sender or Reply-To fields.
Swift Mailer strictly follow RFC standard to avoid emails being caught by spam checker tools.
test#gmail.com doesn't look like a normal email address or does not even exist.
Try with a different email address example send to me
Mail::send('emails.activation', array(
'username'=>$user->username,
'name'=>$user->name,
'code'=>$user->code,
'email'=>$user->email
),
function($message){
$message->from('siteEmailaddress#domain.com'),'Site');
$message->to('avalidEmailaddress#domain.com', 'name' )->subject('Site Activation ');
});
Also if you wish to use gmail as SMTP server below:
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'your-email#gmail.com',
'password' => 'your-password',
EDITED
NB: MAIL_USERNAME must be your #gmail.com email address e.g. myname#gmail.com else you will get Connection could not be established with host smtp.gmail.com error
To handle this exception if you can't resolve it, go to open app\Exceptions\handler.php
add this inside render method:
/**
* Render an exception into an HTTP response.
*
* #param \Illuminate\Http\Request $request
* #param \Exception $e
* #return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($e instanceof Swift_RfcComplianceException){
//redirect to form
//You can also delete the user account here if already created or do other stuffs
return redirect($request->fullUrl())->with('error',"We have issue sending you an email");
}
.......
}
NB: Remember to add use Swift_RfcComplianceException; at the top of handler.php
It looks like the 'from' address is not valid, you're using an env call currently without knowing 100% what it returns.
Try changing the from address to a real email address for testing purposes.

How to handle callback when mail is sent in laravel 4

I wonder how to handle the callback when mail is sent? I want to do some action when the mail is sent. I know the code below is wrong, let's say $callbackOnSend is called before the mail is send and $callbackAfterSent is called after the mail is sent.
Mail::send(array('text' => 'view'), $data, $callbackOnSend, $callbackAfterSent);
I wonder how can I achieve the $callbackAfterSent?
If the mail is sent, the Mail::send() method will return true.
$mailVariables = ['from' => 'abc#abc.com'. 'fromName' => 'abc', 'to' => '123#123.com', 'cc' => 'xyz#xyz.com', 'attachment' => 'file1'];
Mail::send('emails.welcome', $data, function($message) use ($mailVariables)
{
$message->from($mailVariables['from'], $mailVariables['fromName']);
$message->to($mailVariables['to'])->cc($mailVariables['cc']);
$message->attach($mailVariables['attachment']);
});
If you need to do something if/after the mail is sent, you can wrap the whole Mail::send() in an if statement, however, from what I've read, it won't work if you have 'pretent' set to true in your config as with 'pretend' set to true, mail::send() will always return false.
if(Mail::send('emails.welcome', $data, function($message) use ($mailVariables)
{
$message->from($mailVariables['from'], $mailVariables['fromName']);
$message->to($mailVariables['to'])->cc($mailVariables['cc']);
$message->attach($mailVariables['attachment']);
})) {
// do something
}

Categories