Skip to content

Commit 7035677

Browse files
Merge pull request #2 from Dudelisius/development
Custom sender per e-mail
2 parents 0a9af3c + b68b38f commit 7035677

13 files changed

+225
-3
lines changed

database/migrations/2017_12_14_151421_add_attachments_to_emails_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function up()
1818
}
1919

2020
Schema::table('emails', function (Blueprint $table) {
21-
$table->binary('attachments')->after('body')->default('');
21+
$table->binary('attachments')->nullable()->after('body');
2222
});
2323
}
2424

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddFromToEmailsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
if (Schema::hasColumn('emails', 'from')) {
17+
return;
18+
}
19+
20+
Schema::table('emails', function (Blueprint $table) {
21+
$table->binary('from')->after('body')->nullable();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
//
33+
}
34+
}

src/Email.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @property $id
1111
* @property $label
1212
* @property $recipient
13+
* @property $from
1314
* @property $cc
1415
* @property $bcc
1516
* @property $subject
@@ -84,6 +85,36 @@ public function getRecipient()
8485
return $this->recipient;
8586
}
8687

88+
/**
89+
* Get the e-mail from.
90+
*
91+
* @return array
92+
*/
93+
public function getFrom()
94+
{
95+
return $this->from;
96+
}
97+
98+
/**
99+
* Get the e-mail from address.
100+
*
101+
* @return string|null
102+
*/
103+
public function getFromAddress()
104+
{
105+
return $this->from['address'] ?? config('mail.from.address');
106+
}
107+
108+
/**
109+
* Get the e-mail from address.
110+
*
111+
* @return string|null
112+
*/
113+
public function getFromName()
114+
{
115+
return $this->from['name'] ?? config('mail.from.name');
116+
}
117+
87118
/**
88119
* Get the e-mail recipient(s) as string.
89120
*
@@ -231,6 +262,16 @@ public function getError()
231262
return $this->error;
232263
}
233264

265+
/**
266+
* Determine if the e-mail should be sent with custom from values.
267+
*
268+
* @return bool
269+
*/
270+
public function hasFrom()
271+
{
272+
return is_array($this->from) && count($this->from) > 0;
273+
}
274+
234275
/**
235276
* Determine if the e-mail should be sent as a carbon copy.
236277
*

src/EmailComposer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ public function label($label)
9292
return $this->setData('label', $label);
9393
}
9494

95+
/**
96+
* Set the e-mail from address and aname.
97+
*
98+
* @param array $address
99+
* @param array $name
100+
* @return static
101+
*/
102+
public function from($address = null, $name = null)
103+
{
104+
return $this->setData('from', compact('address', 'name'));
105+
}
106+
95107
/**
96108
* Set the e-mail recipient(s).
97109
*

src/Encrypter.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public function encrypt(EmailComposer $composer)
1515

1616
$this->encryptRecipients($composer);
1717

18+
$this->encryptFrom($composer);
19+
1820
$this->encryptSubject($composer);
1921

2022
$this->encryptVariables($composer);
@@ -48,6 +50,20 @@ private function encryptRecipients(EmailComposer $composer)
4850
]);
4951
}
5052

53+
/**
54+
* Encrypt the e-mail addresses for the from field.
55+
*
56+
* @param EmailComposer $composer
57+
*/
58+
private function encryptFrom(EmailComposer $composer)
59+
{
60+
$email = $composer->getEmail();
61+
62+
$email->fill([
63+
'from' => encrypt($email->from),
64+
]);
65+
}
66+
5167
/**
5268
* Encrypt the e-mail subject.
5369
*

src/HasEncryptedAttributes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ trait HasEncryptedAttributes
1313
*/
1414
private $encrypted = [
1515
'recipient',
16+
'from',
1617
'cc',
1718
'bcc',
1819
'subject',
@@ -27,6 +28,7 @@ trait HasEncryptedAttributes
2728
*/
2829
private $encoded = [
2930
'recipient',
31+
'from',
3032
'cc',
3133
'bcc',
3234
'variables',

src/MailableReader.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public function read(EmailComposer $composer)
1616
{
1717
$this->readRecipient($composer);
1818

19+
$this->readFrom($composer);
20+
1921
$this->readCc($composer);
2022

2123
$this->readBcc($composer);
@@ -54,6 +56,21 @@ private function readRecipient(EmailComposer $composer)
5456
$composer->recipient($to);
5557
}
5658

59+
/**
60+
* Read the mailable from field to the email composer.
61+
*
62+
* @param EmailComposer $composer
63+
*/
64+
private function readFrom(EmailComposer $composer)
65+
{
66+
$from = reset($composer->getData('mailable')->from);
67+
68+
$composer->from(
69+
$from['address'],
70+
$from['name']
71+
);
72+
}
73+
5774
/**
5875
* Read the mailable cc to the email composer.
5976
*

src/Preparer.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public function prepare(EmailComposer $composer)
1717

1818
$this->prepareRecipient($composer);
1919

20+
$this->prepareFrom($composer);
21+
2022
$this->prepareCc($composer);
2123

2224
$this->prepareBcc($composer);
@@ -66,6 +68,18 @@ private function prepareRecipient(EmailComposer $composer)
6668
]);
6769
}
6870

71+
/**
72+
* Prepare the from values for database storage.
73+
*
74+
* @param EmailComposer $composer
75+
*/
76+
private function prepareFrom(EmailComposer $composer)
77+
{
78+
$composer->getEmail()->fill([
79+
'from' => json_encode($composer->getData('from', '')),
80+
]);
81+
}
82+
6983
/**
7084
* Prepare the carbon copies for database storage.
7185
*

src/Sender.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private function buildMessage(Message $message, Email $email)
4949
->cc($email->hasCc() ? $email->getCc() : [])
5050
->bcc($email->hasBcc() ? $email->getBcc() : [])
5151
->subject($email->getSubject())
52-
->from(config('mail.from.address'), config('mail.from.name'))
52+
->from($email->getFromAddress(), $email->getFromName())
5353
->setBody($email->getBody(), 'text/html');
5454

5555
$attachmentMap = [

tests/DatabaseInteractionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ function the_body_should_be_saved_correctly()
101101
$this->assertSame($expectedBody, $email->getBody());
102102
}
103103

104+
/** @test */
105+
function from_should_be_saved_correctly()
106+
{
107+
$email = $this->composeEmail()->send();
108+
109+
$this->assertFalse($email->hasFrom());
110+
$this->assertEquals(config('mail.from.address'), $email->getFromAddress());
111+
$this->assertEquals(config('mail.from.name'), $email->getFromName());
112+
113+
$email = $this->composeEmail()->from('marick@dolphiq.nl', 'Marick')->send();
114+
115+
$this->assertTrue($email->hasFrom());
116+
$this->assertEquals('marick@dolphiq.nl', $email->getFromAddress());
117+
$this->assertEquals('Marick', $email->getFromName());
118+
}
119+
104120
/** @test */
105121
function variables_should_be_saved_correctly()
106122
{

tests/EncryptionTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function the_variables_should_be_encrypted_and_decrypted()
6262
$email = $this->sendEmail(['variables' => ['name' => 'Jane Doe']]);
6363

6464
$this->assertEquals(
65-
['name'=> 'Jane Doe'],
65+
['name' => 'Jane Doe'],
6666
decrypt($email->getOriginal('variables'))
6767
);
6868

@@ -83,4 +83,18 @@ function the_body_should_be_encrypted_and_decrypted()
8383

8484
$this->assertEquals($expectedBody, $email->getBody());
8585
}
86+
87+
/** @test */
88+
function from_should_be_encrypted_and_decrypted()
89+
{
90+
$email = $this->composeEmail()->from('marick@dolphiq.nl', 'Marick')->send();
91+
92+
$expect = [
93+
'address' => 'marick@dolphiq.nl',
94+
'name' => 'Marick',
95+
];
96+
97+
$this->assertEquals($expect, decrypt($email->getOriginal('from')));
98+
$this->assertEquals($expect, $email->getFrom());
99+
}
86100
}

tests/MailableReaderTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,37 @@ function it_extracts_attachments()
7171
$this->assertEquals('order.html', $attachments[1]['attachment']['name']);
7272
$this->assertEquals('<p>Thanks for your oder</p>', $attachments[1]['attachment']['data']);
7373
}
74+
75+
/** @test */
76+
function it_extracts_the_from_address_and_or_name()
77+
{
78+
$email = Email::compose()->mailable(
79+
(new TestMailable())
80+
->from('marick@dolphiq.nl', 'Marick')
81+
)->send();
82+
83+
$this->assertTrue($email->hasFrom());
84+
$this->assertEquals('marick@dolphiq.nl', $email->getFromAddress());
85+
$this->assertEquals('Marick', $email->getFromName());
86+
87+
$email = Email::compose()->mailable(
88+
(new TestMailable())
89+
->from('marick@dolphiq.nl')
90+
)->send();
91+
92+
$this->assertTrue($email->hasFrom());
93+
$this->assertEquals('marick@dolphiq.nl', $email->getFromAddress());
94+
$this->assertEquals(config('mail.from.name'), $email->getFromName());
95+
96+
$email = Email::compose()->mailable(
97+
(new TestMailable())
98+
->from(null, 'Marick')
99+
)->send();
100+
101+
$this->assertTrue($email->hasFrom());
102+
$this->assertEquals(config('mail.from.address'), $email->getFromAddress());
103+
$this->assertEquals('Marick', $email->getFromName());
104+
}
74105
}
75106

76107
class TestMailable extends Mailable

tests/SenderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@ function the_email_has_a_correct_from_email_and_from_name()
4343

4444
$this->assertEquals('testfromaddress@gmail.com', key($from));
4545
$this->assertEquals('From CI test', $from[key($from)]);
46+
47+
// custom from...
48+
$this->sent = [];
49+
50+
$this->composeEmail()->from('marick@dolphiq.nl', 'Marick')->send();
51+
$this->artisan('email:send');
52+
$from = reset($this->sent)->getMessage()->getFrom();
53+
$this->assertEquals('marick@dolphiq.nl', key($from));
54+
$this->assertEquals('Marick', $from[key($from)]);
55+
56+
// only address
57+
$this->sent = [];
58+
$this->composeEmail()->from('marick@dolphiq.nl')->send();
59+
$this->artisan('email:send');
60+
$from = reset($this->sent)->getMessage()->getFrom();
61+
$this->assertEquals('marick@dolphiq.nl', key($from));
62+
$this->assertEquals(config('mail.from.name'), $from[key($from)]);
63+
64+
// only name
65+
$this->sent = [];
66+
$this->composeEmail()->from(null, 'Marick')->send();
67+
$this->artisan('email:send');
68+
$from = reset($this->sent)->getMessage()->getFrom();
69+
$this->assertEquals(config('mail.from.address'), key($from));
70+
$this->assertEquals('Marick', $from[key($from)]);
4671
}
4772

4873
/** @test */

0 commit comments

Comments
 (0)