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