プロジェクト

全般

プロフィール

opMailSend.class.php

Akihiro KOBAYASHI, 2014-04-04 05:24

ダウンロード (6.92 KB)

 
1
<?php
2

    
3
/**
4
 * This file is part of the OpenPNE package.
5
 * (c) OpenPNE Project (http://www.openpne.jp/)
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file and the NOTICE file that were distributed with this source code.
9
 */
10

    
11
/**
12
 * opMailSend
13
 *
14
 * @package    OpenPNE
15
 * @subpackage util
16
 * @author     Kousuke Ebihara <ebihara@php.net>
17
 */
18
class opMailSend
19
{
20
  public $subject = '';
21
  public $body = '';
22
  static protected $initialized = false;
23

    
24
  public static function initialize()
25
  {
26
    if (!self::$initialized)
27
    {
28
      opApplicationConfiguration::registerZend();
29

    
30
      if ($host = sfConfig::get('op_mail_smtp_host'))
31
      {
32
        $tr = new Zend_Mail_Transport_Smtp($host, sfConfig::get('op_mail_smtp_config', array()));
33
        Zend_Mail::setDefaultTransport($tr);
34
      }
35
      elseif ($envelopeFrom = sfConfig::get('op_mail_envelope_from') && !ini_get('safe_mode'))
36
      {
37
        $tr = new Zend_Mail_Transport_Sendmail('-f'.$envelopeFrom);
38
        Zend_Mail::setDefaultTransport($tr);
39
      }
40

    
41
      opApplicationConfiguration::unregisterZend();
42

    
43
      self::$initialized = true;
44
    }
45
  }
46

    
47
  public function setSubject($subject)
48
  {
49
    $this->subject = $subject;
50
  }
51

    
52
  public function setTemplate($template, $params = array())
53
  {
54
    $body = $this->getCurrentAction()->getPartial($template, $params);
55
    $this->body = $body;
56
  }
57

    
58
  public function setGlobalTemplate($template, $params = array())
59
  {
60
    $template = '_'.$template;
61
    $view = new opGlobalPartialView(sfContext::getInstance(), 'superGlobal', $template, '');
62
    $view->getAttributeHolder()->setEscaping(false);
63
    $view->setPartialVars($params);
64
    $body = $view->render();
65
    $this->body = $body;
66
  }
67

    
68
  public function send($to, $from)
69
  {
70
    return self::execute($this->subject, $to, $from, $this->body);
71
  }
72

    
73
  public static function getMailTemplate($template, $target = 'pc', $params = array(), $isOptional = true, $context = null)
74
  {
75
    if (!$context)
76
    {
77
      $context = sfContext::getInstance();
78
    }
79

    
80
    $params['sf_config'] = sfConfig::getAll();
81

    
82
    $view = new sfTemplatingComponentPartialView($context, 'superGlobal', 'notify_mail:'.$target.'_'.$template, '');
83
    $context->set('view_instance', $view);
84

    
85
    $view->getAttributeHolder()->setEscaping(false);
86
    $view->setPartialVars($params);
87
    $view->setAttribute('renderer_config', array('twig' => 'opTemplateRendererTwig'));
88
    $view->setAttribute('rule_config', array('notify_mail' => array(
89
      array('loader' => 'sfTemplateSwitchableLoaderDoctrine', 'renderer' => 'twig', 'model' => 'NotificationMail'),
90
      array('loader' => 'opNotificationMailTemplateLoaderConfigSample', 'renderer' => 'twig'),
91
      array('loader' => 'opNotificationMailTemplateLoaderFilesystem', 'renderer' => 'php'),
92
    )));
93
    $view->execute();
94

    
95
    try
96
    {
97
      return $view->render();
98
    }
99
    catch (InvalidArgumentException $e)
100
    {
101
      if ($isOptional)
102
      {
103
        return '';
104
      }
105

    
106
      throw $e;
107
    }
108
  }
109

    
110
  public static function sendTemplateMail($template, $to, $from, $params = array(), $context = null)
111
  {
112
    if (!$to)
113
    {
114
      return false;
115
    }
116

    
117
    if (empty($params['target']))
118
    {
119
      $target = opToolkit::isMobileEmailAddress($to) ? 'mobile' : 'pc';
120
    }
121
    else
122
    {
123
      $target = $params['target'];
124
    }
125

    
126
    if (in_array($target.'_'.$template, Doctrine::getTable('NotificationMail')->getDisabledNotificationNames()))
127
    {
128
      return false;
129
    }
130

    
131
    if (null === $context)
132
    {
133
      $context = sfContext::getInstance();
134
    }
135

    
136
    $body      = self::getMailTemplate($template, $target, $params, false, $context);
137
    $signature = self::getMailTemplate('signature', $target, array(), true, $context);
138
    if ($signature)
139
    {
140
      $signature = "\n".$signature;
141
    }
142

    
143
    $subject = $params['subject'];
144
    $notificationMail = Doctrine::getTable('NotificationMail')->fetchTemplate($target.'_'.$template);
145
    if (($notificationMail instanceof NotificationMail) && $notificationMail->getTitle())
146
    {
147
      $subject = $notificationMail->getTitle();
148
      $templateStorage = new sfTemplateStorageString($subject);
149
      $renderer = new opTemplateRendererTwig();
150
      $params['sf_type'] = null;
151
      $parameterHolder = new sfViewParameterHolder($context->getEventDispatcher(), $params);
152
      $subject = $renderer->evaluate($templateStorage, $parameterHolder->toArray());
153
      $notificationMail->free(true);
154
    }
155

    
156
    return self::execute($subject, $to, $from, $body.$signature);
157
  }
158

    
159
  public static function sendTemplateMailToMember($template, Member $member, $params = array(), $options = array(), $context = null)
160
  {
161
    $mailConfigs = Doctrine::getTable('NotificationMail')->getConfigs();
162

    
163
    $options = array_merge(array(
164
      'from'           => opConfig::get('admin_mail_address'),
165
      'is_send_pc'     => true,
166
      'is_send_mobile' => true,
167
      'pc_params'      => array(),
168
      'mobile_params'  => array()
169
    ), $options);
170

    
171
    // to pc
172
    if ($options['is_send_pc'] && ($address = $member->getConfig('pc_address')) &&
173
      (
174
        !isset($mailConfigs['pc'][$template]['member_configurable']) ||
175
        !$mailConfigs['pc'][$template]['member_configurable'] ||
176
        $member->getConfig('is_send_pc_'.$template.'_mail', true)
177
      )
178
    )
179
    {
180
      opMailSend::sendTemplateMail($template, $address, $options['from'],
181
        array_merge($params, $options['pc_params']), $context);
182
    }
183

    
184
    // to mobile
185
    if ($options['is_send_mobile'] && ($address = $member->getConfig('mobile_address')) &&
186
      (
187
        !isset($mailConfigs['mobile'][$template]['member_configurable']) ||
188
        !$mailConfigs['mobile'][$template]['member_configurable'] ||
189
        $member->getConfig('is_send_mobile_'.$template.'_mail', true)
190
      )
191
    )
192
    {
193
      opMailSend::sendTemplateMail($template, $address, $options['from'],
194
        array_merge($params, $options['mobile_params']), $context);
195
    }
196
  }
197

    
198
  public static function execute($subject, $to, $from, $body)
199
  {
200
    if (!$to)
201
    {
202
      return false;
203
    }
204

    
205
    self::initialize();
206

    
207
    opApplicationConfiguration::registerZend();
208

    
209
    $subject = mb_convert_kana($subject, 'KV');
210

    
211
    if ($envelopeFrom = sfConfig::get('op_mail_envelope_from'))
212
    {
213
      $envelopeFrom = '-f'.$envelopeFrom;
214
      $tr = new Zend_Mail_Transport_Sendmail($envelopeFrom);
215
      Zend_Mail::setDefaultTransport($tr);
216
    }
217

    
218
    $mailer = new Zend_Mail('iso-2022-jp');
219
    $mailer->setHeaderEncoding(Zend_Mime::ENCODING_BASE64)
220
      ->setFrom($from)
221
      ->addTo($to)
222
      ->setSubject(mb_encode_mimeheader($subject, 'iso-2022-jp'))
223
      ->setBodyText(mb_convert_encoding($body, 'JIS', 'UTF-8'), 'iso-2022-jp', Zend_Mime::ENCODING_7BIT);
224

    
225
    $result = $mailer->send();
226

    
227
    opApplicationConfiguration::unregisterZend();
228

    
229
    return $result;
230
  }
231

    
232
 /**
233
  * Gets the current action instance.
234
  *
235
  * @return sfAction
236
  */
237
  protected function getCurrentAction()
238
  {
239
    return sfContext::getInstance()->getController()->getActionStack()->getLastEntry()->getActionInstance();
240
  }
241
}