Laravel 5.4邮件发送在使用config :: set门面设置配置时不起作用

时间:2019-03-11 08:34:30

标签: email laravel-5

我正在使用sendgrid smtp发送电子邮件,当我将凭据添加到.env文件中时工作正常,但是我想将凭据存储在表中并使用config::set外观进行设置并设置,我的电子邮件地址是没有发送,我也没有收到任何错误。以下是我的代码: 我呼叫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));
        }
    }

我正在配置邮件配置时的代码:

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;
}

当我使用print_r(Config::get('mail'))时,我得到如下配置

 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
                )

        )

)

这似乎是正确的,但是电子邮件发送不起作用,也没有出现任何错误

注意!如果我在.env文件中放入相同的凭据,一切都很好,并且电子邮件发送正确,这可能导致此问题,那么我们将不胜感激。

0 个答案:

没有答案