MkFramework
 All Data Structures Functions
plugin_mail.php
1 <?php
2 /*
3 This file is part of Mkframework.
4 
5 Mkframework is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License.
8 
9 Mkframework is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public License
15 along with Mkframework. If not, see <http://www.gnu.org/licenses/>.
16 
17 */
24 
25  private $sFrom;
26  private $sFromLibelle;
27  private $tTo;
28  private $tCC;
29  private $sBCC;
30  private $sTitle;
31  private $sBody;
32  private $sBodyHtml;
33  private $sAttached;
34 
35  private $sErrors;
36 
37  private $sWall;
38 
39  public function __construct(){
40  $this->sWall='afdsfdsfsqhghregregftervgqrdgregregfdgdfd';
41  }
42 
43 
44  public function setFrom($sFromLibelle,$sFrom){
45  $this->sFrom=$sFrom;
46  $this->sFromLibelle=$sFromLibelle;
47  }
48  public function addTo($sTo){
49  $this->tTo[$sTo]=$sTo;
50  }
51  public function addCC($sCC){
52  $this->tCC[$sCC]=$sCC;
53  }
54  public function setBcc($sBCC){
55  $this->sBCC=$sBCC;
56  }
57 
58  public function setTitle($sTitle){
59  $this->sTitle=$sTitle;
60  }
61  public function setSubject($sTitle){
62  $this->sTitle=$sTitle;
63  }
64  public function setBody($sBody){
65  $this->sBody=$sBody;
66  }
67  public function setBodyHtml($sBody){
68  $this->sBodyHtml=$sBody;
69  }
70 
71  public function attachFile($sFile){
72  $this->sAttached=$sFile;
73  }
74 
75  private function checkEmail($sEmail){
76  return preg_match('/^([a-zA-Z0-9\-\_\.]*)@([a-zA-Z0-9\-\_\.]*)\.([a-zA-Z]*)$/',$sEmail);
77  }
78  private function isValid(){
79  $ok=1;
80 
81  $tErrors=array();
82  if($this->sFrom == ''){
83  $ok=0;
84  $tErrors[]="Pas d'email from";
85  }elseif(!$this->checkEmail($this->sFrom)){
86  $ok=0;
87  $tErrors[]="Mauvais format pour l'email from (".$this->sFrom.")";
88  }
89 
90  if( count($this->tTo) == 0){
91  $ok=0;
92  $tErrors[]="Pas d'email to";
93  }else{
94  foreach($this->tTo as $sTo){
95  if(!$this->checkEmail($sTo)){
96  $ok=0;
97  $tErrors[]="Mauvais format pour l'email to (".$sTo.")";
98  }
99  }
100  }
101 
102  if($this->sTitle == ''){
103  $ok=0;
104  $tErrors[]="Pas de sujet";
105  }
106 
107 
108  if(!$ok){
109  $this->sErrors="Erreur plugin_mail lors de l envoi de l'email \n";
110  $this->sErrors.=implode("\n",$tErrors);
111  }
112 
113  return $ok;
114  }
115 
116  public function send(){
117 
118  if(!$this->isValid()){
119  throw new Exception($this->sErrors);
120  }
121 
122  $n0="\n";
123  $r0="\r";
124  $n=$n0;
125  $nn=$n0.$n0 ;
126  $rn=$r0.$n0 ;
127 
128  $sHeader = 'From: "'.$this->sFromLibelle.'" <'.$this->sFrom.'>'.$n;
129  if($this->sBCC!=''){
130  $sHeader .= 'Bcc: '.$this->sBCC."\r\n";
131  }
132  if($this->tCC){
133  $sHeader .= 'Cc: '.implode(',',$this->tCC)."\r\n";
134  }
135  $sHeader .= 'Return-Path: <'.$this->sFrom.'>'.$n;
136  $sHeader .= 'MIME-Version: 1.0'.$n;
137  $sHeader .= 'Content-Type: multipart/mixed; boundary="'.$this->sWall.'"'.$rn;
138 
139  $sMsg = '';
140 
141  if($this->sBody != ''){
142  $sMsg .= '--'.$this->sWall.$n;
143  $sMsg .= 'Content-Type: text/plain; charset="iso-8859-1"'.$nn;
144  $sMsg .= $this->sBody.$nn;
145  }
146 
147  if($this->sBodyHtml != ''){
148  $sMsg .= '--'.$this->sWall.$n;
149  $sMsg .= 'Content-Type: text/html; charset="iso-8859-1"'.$nn;
150  $sMsg .= $this->sBodyHtml.$nn;
151  }
152 
153  if($this->sAttached != ''){
154  $sMsg .= '--'.$this->sWall.''.$n;
155  $sMsg .= 'Content-Type: text/csv; name="'.$this->sAttached.'"'.$n;
156  $sMsg .= 'Content-Transfer-Encoding: base64'.$n;
157  $sMsg .= 'Content-Disposition:attachement; filename="'.$this->sAttached.'"'.$nn;
158  $sMsg .= chunk_split(base64_encode(file_get_contents($this->sAttached)))."\n";
159  }
160 
161  if( mail( implode(',',$this->tTo),$this->sTitle,$sMsg,$sHeader)){
162  return true;
163  }else{
164  return false;
165  }
166  }
167 
168 
169 }