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

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

Related

Send Laravel verification email with a custom template using aws ses

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 :)

Sending bulk emails using different credentials

I need to send hundreds of emails using different credentials from laravel.
Each customer of mine has his/hers mail list and needs to provide their own SMTP server. I process that list and send emails on customer's behalf.
This is what I have so far. It is working, but it is very slow and I don't have many emails so far. I see a problem when I get more emails.
Any suggestions on how to improve?
PS- I use cron Console Command and use Kernel to schedule the job.
public function sendMailings($allMailings) {
foreach ($allMailings as $email) {
Config::set('mail.host', $email['smtpServer']);
Config::set('mail.port', $email['smtpPort']);
Config::set('mail.username', $email['smtpUser']);
Config::set('mail.password', $email['smtpPassword']);
Config::set('mail.encryption', $email['smtpProtocol']);
Config::set('mail.frommmail', trim($email['fromEmail']));
Config::set('mail.fromuser', trim($email['fromUser']));
Config::set('mail.subject', trim($email['subject']));
Config::set('mail.toEmail', trim($email['toEmail']));
Config::set('mail.toName', trim($email['toName']));
Config::set('mail.pretend', false);
$email_body = $email['emailBody'];
Mail::send('emails.availability, compact('email_body')
, function($message) {
$message->from(config('mail.username'), config('mail.fromUser'));
$message->replyTo(config('mail.frommmail'), config('mail.fromUser'));
$message->to(config('mail.toEmail'), config('mail.toName'))->subject(config('mail.subject'));
});
Log::info('Mail was sent');
}
}
You can not change email provider configs on-the-fly, so you must make new instance of mailer in service container. I did it before, i wrote a method in my own class to get new mailer instance:
/**
* #return Mailer
*/
protected function getMailer()
{
// Changing mailer configuration
config(['mail.driver' => static::getName()]);
// Register new instance of mailer on-the-fly
(new MailServiceProvider($this->container))->register();
// Get mailer instance from service container
return $this->container->make('mailer');
}
Sending e-mail messages directly in web app can drastically slow down the responsiveness of your application. You should always queue your messages.
Instead of Mail::send You can use Mail::queue
and then from cron or manually call
php artisan queue:work
That will process the next item on the queue. This command will do nothing if the queue is empty. But if there’s an item on the queue it will fetch the item and attempt to execute it.

Laravel 5: Signing e-mail with DKIM

In my project I'm trying to send e-mail through my own SMTP server using Laravel 5. I have everything set up correctly using the SMTP driver and I'm managing to send and receive e-mail fine.
I want to get set up using DKIM. I've set up my public key and made it available in my DNS and I have a private key ready to start signing my messages.
However, I can't find any documentation on how to set up DKIM signing using Laravel/Swift Mailer. I've managed to sign my e-mails with DKIM before in another non-Laravel project that used PHPMailer but can't find any way of doing it here. I've had a browse through Illuminate\Mail\Message and Illuminte\Mail\Mailer but can't find anything relevant.
Does anyone know how to do this?
Current example code:
public function handle(UserWasRegistered $event)
{
$user = $event->getUser();
$this->mailer->send(['emails.users.welcome.html', 'emails.users.welcome.text'], ['user' => $user], function($message) use($user) {
$message->subject('Welcome to XXXXXX');
$message->to($user->email);
});
}
Ideally, I would like to be able to provide my DKIM private key in the config somewhere and have Laravel/Swift Mailer (Or write some code once) sign my messages for me.
Cheers
I wrote decorator of MailServiceProvider for Laravel 5 that provides ability to sign outgoing messages with DKIM:
https://github.com/vitalybaev/laravel5-dkim
You have to extend Laravel Mailer and mail service provider. Default laravel mailer using new Swift_Message class not Swift_SignedMessage thats why you don't have options for signing.
Here is a package for laravel 4.

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.

Cakephp email official example not working?

I am new to Cakephp (and I have asked a few question on Cakephp here recently) and I am learning by trying out the example on the official site. The following is the link.
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
I added the to my Posts controller which I created before following the official example code.
public function emailTesting(){
$Email = new CakeEmail();
$Email->config('default');
$Email->from(array('me#example.com' => 'My Site'));
$Email->to('myWorkingEmail#gmail.com');
$Email->subject('Testing Subject');
$Email->send('Testing email content');
}
According to the configuration section,
It is not required to create app/Config/email.php, CakeEmail can be used without it and use respective methods to set all configurations separately or load an array of configs.
So, I think the above code should work even if I don't create app/Config/email.php. Then,when I try to open the page
http://localhost/cakephpTesting/emailTesting/
It gives a blank page with no error message (the debug is already 2, which should shows all kind of error message if there is any error) but I didn't receive any email. I also tried different email service provider like Yahoo and my school email. All of them fails, so I think it should be the problem of my code?
So, anybody got any idea on what have I missed?
As mentioned in your question, it is not required to create a app/Config/email.php, IF you use respective methods to set all configurations separately or load an array of configs.
You are using Email::config() method to load a $default config array which is supposed to be defined somewhere. As good practise, it can be defined in the app/Config/email.php or define it in your controller's action (don't recommend the latter). CakeEmail needs to know the transport type, host, port, etc.
Read the section after the line that you have mentioned in your question which tells you how to either use the constructor of CakeEmail or use the $config array.

Categories