Emails.php 5.35 KB
<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "w_emails".
 *
 * @property integer $id
 * @property string $code
 * @property string $name
 * @property string $value
 * @property string $to_email
 * @property string $from_email
 * @property string $from_name
 * @property integer $done
 * @property string $who_comment
 */
class Emails extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'w_emails';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['code', 'name', 'value'], 'required'],
            [['value'], 'string'],
            [['done'], 'integer'],
            [['code', 'name'], 'string', 'max' => 254],
            [['to_email', 'from_email', 'from_name'], 'string', 'max' => 200],
            [['who_comment'], 'string', 'max' => 50],
            [['code'], 'unique']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'code' => 'Code',
            'name' => 'Name',
            'value' => 'Value',
            'to_email' => 'To Email',
            'from_email' => 'From Email',
            'from_name' => 'From Name',
            'done' => 'Done',
            'who_comment' => 'Who Comment',
        ];
    }


    public static function header() {


        $office = Offices::findOne(1);


        $phones = explode(";",$office['phones']);

        $output = "<div style='width:100%; border-bottom: 2px solid #7389b4; font-family: Helvetica-Light;'>
				<div style='float:left'>
					<img src='http://".$_SERVER['HTTP_HOST']."/storage/media/files/settings/inside-placeholder-logo2.png' />
				</div>
				<div style='float:right'>
					<div>
						<div style='width:220px;'>";
        foreach ($phones as &$el) {
            $output .= "
				<div style='float:left; height: 7px;'>
					<img src='http://".$_SERVER['HTTP_HOST']."/storage/media-templates/img/phone.png' />
				</div>
				<div style='text-align:right; color:#7389b4'>
					".$el."
				</div>
				<div style='clear:both'>&nbsp;</div>";
        }
        $output .= "				<div style='float:left; height: 7px;'>
								<img src='http://".$_SERVER['HTTP_HOST']."/storage/media-templates/img/address.png' />
							</div>
							<div style='text-align:right'>".$office['address']."</div>
							<div style='clear:both'>&nbsp;</div>
							<div style='float:left; height: 7px;'>
								<img src='http://".$_SERVER['HTTP_HOST']."/storage/media-templates/img/email.png' />
							</div>
							<div style='text-align:right'>".$office['email']."</div>
							<div style='clear:both'>&nbsp;</div>
						</div>
					</div>
					<div style='clear:both'>&nbsp;</div>
				</div>
				<div style='clear:both'>&nbsp;</div>
			</div>";

        return $output;
    }

    public static function get($code,$vars,$to = '',$from = '',$from_name = '',$captcha=false)
    {
        $model = new Emails();
        $data = $model->findOne(['code'=>$code]);

        //$letter = $data['value'];
        $subject = $data['name'];
        if ($data['value'] == '') {
            return false;
        }
        if ($from == '') {
            $from = $data['from_email'];
        }
        if ($from_name == '') {
            $from_name = $data['from_name'];
        }
        /*if ($to == '') {
            $to = $data['to_email'];
        }*/
        if ($data['to_email'] != '') {
            $to = $data['to_email'];
        }

        $letter = Emails::header().$data['value'];


        if ($code == 'cart_client') {
            $subject = "Заказ №".$vars['order_numer']." - ".date("d.m.Y").", ".date("H:i");
        }
        else if ($code == 'cart_manager') {
            $subject = $vars['name']." (№".$vars['order_numer'].") - ".date("d.m.Y").", ".date("H:i");
        }

        foreach ($vars as $kk=>$vv) {
            $str = '{'.$kk.'}';
            $letter = str_replace($str,$vv,$letter);
        }

        $letter = str_replace(array("../../../"),"http://".$_SERVER['SERVER_NAME']."/",$letter);

        $mail = new \PHPMailer();
        $mail->CharSet = "UTF-8";
        $mail->From     = $from;
        $mail->FromName =$from_name;
        $mail->Subject  =$subject;
        $mail->MsgHTML($letter);
        $fp = fopen("Emails_log.txt","w");
        fwrite($fp, $letter);
        fclose($fp);
        $mail->AddAddress($to);
//        $splitEmails = explode(",",$to);
//        if (count($splitEmails)>1){
//            foreach ($splitEmails as $sEmail){
//                $mail->AddAddress($sEmail);
//
//            }
//
//        } else {
//            $mail->AddAddress($to);
//        }

        if ($captcha){
            if (md5($vars['code'])==$_SESSION['captcha_keystring']) {
                if ($mail->Send()){
                    $mail->ClearAddresses();
                    $_SESSION['sendC'] = 1;
                    return true;
                }
                else
                    return false;
            }
            else {
                $_SESSION['error_data'] = $vars;
                $_SESSION['sendC'] = 2;
                return false;
            }
        }
        else {
            if ($mail->Send()){
                $mail->ClearAddresses();
                $_SESSION['sendC'] = 1;
                return true;
            }
            else
                return false;
        }
    }
}