Skip to content

Commit 0b539c1

Browse files
Added functionality to add from e-mail address and from name
1 parent fab1cfb commit 0b539c1

File tree

8 files changed

+96
-1
lines changed

8 files changed

+96
-1
lines changed

database/migrations/2017_12_14_151403_create_emails_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function up()
2121
$table->increments('id');
2222
$table->string('label')->nullable();
2323
$table->binary('recipient');
24+
$table->binary('from')>nullable();
2425
$table->binary('cc')->nullable();
2526
$table->binary('bcc')->nullable();
2627
$table->binary('subject');

src/Email.php

Lines changed: 21 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,16 @@ public function getRecipient()
8485
return $this->recipient;
8586
}
8687

88+
/**
89+
* Get the e-mail from.
90+
*
91+
* @return string|null
92+
*/
93+
public function getFrom()
94+
{
95+
return $this->from;
96+
}
97+
8798
/**
8899
* Get the e-mail recipient(s) as string.
89100
*
@@ -231,6 +242,16 @@ public function getError()
231242
return $this->error;
232243
}
233244

245+
/**
246+
* Determine if the e-mail should be sent with custom from values.
247+
*
248+
* @return bool
249+
*/
250+
public function hasFrom()
251+
{
252+
return strlen($this->getOriginal('from')) > 0;
253+
}
254+
234255
/**
235256
* Determine if the e-mail should be sent as a carbon copy.
236257
*

src/EmailComposer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ 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 $from
99+
* @return static
100+
*/
101+
public function from($address, $name)
102+
{
103+
return $this->setData('from', ['address' => $address, 'name' => $name]);
104+
}
105+
95106
/**
96107
* Set the e-mail recipient(s).
97108
*

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public function read(EmailComposer $composer)
1616
{
1717
$this->readRecipient($composer);
1818

19+
20+
21+
$this->readFrom($composer);
22+
23+
24+
1925
$this->readCc($composer);
2026

2127
$this->readBcc($composer);
@@ -54,6 +60,20 @@ private function readRecipient(EmailComposer $composer)
5460
$composer->recipient($to);
5561
}
5662

63+
/**
64+
* Read the mailable from field to the email composer.
65+
*
66+
* @param EmailComposer $composer
67+
*/
68+
private function readFrom(EmailComposer $composer)
69+
{
70+
$from = $this->convertMailableAddresses(
71+
$composer->getData('mailable')->from
72+
);
73+
74+
$composer->from($from);
75+
}
76+
5777
/**
5878
* Read the mailable cc to the email composer.
5979
*

src/Preparer.php

Lines changed: 18 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,22 @@ 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+
if (!$composer->hasData('from')) {
79+
return;
80+
}
81+
82+
$composer->getEmail()->fill([
83+
'from' => json_encode($composer->getData('from')),
84+
]);
85+
}
86+
6987
/**
7088
* Prepare the carbon copies for database storage.
7189
*

src/Sender.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,17 @@ private function getMailerInstance()
4545
*/
4646
private function buildMessage(Message $message, Email $email)
4747
{
48+
49+
$from = $email->hasFrom() ? $email->getFrom() : ['address' => config('mail.from.address'), 'name' => config('mail.from.name')];
50+
foreach ($from as $key => $val) {
51+
$from[$key] = !empty($val) ? $val : config('mail.from.' . $key);
52+
}
53+
4854
$message->to($email->getRecipient())
4955
->cc($email->hasCc() ? $email->getCc() : [])
5056
->bcc($email->hasBcc() ? $email->getBcc() : [])
5157
->subject($email->getSubject())
52-
->from(config('mail.from.address'), config('mail.from.name'))
58+
->from($from['address'], $from['name'])
5359
->setBody($email->getBody(), 'text/html');
5460

5561
$attachmentMap = [

0 commit comments

Comments
 (0)