Skip to content

Commit 162f5dd

Browse files
Merge pull request #22 from stackkit/development
Skip files in attach and attachData if their data is empty or null
2 parents c91e98b + 39491a6 commit 162f5dd

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/EmailComposer.php

+12
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

+44
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ public function attachments_are_added_to_the_email()
167167
$this->assertEquals('application/pdf', $attachment->getContentType());
168168
}
169169

170+
/** @test */
171+
public function attachments_are_not_added_if_the_data_is_not_valid()
172+
{
173+
$this->sent = [];
174+
$this->composeEmail()->attach(null)->send();
175+
$this->artisan('email:send');
176+
$attachments = reset($this->sent)->getMessage()->getChildren();
177+
$this->assertCount(0, $attachments);
178+
179+
$this->sent = [];
180+
$this->composeEmail()->attach(false)->send();
181+
$this->artisan('email:send');
182+
$attachments = reset($this->sent)->getMessage()->getChildren();
183+
$this->assertCount(0, $attachments);
184+
185+
$this->sent = [];
186+
$this->composeEmail()->attach('')->send();
187+
$this->artisan('email:send');
188+
$attachments = reset($this->sent)->getMessage()->getChildren();
189+
$this->assertCount(0, $attachments);
190+
}
191+
170192
/** @test */
171193
public function raw_attachments_are_added_to_the_email()
172194
{
@@ -189,4 +211,26 @@ public function raw_attachments_are_added_to_the_email()
189211
$this->assertEquals('application/pdf', $attachment->getContentType());
190212
$this->assertContains('Hello CI!', $attachment->getBody());
191213
}
214+
215+
/** @test */
216+
public function raw_attachments_are_not_added_if_the_data_is_not_valid()
217+
{
218+
$this->sent = [];
219+
$this->composeEmail()->attachData(null, 'test.png')->send();
220+
$this->artisan('email:send');
221+
$attachments = reset($this->sent)->getMessage()->getChildren();
222+
$this->assertCount(0, $attachments);
223+
224+
$this->sent = [];
225+
$this->composeEmail()->attachData(false, 'test.png')->send();
226+
$this->artisan('email:send');
227+
$attachments = reset($this->sent)->getMessage()->getChildren();
228+
$this->assertCount(0, $attachments);
229+
230+
$this->sent = [];
231+
$this->composeEmail()->attachData('', 'test.png')->send();
232+
$this->artisan('email:send');
233+
$attachments = reset($this->sent)->getMessage()->getChildren();
234+
$this->assertCount(0, $attachments);
235+
}
192236
}

0 commit comments

Comments
 (0)