Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Mailer {
- protected $charset = 'UTF-8',
- $to,
- $from,
- $subject = '',
- $headers = array(),
- $attachments = array();
- public function setCharset($value) {
- $this->charset = $value;
- }
- public 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 addHeader($header) {
- $this->headers[] = $header;
- }
- 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) {
- if ($this->from) {
- $this->headers[] = 'From: ' . $this->from;
- }
- $content_type = 'Content-Type: text/' . ($is_html ? 'html' : 'plain') .
- '; charset=' . $this->charset;
- $content_transfer_encoding = 'Content-Transfer-Encoding: 8 bit';
- if ($count = count($this->attachments)) {
- $boundary = uniqid();
- $this->headers[] = 'Content-Type: multipart/mixed; boundary="' .
- $boundary . '"';
- $body = array("--$boundary", $content_type,
- "$content_transfer_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) . "--$boundary--";
- }
- else {
- $this->headers[] = $content_type;
- $this->headers[] = $content_transfer_encoding;
- }
- return @mail($to, $this->subject, $body, implode("\r\n",
- $this->headers));
- }
- public function html($to, $body) {
- return $this->send($to, $body, 1);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment