CMSimple_XH 開発者ドキュメント
Mail.php
1 <?php
2 
16 namespace XH;
17 
28 class Mail
29 {
35  private $to;
36 
42  private $subject;
43 
49  private $message;
50 
56  private $headers;
57 
63  private $lineEnding;
64 
70  public function __construct()
71  {
72  global $cf;
73 
74  $this->headers = array(
75  'MIME-Version' => '1.0',
76  'Content-Type' => 'text/plain; charset=UTF-8; format=flowed',
77  'Content-Transfer-Encoding' => 'base64'
78  );
79  $this->lineEnding = $cf['mailform']['lf_only'] ? "\n" : "\r\n";
80  }
81 
93  public function isValidAddress($address)
94  {
95  $atext = '[!#-\'*+\-\/-9=?A-Z^-~]';
96  $dotAtomText = $atext . '(?:' . $atext . '|\.)*';
97  $pattern = '/^(' . $dotAtomText . ')@([^@]+)$/u';
98  if (!preg_match($pattern, $address, $matches)) {
99  return false;
100  }
101  $domain = $matches[2];
102  if (function_exists('idn_to_ascii')) {
103  $domain = defined('INTL_IDNA_VARIANT_UTS46')
104  ? idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46)
105  : idn_to_ascii($domain);
106  }
107  if ($domain
108  && (strlen($domain) > 255 || gethostbyname($domain) == $domain)
109  ) {
110  return false;
111  }
112  return true;
113  }
114 
122  public function setTo($to)
123  {
124  $this->to = $to;
125  }
126 
132  public function getSubject()
133  {
134  return $this->subject;
135  }
136 
144  public function setSubject($subject)
145  {
146  $this->subject = $this->encodeMIMEFieldBody($subject);
147  }
148 
156  public function setMessage($message)
157  {
158  $message = preg_replace('/\r\n|\r|\n/', $this->lineEnding, trim($message));
159  $message = chunk_split(base64_encode($message));
160  $this->message = $message;
161  }
162 
171  public function addHeader($name, $value)
172  {
173  $this->headers[$name] = $value;
174  }
175 
186  private function encodeMIMEFieldBody($text)
187  {
188  if (!preg_match('/(?:[^\x00-\x7F])/', $text)) { // ASCII only
189  return $text;
190  } else {
191  $lines = array();
192  do {
193  $i = 45;
194  if (strlen($text) > $i) {
195  while ((ord($text[$i]) & 0xc0) == 0x80) {
196  $i--;
197  }
198  $lines[] = substr($text, 0, $i);
199  $text = substr($text, $i);
200  } else {
201  $lines[] = $text;
202  $text = '';
203  }
204  } while ($text != '');
205  $func = function ($line) {
206  return '=?UTF-8?B?' . base64_encode($line) . '?=';
207  };
208  return implode($this->lineEnding . ' ', array_map($func, $lines));
209  }
210  }
211 
217  private function getHeaderString()
218  {
219  $string = '';
220  foreach ($this->headers as $name => $value) {
221  $string .= $name . ': ' . $value . $this->lineEnding;
222  }
223  return $string;
224  }
225 
231  public function send()
232  {
233  if (!isset($this->to, $this->subject, $this->message)) {
234  return false;
235  } else {
236  return mail($this->to, $this->subject, $this->message, $this->getHeaderString());
237  }
238  }
239 }
setSubject($subject)
Definition: Mail.php:144
getSubject()
Definition: Mail.php:132
isValidAddress($address)
Definition: Mail.php:93
send()
Definition: Mail.php:231
addHeader($name, $value)
Definition: Mail.php:171
$i
Definition: cms.php:193
$cf
Definition: cms.php:272
setMessage($message)
Definition: Mail.php:156
setTo($to)
Definition: Mail.php:122
$text
Definition: cms.php:698
__construct()
Definition: Mail.php:70
Definition: Mail.php:28