Completed
Pull Request — master (#39)
by
unknown
01:10
created

InboundEmail::spamScore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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, $spam_score=null, $spam_report=null)
37
    {
38
        return new static([
39
            'message' => $message,
40
            'spam_score' => $spam_score,
41
            'spam_report' => $spam_report,
42
        ]);
43
    }
44
45
    public function id(): string
46
    {
47
        return $this->message()->getHeaderValue('Message-Id', str_random());
0 ignored issues
show
Deprecated Code introduced by
The function str_random() has been deprecated with message: Str::random() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
48
    }
49
50
    public function date(): Carbon
51
    {
52
        return Carbon::make($this->message()->getHeaderValue('Date'));
53
    }
54
55
    public function text(): ?string
56
    {
57
        return $this->message()->getTextContent();
58
    }
59
60
    public function visibleText(): ?string
61
    {
62
        return EmailReplyParser::parseReply($this->text());
63
    }
64
65
    public function html(): ?string
66
    {
67
        return $this->message()->getHtmlContent();
68
    }
69
70
    public function headerValue($headerName): string
71
    {
72
        return $this->message()->getHeaderValue($headerName, null);
73
    }
74
75
    public function subject(): ?string
76
    {
77
        return $this->message()->getHeaderValue('Subject');
78
    }
79
80
    public function from(): string
81
    {
82
        $from = $this->message()->getHeader('From');
83
84
        if ($from instanceof AddressHeader) {
85
            return $from->getEmail();
86
        }
87
88
        return '';
89
    }
90
91
    public function fromName(): string
92
    {
93
        $from = $this->message()->getHeader('From');
94
95
        if ($from instanceof AddressHeader) {
96
            return $from->getPersonName();
97
        }
98
99
        return '';
100
    }
101
102
    public function spamScore(): float
103
    {
104
        return $this->spam_score;
105
    }
106
107
    public function spamReport(): string
108
    {
109
        return $this->spam_report;
110
    }
111
112
    /**
113
     * @return AddressPart[]
114
     */
115
    public function to(): array
116
    {
117
        return $this->convertAddressHeader($this->message()->getHeader('To'));
118
    }
119
120
    /**
121
     * @return AddressPart[]
122
     */
123
    public function cc(): array
124
    {
125
        return $this->convertAddressHeader($this->message()->getHeader('Cc'));
126
    }
127
128
    protected function convertAddressHeader($header): array
129
    {
130
        if ($header instanceof AddressHeader) {
131
            return Collection::make($header->getAddresses())->toArray();
132
        }
133
134
        return [];
135
    }
136
137
    /**
138
     * @return MessagePart[]
139
     */
140
    public function attachments(): array
141
    {
142
        return $this->message()->getAllAttachmentParts();
143
    }
144
145
    public function message(): MimeMessage
146
    {
147
        $this->mimeMessage = $this->mimeMessage ?: MimeMessage::from($this->message);
148
149
        return $this->mimeMessage;
150
    }
151
152
    public function reply(Mailable $mailable)
153
    {
154
        if ($mailable instanceof \Illuminate\Mail\Mailable) {
155
            $mailable->withSwiftMessage(function (\Swift_Message $message) {
156
                $message->getHeaders()->addIdHeader('In-Reply-To', $this->id());
157
            });
158
        }
159
160
        return Mail::to($this->from())->send($mailable);
161
    }
162
163
    public function forward($recipients)
164
    {
165
        return Mail::send([], [], function ($message) use ($recipients) {
166
            $message->to($recipients)
167
                ->subject($this->subject())
168
                ->setBody($this->body(), $this->message()->getContentType());
169
        });
170
    }
171
172
    public function body(): ?string
173
    {
174
        return $this->isHtml() ? $this->html() : $this->text();
175
    }
176
177
    public function isHtml(): bool
178
    {
179
        return ! empty($this->html());
180
    }
181
182
    public function isText(): bool
183
    {
184
        return ! empty($this->text());
185
    }
186
187
    public function isValid(): bool
188
    {
189
        return $this->from() !== '' && ($this->isText() || $this->isHtml());
190
    }
191
}
192