1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\Mailbox; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Facades\Mail; |
8
|
|
|
use EmailReplyParser\EmailReplyParser; |
9
|
|
|
use Illuminate\Contracts\Mail\Mailable; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use ZBateson\MailMimeParser\Header\AddressHeader; |
12
|
|
|
use ZBateson\MailMimeParser\Message as MimeMessage; |
13
|
|
|
use ZBateson\MailMimeParser\Header\Part\AddressPart; |
14
|
|
|
use ZBateson\MailMimeParser\Message\Part\MessagePart; |
15
|
|
|
|
16
|
|
|
class InboundEmail extends Model |
17
|
|
|
{ |
18
|
|
|
protected $table = 'mailbox_inbound_emails'; |
19
|
|
|
|
20
|
|
|
/** @var MimeMessage */ |
21
|
|
|
protected $mimeMessage; |
22
|
|
|
|
23
|
|
|
protected $fillable = [ |
24
|
|
|
'message', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
protected static function boot() |
28
|
|
|
{ |
29
|
|
|
parent::boot(); |
30
|
|
|
|
31
|
|
|
static::creating(function ($model) { |
32
|
|
|
$model->message_id = $model->id(); |
33
|
|
|
}); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function fromMessage($message) |
37
|
|
|
{ |
38
|
|
|
return new static([ |
39
|
|
|
'message' => $message, |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function id(): string |
44
|
|
|
{ |
45
|
|
|
return $this->message()->getHeaderValue('Message-Id', str_random()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function date(): Carbon |
49
|
|
|
{ |
50
|
|
|
return Carbon::make($this->message()->getHeaderValue('Date')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function text(): ?string |
54
|
|
|
{ |
55
|
|
|
return $this->message()->getTextContent(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function visibleText(): ?string |
59
|
|
|
{ |
60
|
|
|
return EmailReplyParser::parseReply($this->text()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function html(): ?string |
64
|
|
|
{ |
65
|
|
|
return $this->message()->getHtmlContent(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function subject(): ?string |
69
|
|
|
{ |
70
|
|
|
return $this->message()->getHeaderValue('Subject'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function from(): string |
74
|
|
|
{ |
75
|
|
|
$from = $this->message()->getHeader('From'); |
76
|
|
|
|
77
|
|
|
if ($from instanceof AddressHeader) { |
78
|
|
|
return $from->getEmail(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return ''; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function fromName(): string |
85
|
|
|
{ |
86
|
|
|
$from = $this->message()->getHeader('From'); |
87
|
|
|
|
88
|
|
|
if ($from instanceof AddressHeader) { |
89
|
|
|
return $from->getPersonName(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return ''; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return AddressPart[] |
97
|
|
|
*/ |
98
|
|
|
public function to(): array |
99
|
|
|
{ |
100
|
|
|
return $this->convertAddressHeader($this->message()->getHeader('To')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return AddressPart[] |
105
|
|
|
*/ |
106
|
|
|
public function cc(): array |
107
|
|
|
{ |
108
|
|
|
return $this->convertAddressHeader($this->message()->getHeader('Cc')); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function convertAddressHeader($header): array |
112
|
|
|
{ |
113
|
|
|
if ($header instanceof AddressHeader) { |
114
|
|
|
return Collection::make($header->getAddresses())->toArray(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return []; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return MessagePart[] |
122
|
|
|
*/ |
123
|
|
|
public function attachments() |
124
|
|
|
{ |
125
|
|
|
return $this->message()->getAllAttachmentParts(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function message(): MimeMessage |
129
|
|
|
{ |
130
|
|
|
$this->mimeMessage = $this->mimeMessage ?: MimeMessage::from($this->message); |
|
|
|
|
131
|
|
|
|
132
|
|
|
return $this->mimeMessage; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function reply(Mailable $mailable) |
136
|
|
|
{ |
137
|
|
|
if ($mailable instanceof \Illuminate\Mail\Mailable) { |
138
|
|
|
$mailable->withSwiftMessage(function (\Swift_Message $message) { |
139
|
|
|
$message->getHeaders()->addIdHeader('In-Reply-To', $this->id()); |
140
|
|
|
}); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return Mail::to($this->from())->send($mailable); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function forward($recipients) |
147
|
|
|
{ |
148
|
|
|
return Mail::send([], [], function ($message) use ($recipients) { |
149
|
|
|
$message->to($recipients) |
150
|
|
|
->subject($this->subject()) |
151
|
|
|
->setBody($this->body(), $this->message()->getContentType()); |
152
|
|
|
}); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function body() |
156
|
|
|
{ |
157
|
|
|
return $this->isHtml() ? $this->html() : $this->text(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function isHtml() |
161
|
|
|
{ |
162
|
|
|
return empty($this->html()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function isText() |
166
|
|
|
{ |
167
|
|
|
return empty($this->text()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function isValid(): bool |
171
|
|
|
{ |
172
|
|
|
return $this->from() !== '' && ($this->text() !== '' || $this->html() !== ''); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.