Envelope::replyTo()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 6

Duplication

Lines 3
Ratio 50 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 3
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Email\Envelope
5
 *
6
 * Wraps and compile a MIME Email envelope.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2016 - http://caffeina.com
11
 */
12
13
namespace Email;
14
15
class Envelope {
16
17
  protected  $uid,
18
             $to,
19
             $from,
20
             $cc,
21
             $bcc,
22
             $replyTo,
23
             $subject,
24
             $message,
25
             $contentType = 'text/html; charset="utf-8"',
26
             $attachments,
27
             $compiled_head,
28
             $compiled_body;
29
30
  public function __construct($email=null){
31
    if ($email) {
32
      $email = (object)$email;
33
      if(isset($email->to))           $this->to($email->to);
34
      if(isset($email->from))         $this->from($email->from);
35
      if(isset($email->cc))           $this->cc($email->cc);
36
      if(isset($email->bcc))          $this->bcc($email->bcc);
37
      if(isset($email->replyTo))      $this->replyTo($email->replyTo);
38
      if(isset($email->subject))      $this->subject($email->subject);
39
      if(isset($email->message))      $this->message($email->message);
40
      if(isset($email->attachments))  $this->attach($email->attachments);
41
    }
42
    $this->uid  = '_CORE_'.md5(uniqid(time()));
43
44
  }
45
46
  protected function add_emails(&$pool, $emails, $append=true){
47
    $this->compiled_head = null;
48
    foreach ((array)$emails as $values) {
49
      foreach(preg_split('/\s*,\s*/',$values) as $value) {
50
        if(strpos($value,'<')!==false){
51
          $value   = str_replace('>','',$value);
52
          $parts   = explode('<',$value,2);
53
          $name    = trim(current($parts));
54
          $email   = trim(end($parts));
55
          $address = "$name <{$email}>";
56
        } else {
57
          $address = $value;
58
        }
59
        if ($append) $pool[] = $address; else $pool = $address;
60
      }
61
    }
62
  }
63
64
  public function from($value=null){
65 View Code Duplication
    if ($value!==null && $value) {
66
      $this->add_emails($this->from, $value, false);
67
    } else if ($value===false) $this->from = '';
68
    return $this->from;
69
  }
70
71
  public function to($value=null){
72 View Code Duplication
    if ($value!==null && $value) {
73
      $this->add_emails($this->to, $value);
74
    } else if ($value===false) $this->to = [];
75
    return $this->to;
76
  }
77
78
  public function cc($value=null){
79 View Code Duplication
    if ($value!==null && $value) {
80
      $this->add_emails($this->cc, $value);
81
    } else if ($value===false) $this->cc = [];
82
    return $this->cc;
83
  }
84
85
  public function bcc($value=null){
86 View Code Duplication
    if ($value!==null && $value) {
87
      $this->add_emails($this->bcc, $value);
88
    } else if ($value===false) $this->bcc = [];
89
    return $this->bcc;
90
  }
91
92
  public function replyTo($value=null){
93 View Code Duplication
    if ($value!==null && $value) {
94
      $this->add_emails($this->replyTo, $value, false);
95
    } else if ($value===false) $this->replyTo = '';
96
    return $this->replyTo;
97
  }
98
99
  public function subject($value=null){
100
    if ($value!==null && $value) {
101
      $this->compiled_head = null;
102
      $this->subject = $value;
103
    } else if ($value===false) $this->subject = '';
104
    return $this->subject;
105
  }
106
107
  public function contentType($value=null){
108
    if ($value!==null && $value) {
109
      if (empty($this->attachments)) $this->compiled_head = null;
110
      $this->compiled_body = null;
111
      $this->contentType = $value;
112
    } else if ($value===false) $this->contentType = '';
113
    return $this->contentType;
114
  }
115
116
  public function message($value=null){
117
    if ($value!==null && $value) {
118
      $this->compiled_body = null;
119
      $this->message = $value;
120
    } else if ($value===false) $this->message = '';
121
    return $this->message;
122
  }
123
124
  public function attach($file){
125
    $this->compiled_body = null;
126
    if (isset($file->content) || isset($file['content'])) {
127
      $this->attachments[] = $file;
128
    } else foreach ((array)$file as $curfile) {
129
      $this->attachments[] = $curfile;
130
    }
131
  }
132
133
  public function attachments($file=null){
134
    if ($file!==null && $file) $this->attach($file);
135
    return $this->attachments ?: [];
136
  }
137
138
  public function head($recompile = false){
139
    if ($recompile || (null === $this->compiled_head)){
140
      $head   = [];
141
      $head[] = "Subject: {$this->subject}";
142
      if($this->from)                        $head[] = "From: {$this->from}";
143
      if(is_array($this->to)  && !empty($this->to))  $head[] = "To: "  . implode(', ',$this->to);
144 View Code Duplication
      if(is_array($this->cc)  && !empty($this->cc))  $head[] = "Cc: "  . implode(', ',$this->cc);
145 View Code Duplication
      if(is_array($this->bcc) && !empty($this->bcc)) $head[] = "Bcc: " . implode(', ',$this->bcc);
146
      if($this->replyTo)                     $head[] = "Reply-To: {$this->replyTo}";
147
      $head[] = "Content-Type: multipart/mixed; boundary=\"{$this->uid}\"";
148
      $head[] = 'MIME-Version: 1.0';
149
      $head[] = '';
150
      $this->compiled_head = implode("\r\n", $head);
151
    }
152
    return \Filter::with( 'core.email.source.head', $this->compiled_head);
153
  }
154
155
  public function body($recompile = false){
156
    if ($recompile || (null === $this->compiled_body)){
157
      $body = [];
158
      $body[] = "--{$this->uid}";
159
      $body[] = "Content-Type: {$this->contentType}";
160
      $body[] = "Content-Transfer-Encoding: quoted-printable";
161
      $body[] = '';
162
      $body[] = quoted_printable_encode($this->message);
163
      $body[] = '';
164
165
      if (!empty($this->attachments)) foreach ((array)$this->attachments as $file) {
166
167
        if (is_string($file)) {
168
          $name = basename($file);
169
          $data = file_get_contents($file);
170
        } else {
171
          $name = isset($file['name'])    ? $file['name']    : 'untitled';
172
          $data = isset($file['content']) ? $file['content'] : '';
173
        }
174
175
        $body[] = "--{$this->uid}";
176
        $body[] = "Content-Type: application/octet-stream; name=\"{$name}\"";
177
        $body[] = "Content-Transfer-Encoding: base64";
178
        $body[] = "Content-Disposition: attachment; filename=\"{$name}\"";
179
        $body[] = '';
180
        $body[] = chunk_split(base64_encode($data));
181
        $body[] = '';
182
      }
183
184
      $body[] = "--{$this->uid}";
185
186
      $this->compiled_body = implode("\r\n", $body);
187
    }
188
    return \Filter::with( 'core.email.source.body', $this->compiled_body);
189
  }
190
191
  public function build(){
192
    return \Filter::with( 'core.email.source', $this->head() . "\r\n" . $this->body() );
193
  }
194
195
}
196