MailTemplate.class.php 2.94 KB
Newer Older
Игорь's avatar
init    
Игорь committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

/**
 * MailTemplate
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 * 
 * @package    sf
 * @subpackage model
 * @author     Atma
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
class MailTemplate extends BaseMailTemplate
{
    public static function sendMailUser($event, $body_mail, $theme_mail, $recipient = false, $attach = false)
    {
        if($event){
            $mail_template = Doctrine::getTable('MailTemplate')->findOneByEvent($event);
            if($mail_template){
                $theme = $mail_template->getTheme();
                $body = $mail_template->getBody();

                if(count($body_mail) > 0){
                    foreach ($body_mail as $ket_b => $b_mail){
                        $body = str_replace('#' . $ket_b . '#', $b_mail , $body);
                    }
                }

                if(count($theme_mail) > 0){
                    foreach ($theme_mail as $ket_th => $th_mail){
                        $theme = str_replace('#'  . $ket_th . '#' , $th_mail , $theme);
                    }
                }

                if($body != ''){
                    $default_email [] = sfConfig::get('app_email_sender');
                    if($mail_template->getCopy() != ''){
                        $copy_email = explode(',', str_replace(' ', '', $mail_template->getCopy()));
                        $emails_array = array_merge($default_email, $copy_email);
                    } else {
                        $emails_array = $default_email;
                    }

                    $context = sfContext::hasInstance() ? sfContext::getInstance() : sfContext::createInstance(ProjectConfiguration::getApplicationConfiguration('www', 'prod', false));
                    $mailer = $context->getMailer();
                    $message = Swift_Message::newInstance()
                        ->setFrom(sfConfig::get('app_email_sender'));
                    if($recipient != false){
                        $message->setTo($recipient);
                    } else {
                        $message->setTo($emails_array);
                    }
                    $message->setSubject($theme);

                    $message->setContentType('text/html; charset=UTF-8');
                    $message->setBody($body);
                    //$this->getMailer()->send($message);
//                    if($attach)
//                    {
//                        $message->attach(Swift_Attachment::fromPath($attach));
//                    }
                    try{
                        if(!$mailer->send($message)){
                            throw new Exception();
                        }
                    }
                    catch (Exception $ex) {
                        file_put_contents(sfConfig::get('sf_log_dir').'/mailError.txt', date("Y-m-d H:i:s") .' '. $ex->getMessage() . ' ' . $ex->getFile() . ' ' . $ex->getLine());
                    }
                }
            }
        }

    }
}