Undefined index cipher on mail from console in Lumen - php

I am using Lumen ( Laravel 5.1 underneath).
I have a simple code
.
.
.
$this->info( 'sending email to: '. $user->full_name );
Mail::send( 'reminders.attendance', ['user' => $user, 'thisRelation' => $thisRelation ], function ( $m ) use
($user) {
$m->to( $user->email, $user->full_name)->subject('EduStatus Attendance Reminder');
});
I can confirm that there exists a view file. And the email is being triggered when the I put this code in the routes.php file and hit the url simply.
I am trying to make this an artisan command but when I use the same code in my command file it just says
[ErrorException]
Undefined index: cipher
When I remove the mail line, it works just fine. Whatever I print out to the console works.
Any idea?

This was a silly mistake of my own. Somehow I edited the app.php in config folder, the file did not have value for the cipher key.

Related

Changing sender name and email laravel

For each user, I change the email configuration in laravel project, when user login i use this:
\Config::set(['mail.mailers.smtp.host' => $user->smtp_mail_host]);
\Config::set(['mail.mailers.smtp.port' => $user->smtp_mail_port]);
\Config::set(['mail.mailers.smtp.username' => $user->smtp_mail_username]);
\Config::set(['mail.mailers.smtp.password' => $user->smtp_mail_password]);
\Config::set(['mail.from.address' => $user->mail_address]);
\Config::set(['mail.from.name' => $user->mail_from_name]);
when I did config() it is showing my change, but when I send, the mail is sent from the .env configuration mail address, there is a missing part I'm not finding it.
thanks for help
This is not optimal solution but can help! Pass the Configuration to your Mail::send before call send method such like this:
First import these classes in your Controller
use Mail;
use \Swift_Mailer;
use \Swift_SmtpTransport as SmtpTransport;
Then in your send function:
$from_email = \Config::get('mail.from.address');
$email_name = \Config::get('mail.from.name');
$to_email = "test#test.com";
$transport = (new SmtpTransport(\Config::get('mail.mailers.smtp.host'), \Config::get('mail.mailers.smtp.port'), null))->setUsername(\Config::get('mail.mailers.smtp.username'))->setPassword(\Config::get('mail.mailers.smtp.password'));
$mailer = new Swift_Mailer($transport);
Mail::setSwiftMailer($mailer);
Mail::send('emails.emailhtmlpage', [], function ($m) use ($from_email,$email_name,$to_email) {
$m->from($from_email, $email_name);
$m->to($to_email, 'TEST')->subject("Your Email Subject");
});
The settings applied using Config::set do not persist across different requests: if you only run them when the user logins, the changes with not apply to the following requests.
You should modify your Authenticate middleware adding:
public function handle($request, Closure $next, ...$guards)
{
$authReturned = parent::handle($request, $next, $guards);
$user = Auth::user();
\Config::set(['mail.mailers.smtp.host' => $user->smtp_mail_host]);
\Config::set(['mail.mailers.smtp.port' => $user->smtp_mail_port]);
\Config::set(['mail.mailers.smtp.username' => $user->smtp_mail_username]);
\Config::set(['mail.mailers.smtp.password' => $user->smtp_mail_password]);
\Config::set(['mail.from.address' => $user->mail_address]);
\Config::set(['mail.from.name' => $user->mail_from_name]);
return $authReturned;
}
if you using Queue and Jobs make sure to
stop the php artisan queue:work first (its seems to have it own cache where it store the configs)
check background process to make sure queue:work already stop (for linux you can use top -c -p $(pgrep -d',' -f php) and search for php artisan queue:work in COMMAND column )
do the change you want in .env or configs
config:clear
redo php artisan queue:work

Artisan::call command package throws error exception will calling through web page

I'm using docker for running a Laravel project and i have this command in Laravel that writes into a file in storage folder
Artisan::call(
'app:cache',
[
"--message" => 'this is a command',
"--seconds" => 0
]
)
when i call it through web like
Route::get('/', function () {
\Artisan::call(
'app:cache',
[
"--message" => 'this is a command',
"--seconds" => 0
]
);
});
an exception from /src/vendor/symfony/console/Input/ArrayInput.php file is generated with this message: "Trying to access array offset on value of type int"
but in command line this command is working completely OK.
the issue was coming from my using version of PHP, the PHP version was 7.4.1 and the package that Laravel was using for this version of PHP was changed and that caused the error to happen, I changed my using PHP version to 7.2 and got worked.
I have been given the code but normally, programmers should not use this kind of packages this way, for example in this one, WEB should not call Artisan commands directly, because if a situation like this happens you will need to override your code like everywhere. Instead, use your own code and put what you want behind it, as an example:
imaging you have this artisan command that writes some information in a file and you want to use it in your WEB section then you must put your code into a class or function outside of your artisan command and use your class/function in your artisan and web for work. then code like bellow
Route::get('/', function () {
\Artisan::call(
'app:cache',
[
"--message" => 'this is a command',
"--seconds" => 0
]
);
});
will become to code like
Route::get('/', function () {
$cache = new \App\Custom\Cache(
$message = 'this is a command',
$seconds = 0
);
});
In this way, your functioning code is separated from your framework and if you even use some packages in your class/function and a package needs to change like it was to call Artisan command then there is just one place to update, not multiple places.
Try calling it like this:
Artisan::call('app:cache --message="this is a command" --seconds=0');
and if you want to put dynamic variables in it:
Artisan::call('app:cache --message=' . $yourMessage . '--seconds=' . $seconds);
You will not have to handle any arrays by passing your variables in a single string line like this. It still makes it simple to read as a single string.

Recover password mail dosen't send to user laravel

the password recovery email in laravel is not sent to the user
and this is my function in controller :
public function recover(Request $request)
{
$validator = Validator::make($request->only('email'), [
'email' => 'required'
]);
if($validator->fails()) {
throw new ValidationHttpException($validator->errors()->all());
}
$response = Password::sendResetLink($request->only('email'), function (Message $message) {
$message->subject(Config::get('boilerplate.recovery_email_subject'));
});
switch ($response) {
case Password::RESET_LINK_SENT:
// return $this->response->noContent();
return response()->json(['success' => true, 'data' => $response], 200);
case Password::INVALID_USER:
// return $this->response->errorNotFound();
return response()->json(['success' => false, 'data' => $response], 200);
}
}
and I configure my .env and mail.php
I use laravel 5.6
if you are using gmail address to sent mail. then you need to on less secure app.
go to https://myaccount.google.com/lesssecureapps
then allow by turning it on.
and also use
php artisan config:clear
sometimes google blocks when you try to send email through some code. In that case you got a alert mail. click on that mail(Check Activity) and mark as yes (Do you recognize this activity?)
Or you can try
MAIL_DRIVER=sendmail
and also use
php artisan config:clear
First of all, try to locate the problem.
I would suggest to try to setup MAIL_DRIVER=log and check laravel.log after executing this function. If nothing is found in the log - then you're not trying to send it. Most common problem, in this case, is using queues, so, check the QUEUE_CONNECTIONvariable in .env, it should be equal to "sync" (or set up your driver, like Redis if required).
By using "log" driver you should see your message in the log. If it works fine with "log" and doesn't work with smtp then you should get an error about it. If an error is present - please post it. If not - say so as well.
P.S. please note that if you're using php artisan serve, all variables from .env are updates only after restarting the php server.

Laravel sending email

I am trying to send an email from my application that uses the Laravel framework. I have installed Sximo as a theme for working with Laravel and the application is running on Amazon AWS. All seems to be good with the setup as it is working as intended.
I have a API setup on *C:\xampp\htdocs\public\api\savequestionnaire.php * that receives data from a mobile application and saves the data to a MySQL table. This also is working as intended.
The idea is that if the API receives some data, an email is generated to a predefined email address. With that, I have a file for receiving the data, writing it to a database and then generating the email as below.
For the Mail section I am following the example on the Laravel website from HERE
savequestionnaire.php
<?php
$json = file_get_contents('php://input');
$questionnairevalues = json_decode($json, true);
$position = $questionnairevalues['position'];
$question_one = $questionnairevalues['question_one'];
$question_two = $questionnairevalues['question_two'];
$question_three = $questionnairevalues['question_three'];
$question_four = $questionnairevalues['question_four'];
$question_five = $questionnairevalues['question_five'];
$query = "INSERT INTO tb_q (position, question_one, question_two, question_three, question_four, question_five) "
. "VALUES('$position', '$question_one', '$question_two', '$question_three', '$question_four', '$question_five')";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
if ($result) {
echo 'Success';
// Getting error here
Mail::send('emails.contact.blade', $position, function ($message) {
$message->from('myEmail#test.com', 'Laravel');
$message->to('myOtherEmail#test.com');
});
echo '\r\nMail sent success';
}
else {
echo 'Error';
}
mysqli_close($mysqli);
When I run the above code I get the error: "Success
Fatal error: Class 'Mail' not found in C:\xampp\htdocs\public\api\savequestionnaire.php on line 56"
I have also tried adding
use Mail;
to the top of the file but then I get the error: "Warning: The use statement with non-compound name 'Mail' has no effect in C:\xampp\htdocs\public\api\savequestionnaire.php on line 2"
How do I go about implementing the Mail feature correctly using Laravel?
I have also tried using PHP's built in
mail('myEmail#test.com', 'My Subject', 'My message');
function. This generates no errors - but no emails are sent or received.
add this at top of controller:
use Illuminate\Support\Facades\Mail;
In your case directly use :
\Illuminate\Support\Facades\Mail::send('emails.contact.blade', $position, function ($message) {
instead of Mail::send('emails.contact.blade', $position, function ($message) {
In Your Config / app.php Check whether there is
Illuminate\Mail\MailServiceProvider::class,
under providers. And Check Under Aliases you have
'Mail' => Illuminate\Support\Facades\Mail::class,
Edit :
According to the Error, it is looking the facade inside the questionnaire.php you are now in. So try putting this code inside of a controller.
To create a controller, in the terminal type,
php artisan make:controller controllerName
If this page is standalone, you have to use composer to be able to use Mail:
add
require 'vendor/autoload.php';
at the top of your page (replace the path for matching the location of your vendor/autoload.php file

Laravel Queue using Beanstalkd Failing

I've tried to using queue for everytime user register and send an email to them to verify.
I'm doing it successfully using Laravel 5.1
I just wandering how can I can stop current queue if I got an error and then when I fix it I restart the job from the last queue?.
How about the error like this:
[InvalidArgumentException]
View [emails.versify_email] not found.
[InvalidArgumentException]
View [emails.versify_email] not found.
[InvalidArgumentException]
View [emails.versify_email] not found.
I've tried at homestead using:
public function failed(){
//I've tried send email but it not sending
}
or at AppServiceProvider
Queue::failing(function ($connection, $job, $data) {
$user ='mymail#gmail.com';
Mail::send('emails.fail_queue', ['user' => $user], function ($m) use ($user) {
$m->subject('Failing:' . $user)
->to($user);
});
});
None of the them is working.
what should i do if its like that when happened at production?.
When an exception is thrown from the handler Laravel tries to release the job back to the queue unless it is explicitly deleted.
https://github.com/laravel/framework/blob/5.1/src/Illuminate/Queue/Worker.php#L198-L227
You can use the $delay parameter to put back in the queue with delay. Or better just bury the job yourself, if you are able to detect the issue.
$this->job->bury()

Categories