How to compose mail in yii2 onclick on any menu

In Yii2, you can compose a mail by clicking on a menu item by using the following steps:

  1. Create a mail composer widget: You can create a mail composer widget by extending the yii\widgets\ActiveForm class. This widget will handle the form fields for the mail composer.

Here's an example of how you can create a mail composer widget:

namespace app\widgets;

use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\mail\MessageInterface;

class MailComposerWidget extends ActiveForm
{
    public $model;

    public function init()
    {
        parent::init();
        $this->fields = [
            'subject' => [
                'type' => 'text',
                'label' => 'Subject',
            ],
            'body' => [
                'type' => 'textarea',
                'label' => 'Body',
            ],
        ];
    }

    public function run()
    {
        return parent::run();
    }
}
  1. Create a mail composer controller action: You need to create a controller action that will handle the mail composer widget. This action will receive the form data from the mail composer widget and send the mail.

Here's an example of how you can create a mail composer controller action:

namespace app\controllers;

use yii\web\Controller;
use yii\helpers\ArrayHelper;
use yii\mail\MessageInterface;
use app\widgets\MailComposerWidget;

class MailComposerController extends Controller
{
    public function actionCompose()
    {
        $model = new MailComposerWidget();
        if ($model->load(Yii::$app->request->post())) {
            $mail = new Message();
            $mail->setFrom('[email protected]');
            $mail->setTo('[email protected]');
            $mail->setSubject($model->subject);
            $mail->setTextBody($model->body);
            $mail->send();
            Yii::$app->session->setFlash('success', 'Mail sent successfully!');
            return $this->redirect(['compose']);
        }
        return $this->render('compose', ['model' => $model]);
    }
}
  1. Create a mail composer view: You need to create a view for the mail composer widget. This view will render the mail composer widget and handle the form submission.

Here's an example of how you can create a mail composer view:

namespace app\views\mailcomposer;

use yii\helpers\Html;
use app\widgets\MailComposerWidget;

$this->title = 'Compose Mail';
?>

<h1>Compose Mail</h1>

<?= $this->render('_form', ['model' => $model]);?>
  1. Add the mail composer widget to the menu: You need to add the mail composer widget to the menu by using the yii\widgets\Menu widget.

Here's an example of how you can add the mail composer widget to the menu:

namespace app\views\site;

use yii\widgets\Menu;
use app\widgets\MailComposerWidget;

$menuItems = [
    ['label' => 'Compose Mail', 'url' => ['mailcomposer/compose'], 'options' => ['class' => 'nav-link']],
];

echo Menu::widget([
    'items' => $menuItems,
]);
  1. Add the mail composer controller action to the route: You need to add the mail composer controller action to the route by using the yii\helpers\Url class.

Here's an example of how you can add the mail composer controller action to the route:

namespace app\config;

use yii\helpers\Url;

return [
    'components' => [
        'urlManager' => [
            'rules' => [
                ['pattern' => 'mailcomposer/compose', 'route' => 'mailcomposer/compose'],
            ],
        ],
    ],
];

That's it! Now you can compose a mail by clicking on the "Compose Mail" menu item.