lundi 11 mars 2019

Laravel 5.4 Mail sending not working when set configuration using config::set facade

I'm sending email using sendgrid smtp It's work fine when i add my credential in the .env file, but i want to store my credential in a table and load and set it using config::set facade my email is not sending and also i'm not getting any error. following is my code: code where i'm calling the Mail::to and send function

$campaign = Campaign::where('id', $id)->first();
    if ($campaign) {
        $campaign->status = self::START_STATUS;
        $campaign->save();
        $list = CampaignList::where('id', $campaign->campaign_list_id)->first();
        $template = Template::where('id', $campaign->template_id)->first();
        foreach ($list->campaignListMember as $contact) {
            $campaignTracking = new CampaignTracking();
            $campaignTracking->campaign_id = $campaign->id;
            $campaignTracking->company_contact_id = $contact->companyContact->id;
            $campaignTracking->email_status = self::PENDING_STATUS;
            $campaignTracking->reason = '';
            $campaignTracking->save();
            Mail::to($contact->companyContact->email)->send(new SendBulkEmails($template, $contact->companyContact,$campaign->id,$this->global->id));
        }
    }

code wher i'm configuring mail configuration:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\CampaignSmtpSetting;
use Illuminate\Mail\MailServiceProvider;
use Illuminate\Support\Facades\Config;

class SendBulkEmails extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;

public $data;
public $contact;
public $campaignId;

public function __construct($data,$contact,$campaignId,$organisationId)
{

    $this->data = $data;
    $this->contact = $contact;
    $this->campaignId = $campaignId;
    $smtpSetting = CampaignSmtpSetting::where('organisation_setting_id',$organisationId)->first();

    Config::set('mail.driver', $smtpSetting->mail_driver);
    Config::set('mail.host',   $smtpSetting->mail_host);
    Config::set('mail.port',  $smtpSetting->mail_port);
    Config::set('mail.username', $smtpSetting->mail_user_name);
    Config::set('mail.password', $smtpSetting->mail_password);
    Config::set('mail.encryption', $smtpSetting->mail_encryption);
    Config::set('mail.from.name', $smtpSetting->mail_from_name);
    Config::set('mail.from.address', $smtpSetting->mail_from_email);

    (new MailServiceProvider(app()))->register();
 //        print_r(Config::get('mail'));exit;
}


public function build()
{

     $this->data->body  = str_replace('{FIRSTNAME}',$this->contact->first_name,$this->data->body);
     $this->data->body  = str_replace('{LASTNAME}',$this->contact->last_name,$this->data->body);
     $this->data->body  = str_replace('{EMAIL}',$this->contact->email,$this->data->body);

     $path  = public_path() . '/user-uploads/template-attachments/';
     $email = $this->view('admin.send-bulk-emails.email',['body' => $this->data->body])
        ->subject($this->data->subject);
     foreach ($this->data->attachments as $attachment){
         $path .= $attachment->file_name;
         $email->attach($path);
     }
     if (!$this->data->cc == ''){
     $email->cc($this->data->cc);
     }
     if (!$this->data->bcc == ''){
         $email->bcc($this->data->bcc);
     }
    return $email;
}

when i use print_r(Config::get('mail')) i get the configuration like below

 Array
   (
        [driver] => smtp
        [host] => smtp.sendgrid.net
        [port] => 587
        [from] => Array
        (
            [address] => hidayat.ullah@piecyfer.com
            [name] => Hidayat
        )

    [encryption] => tls
    [username] => hidayat3676
    [password] => password
    [sendmail] => /usr/sbin/sendmail -bs
    [markdown] => Array
        (
            [theme] => default
            [paths] => Array
                (
                    [0] => E:\xampp\htdocs\hrm\resources\views/vendor/mail
                )

        )

    [stream] => Array
        (
            [ssl] => Array
                (
                    [verify_peer] => 
                    [verify_peer_name] => 
                    [allow_self_signed] => 1
                )

            [tls] => Array
                (
                    [verify_peer] => 
                    [verify_peer_name] => 
                    [allow_self_signed] => 1
                )

        )

)

which seem to be correct, but email sending is not working nor i get any error

Note! if i put the same credential in .env file everything is fine and email sending correctly what could be causing this issue any help is extremely appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire