Back | Home
الـ Path الحالي: /home/picotech/domains/instantly.picotech.app/public_html/public/uploads/./../../././app/Listeners
الملفات الموجودة في هذا الـ Path:
.
..
MailEventListener.php
SendMailListener.php

مشاهدة ملف: SendMailListener.php

<?php

namespace App\Listeners;

use App\Events\SendEMail;
use App\Models\EmailQueue;
use App\Models\MessageLog;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Message;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Crypto\DkimSigner;
use Symfony\Component\Mime\Email;

class SendMailListener implements ShouldQueue
{
    use InteractsWithQueue;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param SendEMail $event
     * @return void
     */
    public function handle(SendEMail $event)
    {
        try {
            $config_value=json_decode($event->config->value);
            $getTransport="get_transport_".strtolower($event->from);
            if(method_exists($this,$getTransport)){
                $transport=$this->$getTransport($config_value);
                $mailer = new Mailer($transport);
                $email = (new Email())
                    ->from(new Address($event->from_email,$event->from_name))
                    ->to($event->to)
                    ->replyTo($event->reply_to)
                    ->priority(Email::PRIORITY_NORMAL)
                    ->subject($event->subject)
                    // ->text('This is an important message!')
                    ->html($event->body);
                if(isset($config_value->dkim_private_key) && isset($config_value->dkim_domain) && isset($config_value->dkim_selector)){
                    $signer = new DkimSigner($config_value->dkim_private_key, $config_value->dkim_domain, $config_value->dkim_selector);
                    $email = $signer->sign($email);
                }
                $mailer->send($email);
            }else{
                throw new \Exception('Sending server not supported right now');
            }
        } catch (\Exception $ex) {
            if ($event->email_queue_id) {
                $queue=EmailQueue::where('id',$event->email_queue_id)->first();
                $queue->update(['status'=>'failed']);
                MessageLog::where('random_ref_key', $queue->random_ref_key)->update(['is_bounced' => 'yes','status'=>'failed','queue_id'=>$queue->id]);
            }
            throw new \Exception($ex->getMessage());
        }
    }

    /* Add new email provider
    using function get_transport_{new_provider}
    should be added in helpers->available_servers & servers_fields */

    private function get_transport_smtp($config_value):TransportInterface{
        return Transport::fromDsn("smtp://".urlencode($config_value->username).":".urlencode($config_value->password)."@$config_value->host:$config_value->port?verify_peer=false");
    }
    private function get_transport_sendmail($config_value):TransportInterface{
        return Transport::fromDsn("sendmail://default");
    }
    private function get_transport_amazon_smtp($config_value):TransportInterface{
        return Transport::fromDsn("ses+smtp://".urlencode($config_value->username).":".urlencode($config_value->password)."@default");
    }
    private function get_transport_amazon_api($config_value):TransportInterface{
        return Transport::fromDsn("ses+api://".urlencode($config_value->access_key).":".urlencode($config_value->secret_key)."@default");
    }
    private function get_transport_google_gmail_smtp($config_value):TransportInterface{
        return Transport::fromDsn("gmail+smtp://".urlencode($config_value->username).":".urlencode($config_value->password)."@default");
    }
    private function get_transport_mailchimp_mandrill_smtp($config_value):TransportInterface{
        return Transport::fromDsn("mandrill+smtp://".urlencode($config_value->username).":".urlencode($config_value->password)."@default");
    }
    private function get_transport_mailchimp_mandrill_api($config_value):TransportInterface{
        return Transport::fromDsn("mandrill+api://".urlencode($config_value->api_key)."@default");
    }
    private function get_transport_mailgun_smtp($config_value):TransportInterface{
        return Transport::fromDsn("mailgun+smtp://".urlencode($config_value->username).":".urlencode($config_value->password)."@default");
    }
    private function get_transport_mailgun_api($config_value):TransportInterface{
        return Transport::fromDsn("mailgun+api://".urlencode($config_value->api_key).":".urlencode($config_value->domain)."@default");
    }
    private function get_transport_mailjet_smtp($config_value):TransportInterface{
        return Transport::fromDsn("mailjet+smtp://".urlencode($config_value->access_key).":".urlencode($config_value->secret_key)."@default");
    }
    private function get_transport_mailjet_api($config_value):TransportInterface{
        return Transport::fromDsn("mailjet+api://".urlencode($config_value->access_key).":".urlencode($config_value->secret_key)."@default");
    }
    private function get_transport_postmark_smtp($config_value):TransportInterface{
        return Transport::fromDsn("postmark+smtp://".urlencode($config_value->postmark_id)."@default");
    }
    private function get_transport_postmark_api($config_value):TransportInterface{
        return Transport::fromDsn("postmark+api://".urlencode($config_value->api_key)."@default");
    }
    private function get_transport_sendgrid_smtp($config_value):TransportInterface{
        return Transport::fromDsn("sendgrid+smtp://".urlencode($config_value->api_key)."@default");
    }
    private function get_transport_sendgrid_api($config_value):TransportInterface{
        return Transport::fromDsn("	sendgrid+api://".urlencode($config_value->api_key)."@default");
    }
    private function get_transport_sendinblue_smtp($config_value):TransportInterface{
        return Transport::fromDsn("	sendinblue+smtp://".urlencode($config_value->username).":".urlencode($config_value->password)."@default");
    }
    private function get_transport_sendinblue_api($config_value):TransportInterface{
        return Transport::fromDsn("sendinblue+api://".urlencode($config_value->api_key)."@default");
    }
    private function get_transport_ohmysmtp_smtp($config_value):TransportInterface{
        return Transport::fromDsn("ohmysmtp+smtp://".urlencode($config_value->api_token)."@default");
    }
    private function get_transport_ohmysmtp_api($config_value):TransportInterface{
        return Transport::fromDsn("	ohmysmtp+api://".urlencode($config_value->api_token)."@default");
    }

    public $tries = 2;

}