Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Mailer {
- protected $_boundary = false,
- $_charset = 'UTF-8',
- $_header = '',
- $_additional = '';
- public $is_html = false;
- public function setCharset($charset) {
- $this->_charset = $charset;
- }
- public function addHeader($name, $value) {
- $this->_headers[] = "$name: $value";
- }
- public function encode($data) {
- return '=?' . $this->_charset . '?B?' . base64_encode($data) . '?=';
- }
- public function setFrom($email, $name = null) {
- $this->addHeader('From', $name ? $this->encode($name) . " <$email>" : $email);
- }
- public function addAttachment($name, $data, $type = null) {
- if (!$this->_boundary) {
- $this->_boundary = uniqid();
- }
- if (!$type) {
- $type = 'application/octet-stream';
- }
- $this->_additional .= "--{$this->_boundary}\r\n";
- $this->_additional .= "Content-Type: $type; name=\"" . $this->encode($name) . "\"\r\n";
- $this->_additional .= "Content-Disposition: attachment\r\n";
- $this->_additional .= "Content-Transfer-Encoding: base64\r\n\r\n";
- $this->_additional .= chunk_split(base64_encode($data)) . "\r\n";
- }
- public function attachFile($filename, $content_type = null) {
- $this->addAttachment(basename($filename), file_get_contents($filename), $content_type);
- }
- public function send($addr, $subject = '', $message = '') {
- $message = chunk_split(base64_encode($message));
- $content_type = 'Content-Type: text/' . ($this->is_html ? 'html' : 'plain') . '; charset=' . $this->_charset;
- $content_transfer_encoding = 'Content-Transfer-Encoding: base64';
- $body = '';
- if ($this->_boundary) {
- $this->_headers[] = "Content-Type: multipart/mixed; boundary=\"{$this->_boundary}\"";
- $body .= "--{$this->_boundary}\r\n";
- $body .= "$content_type\r\n";
- $body .= "$content_transfer_encoding\r\n\r\n";
- $body .= "$message\r\n";
- $body .= $this->_additional;
- $body .= "--{$this->_boundary}--";
- }
- else {
- $this->_headers[] = $content_type;
- $this->_headers[] = $content_transfer_encoding;
- $body = $message;
- }
- return mail($addr, $this->encode($subject), $body, implode("\r\n", $this->_headers));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment