00001 <?php
00002 Load::components('mailmessage');
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 class MailBaseCommand extends CommandBase {
00033
00034 protected $template = null;
00035
00036 protected $template_args = false;
00037
00038 protected $subject;
00039
00040 protected $email = '';
00041
00042 protected $html_mail = false;
00043
00044
00045 protected $err = null;
00046
00047
00048
00049
00050
00051 protected $view;
00052
00053 protected $alt_message = '';
00054 protected $files_to_attach = array();
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 public function __construct($subject, $email, $template, $template_args = array()) {
00065 $this->template = $template;
00066 $this->template_args = $template_args;
00067 $this->subject = $subject;
00068 $this->email = $email;
00069
00070 $this->err = new Status();
00071 }
00072
00073 public function can_execute($user) {
00074 return (
00075 $user &&
00076 $user != $this->email
00077 );
00078 }
00079
00080
00081
00082
00083
00084
00085 public function execute() {
00086 $to = $this->get_to();
00087 if (empty($to)) {
00088 return $this->err;
00089 }
00090
00091 $message = $this->get_message();
00092 $append = $this->get_message_extension();
00093 if (!empty($append)) {
00094 $message .= "\n\n------------------------\n\n" . $append;
00095 }
00096
00097 $subject = $this->get_subject();
00098
00099 if ($this->err->is_ok()) {
00100 $mail = $this->create_mail_message($subject, $message, $to);
00101
00102
00103 $mail->set_alt_message($this->compute_alt_message($message));
00104
00105
00106 foreach($this->files_to_attach as $filename => $name) {
00107 $mail->add_attachment($filename, $name);
00108 }
00109
00110
00111 foreach(Arr::force($this->get_attachments(), false) as $a) {
00112 $mail->add_attachment($a);
00113 }
00114
00115
00116 $this->err->merge($mail->send());
00117 }
00118
00119 return $this->err;
00120 }
00121
00122
00123
00124
00125
00126
00127 protected function create_mail_message($subject, $message, $to) {
00128 $content_type = $this->html_mail ? 'text/html; charset=%charset' : '';
00129 return new MailMessage($subject, $message, $to, '', $content_type);
00130 }
00131
00132
00133
00134
00135
00136
00137 protected function compute_alt_message($org_message) {
00138 $ret = '';
00139 if ($this->alt_message instanceof IView) {
00140 if ($this->alt_message == $this->view) {
00141 $ret = $org_message;
00142 if ($this->is_html()) {
00143 $ret = ConverterFactory::decode($ret, ConverterFactory::HTML_EX, array('p' => "\n\n", 'a' => '$url$'));
00144 }
00145 } else {
00146 $ret = $this->alt_message->render();
00147 }
00148 }
00149 return $ret;
00150 }
00151
00152
00153
00154
00155 protected function set_error($message) {
00156 $this->err->append($message);
00157 }
00158
00159
00160
00161
00162 public function set_subject($subject) {
00163 $this->subject = $subject;
00164 }
00165
00166
00167
00168
00169 protected function get_subject() {
00170 return $this->subject;
00171 }
00172
00173
00174
00175
00176 protected function get_message() {
00177 $this->view = ViewFactory::create_view(IViewFactory::MESSAGE, $this->template);
00178 if ($this->view) {
00179 foreach(Arr::force($this->template_args) as $name => $value) {
00180 $this->view->assign($name, $value);
00181 }
00182 $this->view->assign('to', $this->get_to());
00183 $this->view->assign('mailcmd', $this);
00184 return $this->view->render();
00185 }
00186 else {
00187 $this->set_error(tr('Mail message view not created', 'commands'));
00188 }
00189 }
00190
00191
00192
00193
00194 protected function get_message_extension() {
00195 return '';
00196 }
00197
00198
00199
00200
00201 protected function get_to() {
00202 return Cast::string($this->email);
00203 }
00204
00205
00206
00207
00208 protected function get_attachments() {
00209 return array();
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 public function set_is_html($yesno) {
00222 $this->html_mail = $yesno;
00223 }
00224
00225
00226
00227
00228
00229
00230 protected function is_html() {
00231 return $this->html_mail;
00232 }
00233
00234
00235
00236
00237
00238
00239 public function set_alt_message($alt_message) {
00240 $this->alt_message = $alt_message;
00241 }
00242
00243
00244
00245
00246
00247
00248 public function get_alt_message() {
00249 return $this->alt_message;
00250 }
00251
00252
00253
00254
00255
00256
00257
00258 public function add_attachment($file_name, $name = '') {
00259 $this->files_to_attach[$file_name] = $name;
00260 }
00261 }