Doubt Laravel Queue, Job, Mail

Asked

Viewed 224 times

1

I’m trying to get my system from a cronjob, send email to a certain list, my problem is this: when I do it through a route have no problem any emails are sent!

But when sending the email is done through a function call that runs in cronjob the email is not sent, so I tried to do as it is recommended on the web to create a Queue that will send. I managed to make What works perfectly, it reaches the function of sending email, but the email is not sent and has no error message!

Note: The email is sent if I access a get route defined by me.

Follow the code for analysis:

class Sendmailjob Mplements Shouldqueue

class SendMailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $lead;
protected $item;
protected $campaignLeadId;

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct($lead, $item, $campaignLeadId)
{
    $this->lead = $lead;
    $this->item = $item;
    $this->campaignLeadId = $campaignLeadId;
}

/**
 * Execute the job.
 *
 * @return void
 */


public function handle()
{
    $item = $this->item;
    $lead = $this->lead;
    $campaignLeadId = $this->campaignLeadId;
    Mail::queue(new DefaultMail($lead,$item,$campaignLeadId));
}

}

class Defaultmail

class DefaultMail extends Mailable
{
use Queueable, SerializesModels;
public $template;
protected $id;
public $subject;
public $lead;
public $item;
public $campaignLeadId;
/**
 * Create a new message instance.
 *
 * @return void
 */
public function __construct($lead, $item, $campaignLeadId)
{

    $this->lead = $lead ;
    $this->item = $item;
    $this->campaignLeadId =$campaignLeadId;
}

  /**
 * Build the message.
 *
 * @return $this
 */

public function build(Mailable $mailable)
{
    $url = 'https://***********.com';//env('APP_ENV')!='local'?'https://***********.com/':'http://agencia.test';
    $template = $this->item['template'];
    $template = str_replace("[[NOME]]", $this->lead['name'], $template);
    $template = str_replace("[[LINK]]", $url.'/u/'.$this->campaignLeadId, $template);
    $template = str_replace("[[LINK_OPT-OUT]]", $url.'/u/out/'.$this->campaignLeadId, $template);
    $lead = $this->lead ;
    $item = $this->item;
    $campaignLeadId =$this->campaignLeadId;

    return $this->to($lead['email'], $lead['name'])->from('***********@***********.com','Everton da Ilha do Software')
        ->html($template)->subject($item['name'])->view('template.renderMail', ['template' => $template]);
}

}

class Sendmailscron JOB call

   if(dispatch(new SendMailJob($lead, $item, $campaignLeadId))){
         DB::commit();
         $sendCount++;
   }
  • 1

    managed to solve ?

  • with the functionality developed by the Lockable no, I had to use phpmailer. I developed a function within the job with this library, ai the sending worked!

1 answer

0


After many attempts I was able to find a solution external to the Laravel, it follows the code used and the link to Phpmailer, the library I used to solve the problem!

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

function send_mail_phpmailer($lead, $item, $campaignLeadId){
    $url = 'https://**********.com';//env('APP_ENV')!='local'?'https://**********.com/':'http://**********.test';
    $template = $item['template'];

    $template = str_replace("[[NOME]]", $lead['name'], $template);
    $template = str_replace("[[LINK]]", $url.'/u/'.$campaignLeadId, $template);
    $template = str_replace("[[LINK_OPT-OUT]]", $url.'/u/out/'.$campaignLeadId, $template);

    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
    try {
        //Server settings
        $mail->SMTPDebug = 2;                                 // Enable verbose debug output
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = env('MAIL_HOST');  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;
        // Enable SMTP authentication
        $mail->Username = env('MAIL_USERNAME');                 // SMTP username
        $mail->Password = env('MAIL_PASSWORD');                           // SMTP password
        $mail->SMTPSecure = env('MAIL_ENCRYPTION');                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = env('MAIL_PORT');                                    // TCP port to connect to
        $mail->CharSet = "utf-8";


        //Recipients
        $mail->setFrom(env('MAIL_USERNAME'), env('MAIL_NAME').' da Ilha do Software');
        $mail->addAddress($lead['email'], $lead['name']);     // Add a recipient
        //Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $item['name'];
        $mail->Body    = $template;
        if($mail->send()){
            if($mail->isError()){
                return false;
            }
            return true;
        }
        return false;
    } catch (Exception $e) {
        echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
        return false;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.