1<?php
2
3namespace Illuminate\Mail;
4
5use Illuminate\Contracts\Mail\Mailable as MailableContract;
6use Illuminate\Contracts\Mail\Mailer as MailerContract;
7use Illuminate\Contracts\Translation\HasLocalePreference;
8
9class PendingMail
10{
11    /**
12     * The mailer instance.
13     *
14     * @var \Illuminate\Contracts\Mail\Mailer
15     */
16    protected $mailer;
17
18    /**
19     * The locale of the message.
20     *
21     * @var string
22     */
23    protected $locale;
24
25    /**
26     * The "to" recipients of the message.
27     *
28     * @var array
29     */
30    protected $to = [];
31
32    /**
33     * The "cc" recipients of the message.
34     *
35     * @var array
36     */
37    protected $cc = [];
38
39    /**
40     * The "bcc" recipients of the message.
41     *
42     * @var array
43     */
44    protected $bcc = [];
45
46    /**
47     * Create a new mailable mailer instance.
48     *
49     * @param  \Illuminate\Contracts\Mail\Mailer  $mailer
50     * @return void
51     */
52    public function __construct(MailerContract $mailer)
53    {
54        $this->mailer = $mailer;
55    }
56
57    /**
58     * Set the locale of the message.
59     *
60     * @param  string  $locale
61     * @return $this
62     */
63    public function locale($locale)
64    {
65        $this->locale = $locale;
66
67        return $this;
68    }
69
70    /**
71     * Set the recipients of the message.
72     *
73     * @param  mixed  $users
74     * @return $this
75     */
76    public function to($users)
77    {
78        $this->to = $users;
79
80        if (! $this->locale && $users instanceof HasLocalePreference) {
81            $this->locale($users->preferredLocale());
82        }
83
84        return $this;
85    }
86
87    /**
88     * Set the recipients of the message.
89     *
90     * @param  mixed  $users
91     * @return $this
92     */
93    public function cc($users)
94    {
95        $this->cc = $users;
96
97        return $this;
98    }
99
100    /**
101     * Set the recipients of the message.
102     *
103     * @param  mixed  $users
104     * @return $this
105     */
106    public function bcc($users)
107    {
108        $this->bcc = $users;
109
110        return $this;
111    }
112
113    /**
114     * Send a new mailable message instance.
115     *
116     * @param  \Illuminate\Contracts\Mail\Mailable  $mailable
117     * @return void
118     */
119    public function send(MailableContract $mailable)
120    {
121        $this->mailer->send($this->fill($mailable));
122    }
123
124    /**
125     * Push the given mailable onto the queue.
126     *
127     * @param  \Illuminate\Contracts\Mail\Mailable  $mailable
128     * @return mixed
129     */
130    public function queue(MailableContract $mailable)
131    {
132        return $this->mailer->queue($this->fill($mailable));
133    }
134
135    /**
136     * Deliver the queued message after the given delay.
137     *
138     * @param  \DateTimeInterface|\DateInterval|int  $delay
139     * @param  \Illuminate\Contracts\Mail\Mailable  $mailable
140     * @return mixed
141     */
142    public function later($delay, MailableContract $mailable)
143    {
144        return $this->mailer->later($delay, $this->fill($mailable));
145    }
146
147    /**
148     * Populate the mailable with the addresses.
149     *
150     * @param  \Illuminate\Contracts\Mail\Mailable  $mailable
151     * @return \Illuminate\Mail\Mailable
152     */
153    protected function fill(MailableContract $mailable)
154    {
155        return tap($mailable->to($this->to)
156            ->cc($this->cc)
157            ->bcc($this->bcc), function (MailableContract $mailable) {
158                if ($this->locale) {
159                    $mailable->locale($this->locale);
160                }
161            });
162    }
163}
164