OpenSencillo  2015.009
Long live the simplicity of PHP
 All Data Structures Namespaces Files Functions Pages
mail.generator.mailgen.php
1 <?php
11 class mailGen
12 {
13  protected $head;
14  protected $body;
15  protected $mail;
16  protected $from;
17  protected $to;
18  protected $charset='UTF-8';
19  protected $ctype='text/html';
20  protected $subject;
21 
26  public function to($to)
27  {
28  $this->to = $to;
29  }
30 
35  public function from($from)
36  {
37  $this->from = $from;
38  }
39 
44  public function subject($subject)
45  {
46  $this->subject = $subject;
47  }
48 
53  public function html($bool=true)
54  {
55  if($bool)
56  {
57  $this->ctype = 'text/html';
58  }
59  else
60  {
61  $this->ctype = 'text/plain';
62  }
63  }
64 
69  public function text($bool=true)
70  {
71  if($bool)
72  {
73  $this->ctype = 'text/plain';
74  }
75  else
76  {
77  $this->ctype = 'text/html';
78  }
79  }
80 
84  private function head()
85  {
86  $this->head[]='MIME-Version: 1.0';
87  $this->head[]='Content-type: '.$this->ctype.'; charset='.$this->charset;
88  $this->head[]='From: '.$this->from;
89  $this->head[]='Reply-To: '.$this->from;
90  $this->head[]='X-Mailer: PHP/'.phpversion();
91  return implode("\r\n",$this->head);
92  }
93 
98  public function body($content)
99  {
100  $this->body[] = $content;
101  }
102 
107  public function send()
108  {
109  $this->head();
110  if((filter_var($this->from, FILTER_VALIDATE_EMAIL))&&(filter_var($this->to, FILTER_VALIDATE_EMAIL)))
111  {
112  return mail(''.$this->to.'',$this->subject,implode(PHP_EOL,$this->body),implode("\r\n",$this->head));
113  }
114  else
115  {
116  return false;
117  }
118  }
119 
124  public function compile()
125  {
126  $h = $this->head();
127  $b = implode(PHP_EOL,$this->body);
128  return array(
129  'source'=>array(
130  'head'=>$this->head,
131  'from'=>$this->from,
132  'to'=>$this->to,
133  'subject'=>$this->subject,
134  'body'=>$this->body,
135  'text'=>convert::stripHtml($this->body)
136  ),
137  'send'=>array(
138  'head'=>$h,
139  'to'=>$this->to,
140  'subject'=>$this->subject,
141  'body'=>$b
142  )
143  );
144  }
145 }
146 ?>