Send Laravel verification email with a custom template using aws ses - php

What I am trying to do.
My goal is to send a custom blade template for the built-in Laravel email verification functionality. Right now in my AuthServiceProvider inside the boot function I call VerifyEmail::toMailUsing function and attempt to create an email based on the send_email function I already have. it takes a template name, email and params, finds the blade file, and inserts the params and sends the email with aws ses.
VerifyEmail::toMailUsing(function ($notifiable, $url) {
$params = [
'first_name' => $notifiable->first_name,
'verify_url' => $url
];
$generate_email = new SendEmailController();
$generate_email->send_email('verify', $notifiable->email, $params);
});
The issue:
The email gets sent successfully but throws an error:
I understand it must have to do with the toMailUsing, but just cant point my finger to it.
Expected outcome should be: no error :)

Related

Laravel Nexmo invalid signature

I have SMS notification sending method via Nexmo client:
try {
$client = new Client(new SignatureSecret($nexmoKey, $signatureSecret, 'md5hash'));
$message = $client->message()->send(
[
'to' => $this->userNumber,
'from' => $from,
'text' => $this->notificationMessage,
'callback' => $webhookReceiveURL,
]
);
$response = $message->getResponseData();
$this->notificationId = $response['messages'][0]['message-id'];
return true;
} catch (\Exception $e) {
$this->exceptions[] = $e->getMessage();
return false;
}
If I put this piece of code in a service class where handle() method is called in Laravel's commands via crontab I get: Invalid Signature error but if I call this method from any controller method notification sends successfully. Can anyone explain why I get error if I try to send notification via crontab?
If the same code works in a controller, it sounds like configuration settings aren't getting passed into the job correctly. I would make sure that $nexmoKey and $signatureSecret look correct in the job.
On a side note, we do also offer a Laravel plugin through nexmo/laravel (https://github.com/Nexmo/nexmo-laravel/), which provides access to a Nexmo object via the Service Container and Facade layer. That may help cut down on the boilerplate for making the Nexmo object.

How to make custom email verification (Laravel 5.8)? [duplicate]

This question already has answers here:
How to customize the email verification email from Laravel 5.7?
(9 answers)
Closed 2 years ago.
I can make email verification using default template from Laravel 5.8.
Now, I want to customize the email.
Can you help me?
I tried to follow instructions from this: https://medium.com/#lordgape/custom-verification-email-notification-laravel-5-8-573ba3183ead. There is no error, but no email sent.
When I switch back to default verification, the default email is sent.
Edit:
The steps I tried so far:
Create a mailable for email verification.
Create views for new email template
override toMailUsing() using the following code in AppServiceProvider.php:
VerifyEmail::toMailUsing(function ($notifiable){
$verifyUrl = URL::temporarySignedRoute(
'verification.verify',
Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
['id' => $notifiable->getKey()]
);
// dd($notifiable);
return new EmailVerification($verifyUrl, $notifiable);
});
edit mailable, add two variable: $verifyUrl and $user.
edit __construct function:
public function __construct($url, User $user)
{
$this->user = $user;
$this->verifyUrl = $url;
}
edit build() function in mailable, add return $this->view('emails.verifyUser'); (views of custom template).
No error, "please verify your email" page is shown like usual. But no email sent.
If you want to only customize the look and the layout of the sent email and not the contents (text) of the mail you have to publish the view files of the notification and mail components.
To do so you can type:
php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-mail
These command will copy the Laravel mail message templates from the vendor folder to resources/views/vendor/notifications and resources/views/vendor/mail.
In the former path you have the email.blade.php which is basically the the MailMessage layout. (In fact you can see all the available slots you can customize with MailMessage).
In the latter path you can find the markdown and html components (that are also used in the MailMessage layout I mentioned earlier). You can take a look at the various files and modify them.
Note: the changes you makes to these templates would apply to any email message that you send, also if you create a new notification and mailable, it would still use these modified templates.
You can use this method to do global changes to the look of an email and, for example customize the header to include the logo of your company, etc.
If you just have to edit the content (texts) of an email message, you should still have to create your custom notification/mailable.
Quick tip: you can preview emails in the browser to make adjustments to the layout and texts quickly, as you don't have to fire off the notification all the time: https://medium.com/#jaouad_45834/preview-your-emails-notifications-in-browser-laravel-9058d8c856c4

Updating config on runtime for a user

I am using Laravel 5.1
I created a function to get smtp info from db for a user $mail_config=STMPDetails::where('user_id',36)->first() and then I can just call config helper function and pass the array to set config value config($mail_config). and then I call Mail::queue function.
but before it reaches createSmtpDriver#vendor/laravel/framework/src/Illuminate/Mail/TransportManager.php where it reads the configuration again to send the mail, mail config are changed to the one specified in .env file.
Another thing to note is Mail sending function is in a Listener
I am not able to figure out where can I call the function such that configuration changes are retained before mail is sent.
Thanks,
K
This should work:
// Set your new config
Config::set('mail.driver', $driver);
Config::set('mail.from', ['address' => $address, 'name' => $name]);
// Re execute the MailServiceProvider that should use your new config
(new Illuminate\Mail\MailServiceProvider(app()))->register();
Since the default MailServiceProvider is a deferred provider, you should be able to change config details before the service is actually created.
Could you show the contents of $mail_config? I'm guessing that's the problem. It should be something like
config(['mail.port' => 587]);
Update - tested in 5.1 app:
Mail::queue('emails.hello', $data, function ($mail) use ($address) {
$mail->to($address);
});
->> Sent normally to recipient.
config(['mail.driver' => 'log']);
Mail::queue('emails.hello', $data, function ($mail) use ($address) {
$mail->to($address);
});
->> Not sent; message logged.

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: Load a page and then send a mail

i am trying to load page first and then activate the $mailer to send email. because when i click on to go to next page its taking time, because its sending emails and then it is loading so,
what is the best way to do it. or any way. because i cant figure it out.
here is snippet
public function sInterest($project_id, AppMailer $mailer)
{
$project = Project::findOrFail($project_id);
if($project->investment){
$mailer->sendInterestNotificationI($user, $project);
$mailer->sendInterestNotificationD($project, $user);
$mailer->sendInterestNotificationA($project, $user);
return view('projects.offer', compact('project'));
}
}
is there a way $mailer to activate after returning a page?
In your AppMailer sendInterestNotification*() methods, replace sync email delivery with queued email delivery (see Queueing Mail in Laravel documentation)
Then page will be returned instantly, emails will be put in the corresponding queue. You will have to edit .env file to change the QUEUE driver and to start a queue listener as a separate process, detailed documentation is given on Laravel website
There is no way you can return a page to browser and then run some extra commands in your controller.

Categories