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('[email protected]', 'Marick')->send();
114+
115+
$this->assertTrue($email->hasFrom());
116+
$this->assertEquals('[email protected]', $email->getFromAddress());
117+
$this->assertEquals('Marick', $email->getFromName());
118+
}
119+
104120
/** @test */
105121
function variables_should_be_saved_correctly()
106122
{

0 commit comments

Comments
 (0)