Skip to content

Commit

Permalink
Merge pull request #5 from frenchykiller/feature/variable-subject-line
Browse files Browse the repository at this point in the history
Added renderSubject method to allow for variables in email subject field
  • Loading branch information
sebastienheyd authored Oct 4, 2021
2 parents f92f144 + a6b6c2a commit 0471e43
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":[],"times":[]}
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ Permissions and roles are manageable by default with `sebastienheyd/boilerplate`
In the editing of the content of an e-mail, you will find a "Insert a variable" button. This button allows you to insert
a variable in the e-mail and make it uneditable.

However, you can also enter the variables by hand by framing them with [ and ].
However, you can also enter the variables by hand by framing them with [ and ]. In this manner, you can also add
variables to the subject line of the email.

Example : "Hello [first_name]"

Expand All @@ -83,4 +84,4 @@ Email::find(1)->send('[email protected]', $data);

## Package update

Version 7 has undergone a major upgrade, do not upgrade to this version without knowing what you are doing.
Version 7 has undergone a major upgrade, do not upgrade to this version without knowing what you are doing.
5 changes: 3 additions & 2 deletions src/Mail/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ class Email extends Mailable
*
* @return void
*/
public function __construct($id, $data = [])
public function __construct($id, $data = [], $subjectData = [])
{
$this->id = $id;
$this->data = $data;
$this->subjectData = $subjectData;
}

/**
Expand All @@ -36,7 +37,7 @@ public function build()
$email = EmailModel::findOrFail($this->id);

$this->html($email->render($this->data))
->subject($email->subject)
->subject($email->renderSubject($email->subject, $this->subjectData))
->from(
$email->sender_email ?? config('mail.from.address'),
$email->sender_name ?? config('mail.from.name')
Expand Down
21 changes: 19 additions & 2 deletions src/Models/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ private function domTextReplace($search, $replace, &$domNode)
}
}

/**
* Render email subject line.
*
* @param string $subject
* @param array $data
*
* @return string
*/
public function renderSubject($subject, $data = [])
{
foreach ($data as $k => $v) {
$subject = str_replace("[$k]", $v, $subject);
}

return $subject;
}

/**
* Render email content.
*
Expand Down Expand Up @@ -176,9 +193,9 @@ private function minify($content)
* @param string $to
* @param array $data
*/
public function send($to, $data = [])
public function send($to, $data = [], $subjectData = [])
{
$mail = new EmailToSend($this->id, $data);
$mail = new EmailToSend($this->id, $data, $subjectData);
Mail::to($to)->send($mail);
}
}

0 comments on commit 0471e43

Please sign in to comment.