Back | Home
الـ Path الحالي: /home/picotech/domains/instantly.picotech.app/public_html/app/EmailProvider
الملفات الموجودة في هذا الـ Path:
.
..
AmazonSmtp.php
ElasticApi.php
ElasticSmtp.php
MailGunApi.php
MailGunSmtp.php
SendEMail.php
SendGridApi.php
SendMailInterface.php
SendMailProcess.php
Sendmail.php
Smtp.php
SparkpostSmtp.php

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

<?php

namespace App\EmailProvider;


use App\Models\MessageLog;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Mailgun\Mailgun;

class MailGunApi implements SendMailInterface
{

    public function message($message){
        $this->message = $message;

        return $this;
    }

    public function from($from)
    {
        $this->from = $from;
        return $this;
    }
    public function from_name($from_name)
    {
        $this->from_name = $from_name;
        return $this;
    }
    public function reply_to($reply_to)
    {
        $this->reply_to = $reply_to;
        return $this;
    }
    public function to($to)
    {
        $this->to = $to;
        return $this;
    }

    public function subject($subject)
    {
        $this->subject = $subject;
        return $this;
    }

    public function body($body)
    {
        $this->body = $body;
        return $this;
    }

    public function files($files){
        $files  = json_decode($files);
        $files_arr = [];
        if ($files) {
            foreach ($files as $file) {
                $files_arr[] = $file;
            }
        }
        $this->files = $files_arr;
        return $this;
    }

    public function config($config){

        $this->config=$config;
        return $this;
    }

    public function process()
    {
        if($this->message){
            $this->message_log=MessageLog::updateOrCreate([
                'to'=>$this->message->to,
                'from'=>$this->message->from,
                'message_id'=>$this->message->id,
                'customer_id'=>$this->message->customer_id,
                'type'=>'sent',
            ],['status'=>'succeed','queue_id'=>$this->message->id]);
            $messageLog= $this->message_log;
        }

        try {
            $server = $this->config;
            if(isset($server) && isset($server->value)) {

                $messageLog->status='pending';
                $messageLog->save();

                $templateBody = replaceMessageBody($this->body, $this->message_log->campaign_id, $this->message_log->id);

                $config_value = json_decode($server->value);

                $mgClient = Mailgun::create($config_value->api_key);
                $domain = $config_value->domain;
                $params = array(
                    'from'    => $this->from,
                    'to'      => $this->to,
                    'subject' => $this->subject,
                    'text'    => 'Your mail does not support HTML',
                    'html'    => $templateBody
                );

                $response =  $mgClient->messages()->send($domain, $params);

//                $templateBody = replaceMessageBody($this->body, $this->message_log->campaign_id, $this->message_log->id);
//
//                $config_value = json_decode($server->value);
//                $ch = curl_init();
//                curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//                curl_setopt($ch, CURLOPT_USERPWD, "$config_value->api_key");
//                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
//                curl_setopt($ch, CURLOPT_URL,
//                    'https://api.mailgun.net/v2/samples.mailgun.org/messages');
//                curl_setopt($ch, CURLOPT_POSTFIELDS,
//                    array('from' => "$this->from",
//                        'to' => "$this->to",
//                        'subject' => "$this->subject",
//                        'text' => "$templateBody"));
//                $result = curl_exec($ch);
//                curl_close($ch);

                if ($response){
                    $messageLog->status='success';
                    $messageLog->save();
                }
            }
        } catch (\Exception $ex) {
            $messageLog->status='failed';
            $messageLog->save();
            Log::info($ex->getMessage());
        }
    }

    public function errors()
    {
        // TODO: Implement errors() method.
    }
}