Skip to content

Commit e471d65

Browse files
Merge branch 'master' into feature/send-emails-immediately
2 parents bf7862c + 162f5dd commit e471d65

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/EmailComposer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ public function mailable(Mailable $mailable)
218218
*/
219219
public function attach($file, $options = [])
220220
{
221+
$validFileName = (is_string($file) && strlen($file) > 0);
222+
223+
if (! $validFileName) {
224+
return $this;
225+
}
226+
221227
$attachments = $this->hasData('attachments') ? $this->getData('attachments') : [];
222228

223229
$attachments[] = compact('file', 'options');
@@ -235,6 +241,12 @@ public function attach($file, $options = [])
235241
*/
236242
public function attachData($data, $name, array $options = [])
237243
{
244+
$validData = (is_string($data) && strlen($data) > 0);
245+
246+
if (! $validData) {
247+
return $this;
248+
}
249+
238250
$attachments = $this->hasData('rawAttachments') ? $this->getData('rawAttachments') : [];
239251

240252
$attachments[] = compact('data', 'name', 'options');

tests/SenderTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@ public function attachments_are_added_to_the_email()
169169
$this->assertEquals('application/pdf', $attachment->getContentType());
170170
}
171171

172+
/** @test */
173+
public function attachments_are_not_added_if_the_data_is_not_valid()
174+
{
175+
$this->sent = [];
176+
$this->composeEmail()->attach(null)->send();
177+
$this->artisan('email:send');
178+
$attachments = reset($this->sent)->getMessage()->getChildren();
179+
$this->assertCount(0, $attachments);
180+
181+
$this->sent = [];
182+
$this->composeEmail()->attach(false)->send();
183+
$this->artisan('email:send');
184+
$attachments = reset($this->sent)->getMessage()->getChildren();
185+
$this->assertCount(0, $attachments);
186+
187+
$this->sent = [];
188+
$this->composeEmail()->attach('')->send();
189+
$this->artisan('email:send');
190+
$attachments = reset($this->sent)->getMessage()->getChildren();
191+
$this->assertCount(0, $attachments);
192+
}
193+
172194
/** @test */
173195
public function raw_attachments_are_added_to_the_email()
174196
{
@@ -207,4 +229,26 @@ public function emails_can_be_sent_immediately()
207229
$this->artisan('email:send');
208230
$this->assertCount(1, $this->sent);
209231
}
232+
233+
/** @test */
234+
public function raw_attachments_are_not_added_if_the_data_is_not_valid()
235+
{
236+
$this->sent = [];
237+
$this->composeEmail()->attachData(null, 'test.png')->send();
238+
$this->artisan('email:send');
239+
$attachments = reset($this->sent)->getMessage()->getChildren();
240+
$this->assertCount(0, $attachments);
241+
242+
$this->sent = [];
243+
$this->composeEmail()->attachData(false, 'test.png')->send();
244+
$this->artisan('email:send');
245+
$attachments = reset($this->sent)->getMessage()->getChildren();
246+
$this->assertCount(0, $attachments);
247+
248+
$this->sent = [];
249+
$this->composeEmail()->attachData('', 'test.png')->send();
250+
$this->artisan('email:send');
251+
$attachments = reset($this->sent)->getMessage()->getChildren();
252+
$this->assertCount(0, $attachments);
253+
}
210254
}

0 commit comments

Comments
 (0)