Skip to content

Commit

Permalink
implement sendMail action (#32)
Browse files Browse the repository at this point in the history
* implement sendMail action

* typo
  • Loading branch information
DarkSide666 committed Mar 17, 2020
1 parent 794c94a commit 867b413
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/Feature/SendEmailAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace atk4\login\Feature;

use atk4\data\UserAction;

/**
* Adding this trait to your user model will allow users to send emails. Additionally execute
* $this->initSendEmailAction() from your init() method.
*
* @package atk4\login\Feature
*/
trait SendEmailAction
{
/**
* Adds sendEmail action.
*
* @return UserAction\Generic
*/
public function initSendEmailAction(): UserAction\Generic
{
return $this->addAction('sendEmail', [
'callback' => [$this, 'sendEmail'],
'scope' => UserAction\Generic::SINGLE_RECORD,
'description' => 'Send e-mail to user',
'ui' => ['icon' => 'mail'],
'args' => [
'subject' => ['caption' => 'Subject', 'type' => 'string'],
'message' => ['caption' => 'Message', 'type' => 'text'],
],
]);
}

/**
* Sends email.
*
* @param string $subject Email subject
* @param string $message Email body
*
* @return bool
*/
public function sendEmail(string $subject, string $message): bool
{
$to = $this['email'];
$message = str_replace(["\r\n", "\r", "\n"], PHP_EOL, $message);
$message = wordwrap($message, 70, PHP_EOL);

return mail($to, $subject, $message);
}
}

0 comments on commit 867b413

Please sign in to comment.