Skip to content

Custom sender per e-mail #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function up()
}

Schema::table('emails', function (Blueprint $table) {
$table->binary('attachments')->after('body')->default('');
$table->binary('attachments')->nullable()->after('body');
});
}

Expand Down
34 changes: 34 additions & 0 deletions database/migrations/2017_12_22_114011_add_from_to_emails_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddFromToEmailsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (Schema::hasColumn('emails', 'from')) {
return;
}

Schema::table('emails', function (Blueprint $table) {
$table->binary('from')->after('body')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
41 changes: 41 additions & 0 deletions src/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @property $id
* @property $label
* @property $recipient
* @property $from
* @property $cc
* @property $bcc
* @property $subject
Expand Down Expand Up @@ -84,6 +85,36 @@ public function getRecipient()
return $this->recipient;
}

/**
* Get the e-mail from.
*
* @return array
*/
public function getFrom()
{
return $this->from;
}

/**
* Get the e-mail from address.
*
* @return string|null
*/
public function getFromAddress()
{
return $this->from['address'] ?? config('mail.from.address');
}

/**
* Get the e-mail from address.
*
* @return string|null
*/
public function getFromName()
{
return $this->from['name'] ?? config('mail.from.name');
}

/**
* Get the e-mail recipient(s) as string.
*
Expand Down Expand Up @@ -231,6 +262,16 @@ public function getError()
return $this->error;
}

/**
* Determine if the e-mail should be sent with custom from values.
*
* @return bool
*/
public function hasFrom()
{
return is_array($this->from) && count($this->from) > 0;
}

/**
* Determine if the e-mail should be sent as a carbon copy.
*
Expand Down
12 changes: 12 additions & 0 deletions src/EmailComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ public function label($label)
return $this->setData('label', $label);
}

/**
* Set the e-mail from address and aname.
*
* @param array $address
* @param array $name
* @return static
*/
public function from($address = null, $name = null)
{
return $this->setData('from', compact('address', 'name'));
}

/**
* Set the e-mail recipient(s).
*
Expand Down
16 changes: 16 additions & 0 deletions src/Encrypter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function encrypt(EmailComposer $composer)

$this->encryptRecipients($composer);

$this->encryptFrom($composer);

$this->encryptSubject($composer);

$this->encryptVariables($composer);
Expand Down Expand Up @@ -48,6 +50,20 @@ private function encryptRecipients(EmailComposer $composer)
]);
}

/**
* Encrypt the e-mail addresses for the from field.
*
* @param EmailComposer $composer
*/
private function encryptFrom(EmailComposer $composer)
{
$email = $composer->getEmail();

$email->fill([
'from' => encrypt($email->from),
]);
}

/**
* Encrypt the e-mail subject.
*
Expand Down
2 changes: 2 additions & 0 deletions src/HasEncryptedAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ trait HasEncryptedAttributes
*/
private $encrypted = [
'recipient',
'from',
'cc',
'bcc',
'subject',
Expand All @@ -27,6 +28,7 @@ trait HasEncryptedAttributes
*/
private $encoded = [
'recipient',
'from',
'cc',
'bcc',
'variables',
Expand Down
17 changes: 17 additions & 0 deletions src/MailableReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public function read(EmailComposer $composer)
{
$this->readRecipient($composer);

$this->readFrom($composer);

$this->readCc($composer);

$this->readBcc($composer);
Expand Down Expand Up @@ -54,6 +56,21 @@ private function readRecipient(EmailComposer $composer)
$composer->recipient($to);
}

/**
* Read the mailable from field to the email composer.
*
* @param EmailComposer $composer
*/
private function readFrom(EmailComposer $composer)
{
$from = reset($composer->getData('mailable')->from);

$composer->from(
$from['address'],
$from['name']
);
}

/**
* Read the mailable cc to the email composer.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Preparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function prepare(EmailComposer $composer)

$this->prepareRecipient($composer);

$this->prepareFrom($composer);

$this->prepareCc($composer);

$this->prepareBcc($composer);
Expand Down Expand Up @@ -66,6 +68,18 @@ private function prepareRecipient(EmailComposer $composer)
]);
}

/**
* Prepare the from values for database storage.
*
* @param EmailComposer $composer
*/
private function prepareFrom(EmailComposer $composer)
{
$composer->getEmail()->fill([
'from' => json_encode($composer->getData('from', '')),
]);
}

/**
* Prepare the carbon copies for database storage.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private function buildMessage(Message $message, Email $email)
->cc($email->hasCc() ? $email->getCc() : [])
->bcc($email->hasBcc() ? $email->getBcc() : [])
->subject($email->getSubject())
->from(config('mail.from.address'), config('mail.from.name'))
->from($email->getFromAddress(), $email->getFromName())
->setBody($email->getBody(), 'text/html');

$attachmentMap = [
Expand Down
16 changes: 16 additions & 0 deletions tests/DatabaseInteractionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ function the_body_should_be_saved_correctly()
$this->assertSame($expectedBody, $email->getBody());
}

/** @test */
function from_should_be_saved_correctly()
{
$email = $this->composeEmail()->send();

$this->assertFalse($email->hasFrom());
$this->assertEquals(config('mail.from.address'), $email->getFromAddress());
$this->assertEquals(config('mail.from.name'), $email->getFromName());

$email = $this->composeEmail()->from('[email protected]', 'Marick')->send();

$this->assertTrue($email->hasFrom());
$this->assertEquals('[email protected]', $email->getFromAddress());
$this->assertEquals('Marick', $email->getFromName());
}

/** @test */
function variables_should_be_saved_correctly()
{
Expand Down
16 changes: 15 additions & 1 deletion tests/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function the_variables_should_be_encrypted_and_decrypted()
$email = $this->sendEmail(['variables' => ['name' => 'Jane Doe']]);

$this->assertEquals(
['name'=> 'Jane Doe'],
['name' => 'Jane Doe'],
decrypt($email->getOriginal('variables'))
);

Expand All @@ -83,4 +83,18 @@ function the_body_should_be_encrypted_and_decrypted()

$this->assertEquals($expectedBody, $email->getBody());
}

/** @test */
function from_should_be_encrypted_and_decrypted()
{
$email = $this->composeEmail()->from('[email protected]', 'Marick')->send();

$expect = [
'address' => '[email protected]',
'name' => 'Marick',
];

$this->assertEquals($expect, decrypt($email->getOriginal('from')));
$this->assertEquals($expect, $email->getFrom());
}
}
31 changes: 31 additions & 0 deletions tests/MailableReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,37 @@ function it_extracts_attachments()
$this->assertEquals('order.html', $attachments[1]['attachment']['name']);
$this->assertEquals('<p>Thanks for your oder</p>', $attachments[1]['attachment']['data']);
}

/** @test */
function it_extracts_the_from_address_and_or_name()
{
$email = Email::compose()->mailable(
(new TestMailable())
->from('[email protected]', 'Marick')
)->send();

$this->assertTrue($email->hasFrom());
$this->assertEquals('[email protected]', $email->getFromAddress());
$this->assertEquals('Marick', $email->getFromName());

$email = Email::compose()->mailable(
(new TestMailable())
->from('[email protected]')
)->send();

$this->assertTrue($email->hasFrom());
$this->assertEquals('[email protected]', $email->getFromAddress());
$this->assertEquals(config('mail.from.name'), $email->getFromName());

$email = Email::compose()->mailable(
(new TestMailable())
->from(null, 'Marick')
)->send();

$this->assertTrue($email->hasFrom());
$this->assertEquals(config('mail.from.address'), $email->getFromAddress());
$this->assertEquals('Marick', $email->getFromName());
}
}

class TestMailable extends Mailable
Expand Down
25 changes: 25 additions & 0 deletions tests/SenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ function the_email_has_a_correct_from_email_and_from_name()

$this->assertEquals('[email protected]', key($from));
$this->assertEquals('From CI test', $from[key($from)]);

// custom from...
$this->sent = [];

$this->composeEmail()->from('[email protected]', 'Marick')->send();
$this->artisan('email:send');
$from = reset($this->sent)->getMessage()->getFrom();
$this->assertEquals('[email protected]', key($from));
$this->assertEquals('Marick', $from[key($from)]);

// only address
$this->sent = [];
$this->composeEmail()->from('[email protected]')->send();
$this->artisan('email:send');
$from = reset($this->sent)->getMessage()->getFrom();
$this->assertEquals('[email protected]', key($from));
$this->assertEquals(config('mail.from.name'), $from[key($from)]);

// only name
$this->sent = [];
$this->composeEmail()->from(null, 'Marick')->send();
$this->artisan('email:send');
$from = reset($this->sent)->getMessage()->getFrom();
$this->assertEquals(config('mail.from.address'), key($from));
$this->assertEquals('Marick', $from[key($from)]);
}

/** @test */
Expand Down