We help IT Professionals succeed at work.

Could you point a Laravel library to send an email after an action?

Eduardo Fuerte
on
Medium Priority
44 Views
Last Modified: 2020-02-20
Hi Experts


Could you point a Laravel library to send an email after an action?

After an user sends an image to be shown at a vitrine it must to be approved/ reproved by the admin and then an email must to be automatically sent to the user relating the result.

Thanks in advance
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
Hey Eduardo,

Laravel already has a Mail Library built-in (SwiftMailer). You'd need to configure that with the correct credentials (look at config/mail.php) and those will depend on how you want to actually send the email (SMTP / MailGun / sendmail etc).

Basicaly, once you've got your email drivers configured, then you create a new class, called a Mailble. You can do this easily with artisan:

php artisan make:mail ImageApproved

This creates a new mailable class in the App/Mail folder and this class contains all the info the system needs for sending that email (the from address / the content of the message etc).

One it's all up and running, you just call the to() method on the Mail facade from within your controller. You can pass in an email address, or even a user and the system will automatially get the address from that

$user = User::find($userId);
Mail::to($user)->send(new ImageApproved($some_data));

Here are the full docs -> https://laravel.com/docs/6.x/mail
David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Expanding on comment Chris made, start with a relay service like MailGun, for instant delivery.

Then as time permits, go through steps https://www.experts-exchange.com/questions/29165628/Sending-Email-by-getting-email-address-from-database.html provides, if running your own in house MTA is required.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Hi Chris


Really... the site already uses the approach you had pointed...


What I had adpted is:

Controller

use App\Mail\FotoAprovada;
use Mail;

  try {
	$vitrine->save();
	$resultado=new Resultado(false,"","Foto aprovada com sucesso.");
	
   // Email 
	 try {
		Mail::to($vitrine->email)
			->send(new FotoAprovada());
	} catch (\Exception $e){
		print ' - '.$e->getMessage().PHP_EOL;
 }

Open in new window



\app\mail\fotoaprovada\index.blade.php

Email text:

<html>
<head>
<title>Foto Aprovada</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>

</style>
</head>
<body bgcolor="#003366" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
		<td style="text-align:center;">
      <img alt="" src="https://XXXX/vitrines/a2bae276qw86e8q6q8we6q8e668qe68qe6e868qw686e2f3df.jpg"/>
 
    </td>
	</tr>
</table>
</body>
</html>

Open in new window



<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class FotoAprovada 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->view('emails.fotoaprovada.index')->subject('XXXXXXXX - Foto Aprovada');
    }
}

Open in new window

Eduardo FuerteDeveloper and Analyst

Author

Commented:
Chris

Thank you for the reply!
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
No worries Eduardo