Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Mailer {
- protected $charset = 'UTF-8',
- $to,
- $from,
- $subject = '',
- $attachments = array();
- public function setCharset($value) {
- $this->charset = $value;
- }
- protected function encode($value) {
- return '=?' . $this->charset . '?B?' . base64_encode($value) . '?=';
- }
- public function setFrom($email, $name = null) {
- $this->from = $name ? $this->encode($name) . " <$email>" : $email;
- }
- public function setSubject($value) {
- $this->subject = $this->encode($value);
- }
- public function addAttachment($name, $data, $type = 'application/octet-stream') {
- $name = $this->encode($name);
- $data = chunk_split( base64_encode($data) );
- $this->attachments[] = compact('name', 'data', 'type');
- }
- public function send($to, $body, $is_html = 0) {
- // $headers = array('Mime-Version: 1.0');
- $headers = array();
- if ($this->from) {
- $headers[] = 'From: ' . $this->from;
- }
- $type = 'Content-Type: text/' . ($is_html ? 'html' : 'plain') . '; charset=' . $this->charset;
- $encoding = 'Content-Transfer-Encoding: 8 bit';
- if ( $count = count($this->attachments) ) {
- $boundary = md5( time() );
- $headers[] = 'Content-Type: multipart/mixed; boundary="' . $boundary . '"';
- $body = array("--$boundary", $type, "$encoding\r\n", $body);
- for ($i = 0; $i < $count; ++$i) {
- $body[] = "--$boundary";
- $body[] = "Content-Type: " . $this->attachments[$i]['type'] . "; name=\"" . $this->attachments[$i]['name'] . "\"";
- $body[] = 'Content-Disposition: attachment';
- $body[] = "Content-Transfer-Encoding: base64\r\n";
- $body[] = $this->attachments[$i]['data'];
- }
- $body = implode("\r\n", $body) . "\r\n--$boundary--";
- }
- else {
- $headers[] = $type;
- $headers[] = $encoding;
- }
- return @mail( $to, $this->subject, $body, implode("\r\n", $headers) );
- }
- public function html($to, $body) {
- return $this->send($to, $body, 1);
- }
- }
- // $mail = new Mailer;
- // $mail->setSubject('Привет');
- // $mail->addAttachment('file.txt', 'Some contents');
- // $mail->send('[email protected]', 'Как дела?');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment