使用Laravel

时间:2019-03-29 06:47:43

标签: laravel-mail

我已经使用Laravel开发了一个saas应用程序。

有超级管理员和管理员。 每个管理员在那里管理自己的社区/仪表板。 每个社区都有自己的电子邮件设置,用于向该社区用户发送电子邮件。

如果社区无法使用自己的电子邮件地址向其用户发送电子邮件,则需要通过超级管理员电子邮件发送错误的电子邮件。 因此伪代码如下:

发送电子邮件给特定社区的用户 如果电子邮件失败,请切换电子邮件协议,然后通过“超级管理员电子邮件”设置将电子邮件发送给管理员,超级管理员和开发人员。

问题是,一旦我在CATCH逻辑中的协议之间进行切换,便总是执行TRY部分。

这是代码:

<?php

namespace App;
use Illuminate\Support\Facades\Mail;
use App\ErrorLog;
use App\Common;
use DB;
use Config;
use Artisan;

/**
 * Class SendMails, this class is to send various types of mails
 *
 * @package App
 */
class SendMails
{
    const FROM_ADDRESS = Common::FROM_EMAIL;
    const FROM_NAME = Common::SITE_TITLE;

    public static function sendMail(array $data, $view, $community_id='')
    {
        try{           

            /*Check if request comes main domain or subdomain and configure smtp.*/
            if($community_id==''){ // Main domain
                $smtp_host = 'smtp.XXX.com';
                $smtp_port = XXX;
                $smtp_email = 'x@example.com';
                $smtp_password = 'XXXXXXXXXXXXXXXX';
                $smtp_secure = 'tls';

                $from_email = 'x@example.com';
                $from_name=SendMails::FROM_NAME;
            }
            else{ // Subdomain
                $setting = Settings::where('community_id',$community_id)->first();
                if(!empty($setting)){
                    $smtp_host = $setting->smtp_host;
                    $smtp_port = $setting->smtp_port;
                    $smtp_email = $setting->smtp_email;
                    $smtp_password = $setting->smtp_password;
                    $smtp_secure = $setting->smtp_secure;

                    $from_email = $setting->smtp_email;
                    $from_name=$setting->system;
                }
            }

            if(isset($data['email'])) {
                $to_email = $data['email'];
            }
            else{
                $to_email = SendMails::FROM_ADDRESS;
            }
            if(isset($data['email_cc'])) {
                $cc_email = $data['email_cc'];
            }
            else{
                $cc_email = [];
            }
            if(isset($data['email_bcc'])) {
                $bcc_email = $data['email_bcc'];
            }
            else{
                $bcc_email = [];
            }
            if(isset($data['attachments'])) {
                $attachments = $data['attachments'];
            }
            else{
                $attachments = [];
            }
            if(isset($data['from_email'])){
                $from_email = $data['from_email'];
            }
            if(isset($data['from_name'])){
                $from_name = $data['from_name'];
            }
            if(isset($data['subject'])) {
                $subject = $data['subject'];
            }
            else{
                $subject = "Welcome To ".SendMails::FROM_NAME;
            }
            /*
            *   First update email configuration on the fly
            */
            self::updateSmtpConfiguration($smtp_host,$smtp_port,$smtp_email,$smtp_password,$smtp_secure);


            Mail::send($view, $data, function ($message) use ($to_email,$from_email,$from_name,$cc_email,$bcc_email,$attachments,$subject) {
                $message->from($from_email, $from_name);

                if(count($to_email)!=0){
                    $message->to($to_email);
                }
                if(count($cc_email)!=0){
                    $message->cc($cc_email);
                }
                if(count($bcc_email)!=0){
                    $message->bcc($bcc_email);
                }
                if(count($attachments)!=0){
                    foreach ($attachments as $attach) {
                        if($attach != ''){
                            $message->attach($attach);
                        }
                    }
                }
                $message->subject($subject);
            });

            return 'ok';
        }catch (\Exception $exception){
            self::smtpWarningEmailToAdmin($community_id);
            return $exception->getMessage();
        }
    }

    public static function smtpWarningEmailToAdmin($community_id){ // Send email to admin if community smtp failed.
        try{           

            $smtp_host = 'smtp.example.com';
            $smtp_port = XXX;
            $smtp_email = 'x@example.com';
            $smtp_password = 'XXXXXXXXXXXXXX';
            $smtp_secure = 'tls';

            $from_email = 'x@example.com';
            $from_name=SendMails::FROM_NAME;

            if($community_id !=''){ // Subdomain
                $setting = Settings::where('community_id',$community_id)->first();
                $to_email = ['x@gmail.com','user@gmail.com','another_user@gmail.com'];   
                $cc_email = [];    
                $bcc_email = [];    
                $attachments = [];
                $subject = "SMTP configuration warning";   
                $data['community_name']  = $setting->system;
                $data['bodyMessage']  = '';
                $view = 'emails.smtp_configuration_warning_email';
            }
            else{ // Main domain
                // 
            }

        /*
            *   First update email configuration on the fly
            */
            self::updateSmtpConfiguration($smtp_host,$smtp_port,$smtp_email,$smtp_password,$smtp_secure);
            Mail::send($view, $data, function ($message) use ($to_email,$from_email,$from_name,$cc_email,$bcc_email,$attachments,$subject) {
                $message->from($from_email, $from_name);

                if(count($to_email)!=0){
                    $message->to($to_email);
                }
                if(count($cc_email)!=0){
                    $message->cc($cc_email);
                }
                if(count($bcc_email)!=0){
                    $message->bcc($bcc_email);
                }
                if(count($attachments)!=0){
                    foreach ($attachments as $attach) {
                        if($attach != ''){
                            $message->attach($attach);
                        }
                    }
                }
                $message->subject($subject);
            });

            return 'ok';
        }catch (\Exception $exception){
            return $exception->getMessage();
        }
    }

    public static function updateSmtpConfiguration($smtp_host,$smtp_port,$smtp_email,$smtp_password,$smtp_secure){
        Artisan::call('cache:clear');

        /*Update smtp configuration in config/mail file*/
        Config::set('mail.port', $smtp_port); 
        Config::set('mail.host', $smtp_host); 
        Config::set('mail.username', $smtp_email); 
        Config::set('mail.password', $smtp_password); 
        Config::set('mail.encryption', $smtp_secure); 
    }


}

0 个答案:

没有答案